using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using BLL;
namespace FineUIPro.Web.WeldingProcess.WeldingReport
{
    public partial class RTRejectedRateReport : PageBase
    {
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BLL.Base_UnitService.InitProjectUnitDropDownList(this.drpUnitId, false, this.CurrUser.LoginProjectId, BLL.Const.UnitType_5, Resources.Lan.PleaseSelect);
                this.txtStarTime.Text = String.Format("{0:yyyy-MM-dd}", System.DateTime.Now.AddDays(-7));
                this.txtEndTime.Text = String.Format("{0:yyyy-MM-dd}", System.DateTime.Now);
            }
        }
        /// 
        /// 确定按钮
        /// 
        /// 
        /// 
        protected void btnSure_Click(object sender, EventArgs e)
        {
            string units = string.Empty;
            if (this.drpUnitId.SelectedItem != null)
            {
                foreach (ListItem item in drpUnitId.SelectedItemArray)
                {
                    units += item.Value + ",";
                }
                if (!string.IsNullOrEmpty(units))
                {
                    units = units.Substring(0, units.LastIndexOf(','));
                }
            }
            this.gvDataSetChart(units);
        }
        #region 给gv赋值 并设置图像值
        /// 
        /// 给gv赋值 并设置图像值
        /// 
        protected void gvDataSetChart(string units)
        {
            if (!string.IsNullOrEmpty(this.txtStarTime.Text) && !string.IsNullOrEmpty(this.txtEndTime.Text))
            {
                SqlParameter[] values = new SqlParameter[]
                {
                    new SqlParameter("@UnitIds",units),
                    new SqlParameter("@date1", this.txtStarTime.Text),
                    new SqlParameter("@date2", this.txtEndTime.Text),
                    new SqlParameter("@projectId", this.CurrUser.LoginProjectId)
                };
                DataTable dtchart = BLL.SQLHelper.GetDataTableRunProc("sp_RTRejectedRate", values);
                DataTable dt = dtchart.Copy();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var unit = BLL.Funs.DB.Base_Unit.FirstOrDefault(x => x.UnitId == dt.Rows[i][0].ToString());
                    if (unit != null)
                    {
                        dt.Rows[i][0] = unit.UnitName + "(" + unit.UnitCode + ")";
                    }
                }
                this.gvRtRepairRate.DataSource = dt;
                this.gvRtRepairRate.DataBind();
                CreateChart(dtchart, Chart1);
            }
            else
            {
                //this.tabSearch.Visible = true;
                ScriptManager.RegisterStartupScript(this, typeof(string), "_alert", "alert('请选择查询条件!')", true);
            }
        }
        #endregion
        private void CreateChart(DataTable dt, Chart chart1)
        {
            chart1.BackColor = Color.WhiteSmoke;
            chart1.ImageLocation = "~/Images/ChartPic_#SEQ(300,3)";
            chart1.BorderlineDashStyle = ChartDashStyle.Solid;
            chart1.Palette = ChartColorPalette.BrightPastel;
            chart1.BackSecondaryColor = Color.White;
            chart1.BackGradientStyle = GradientStyle.TopBottom;
            chart1.BorderWidth = 2;
            chart1.BorderColor = Color.FromArgb(26, 59, 105);
            chart1.ImageType = ChartImageType.Png;
            Title title = new Title();
            title.Text = "周RT检测返修比例图表";
            title.ShadowColor = Color.FromArgb(32, 0, 0, 0);
            title.Font = new Font("Trebuchet MS", 12F, FontStyle.Bold);
            title.ShadowOffset = 3;
            title.ForeColor = Color.FromArgb(26, 59, 105);
            chart1.Titles.Add(title);
            Legend legend1 = new Legend();
            legend1.TextWrapThreshold = 1;
            legend1.Docking = Docking.Right;
            legend1.Alignment = StringAlignment.Center;
            legend1.BackColor = Color.Transparent;
            legend1.Font = new Font(new FontFamily("Trebuchet MS"), 8);
            legend1.LegendStyle = LegendStyle.Column;
            legend1.IsEquallySpacedItems = true;
            legend1.IsTextAutoFit = false;
            chart1.Legends.Add(legend1);
            ChartArea chartArea = new ChartArea();
            chartArea.Name = "周RT检测返修比例";
            chartArea.BackColor = Color.Transparent;
            chartArea.AxisX.IsLabelAutoFit = false;
            chartArea.AxisY.IsLabelAutoFit = false;
            chartArea.AxisX.LabelStyle.Font = new Font("Verdana,Arial,Helvetica,sans-serif", 8F, FontStyle.Regular);
            chartArea.AxisY.LabelStyle.Font = new Font("Verdana,Arial,Helvetica,sans-serif", 8F, FontStyle.Regular);
            chartArea.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);
            chartArea.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);
            chartArea.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
            chartArea.AxisX.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
            chartArea.AxisX.Interval = 1;
            chartArea.Area3DStyle.Enable3D = ckb.Checked;
            chart1.ChartAreas.Add(chartArea);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string unitCode = null;
                var unit = BLL.Funs.DB.Base_Unit.FirstOrDefault(x => x.UnitId == dt.Rows[i][0].ToString());
                if (unit != null)
                {
                    unitCode = unit.UnitCode;
                }
                chart1.Series.Add(unitCode);
                if (!string.IsNullOrEmpty(drpChartType.SelectedValue))
                {
                    chart1.Series[unitCode].ChartType = ChartControlService.GetChartType(drpChartType.SelectedValue);
                }
                else
                {
                    chart1.Series[unitCode].ChartType = ChartControlService.GetChartType("Column");
                }
                chart1.Series[unitCode].Name = unitCode;
                chart1.Series[unitCode].IsValueShownAsLabel = true;
                chart1.Series[unitCode].BorderWidth = 2;
                chart1.Series[unitCode]["DrawingStyle"] = "Cylinder";
                if (dt.Columns.Count > 0)
                {
                    for (int j = 1; j < dt.Columns.Count; j++)
                    {
                        chart1.Series[unitCode].Points.AddXY(dt.Columns[j].ColumnName, dt.Rows[i][j].ToString());
                        chart1.Series[unitCode].Points[j - 1].ToolTip = "#VALX:#VALY";
                    }
                }
            }
        }
        #region gv 绑定列宽
        /// 
        /// gv 绑定
        /// 
        /// 
        /// 
        protected void gvRtRepairRate_DataBound(object sender, EventArgs e)
        {
            if (this.gvRtRepairRate.Rows.Count > 0)
            {
                int widthValue = 0;
                for (int i = 0; i < this.gvRtRepairRate.Rows[0].Cells.Count; i++)
                {
                    if (i == 0)
                    {
                        this.gvRtRepairRate.Rows[0].Cells[i].Width = 150;
                        widthValue += 150;
                    }
                    else
                    {
                        this.gvRtRepairRate.Rows[0].Cells[i].Width = 60;
                        widthValue += 80;
                    }
                }
                if (widthValue < 1000)
                {
                    widthValue = 1000;
                }
                this.gvRtRepairRate.Width = widthValue;
            }
        }
        protected void gvRtRepairRate_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //获取表头所在行的所有单元格
                //TableCellCollection tcHeader = e.Row.Cells;
                TableCell cell = e.Row.Cells[0];
                //清除自动生成的表头
                cell.Text = "施工单位 Construction Unit ";
            }
        }
        #endregion
        protected void txtStarTime_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtStarTime.Text))
            {
                this.txtEndTime.Text = String.Format("{0:yyyy-MM-dd}", Convert.ToDateTime(txtStarTime.Text).AddDays(7));
            }
        }
    }
}