using BLL;
using FineUIPro.Web.Controls;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using AspNet = System.Web.UI.WebControls;
namespace FineUIPro.Web.HJGL.WeldingReport
{
    public partial class UnitWorkAreaQuality :PageBase
    {
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Funs.DropDownPageSize(this.ddlPageSize);
                this.ddlPageSize.SelectedValue = this.Grid1.PageSize.ToString();
                List units = new List();
                var pUnit = BLL.ProjectUnitService.GetProjectUnitByUnitIdProjectId(this.CurrUser.LoginProjectId, this.CurrUser.UnitId);
                if (pUnit == null || pUnit.UnitType == Const.ProjectUnitType_1)
                {
                    units = BLL.UnitService.GetUnitByProjectIdUnitTypeList(this.CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_2);
                    //this.drpUnit.Enabled = false;
                    this.drpUnit.DataTextField = "UnitName";
                    this.drpUnit.DataValueField = "UnitId";
                    this.drpUnit.DataSource = units;
                    this.drpUnit.DataBind();
                    Funs.FineUIPleaseSelect(this.drpUnit);           
                    Funs.FineUIPleaseSelect(this.drpInstallation);
                }
                else
                {
                    units = BLL.UnitService.GetSubUnitsListBySupervisorUnitId(this.CurrUser.ProjectId, this.CurrUser.UnitId);
                    this.drpUnit.Enabled = false;
                    this.drpUnit.DataTextField = "UnitName";
                    this.drpUnit.DataValueField = "UnitId";
                    this.drpUnit.DataSource = units;
                    this.drpUnit.DataBind();
                    this.drpUnit.SelectedValue = CurrUser.UnitId;
                    Project_InstallationService.InitInstallationDropDownList(this.drpInstallation, this.CurrUser.LoginProjectId, true);
                    this.drpInstallation.SelectedValue = BLL.Const._Null;
                }
                ///单位
                Funs.FineUIPleaseSelect(this.drpWorkArea);
                ////钢材类型
                this.drpSteType.DataTextField = "Text";
                this.drpSteType.DataValueField = "Value";
                this.drpSteType.DataSource = BLL.DropListService.HJGL_GetSteTypeList();
                this.drpSteType.DataBind();
                Funs.FineUIPleaseSelect(this.drpSteType);
                //显示列
                //Model.Sys_UserShowColumns c = BLL.UserShowColumnsService.GetColumnsByUserId(this.CurrUser.UserId, Const.UnitAreaQualityMenuId);
                //if (c != null)
                //{
                //    this.GetShowColumn(c.Columns);
                //}
                //BindGrid();
            }
        }
        #endregion
        #region 绑定BindGrid
        /// 
        /// 
        /// 
        private DataTable tb = null;
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            List listStr = new List
            {
                new SqlParameter("@projectId", this.CurrUser.LoginProjectId)
            };
            if (this.drpUnit.SelectedValue != BLL.Const._Null)
            {
                listStr.Add(new SqlParameter("@UnitNo", this.drpUnit.SelectedValue));
            }
            else
            {
                listStr.Add(new SqlParameter("@UnitNo", null));
            }
            if (this.drpWorkArea.SelectedValue != BLL.Const._Null)
            {
                listStr.Add(new SqlParameter("@areaNo", this.drpWorkArea.SelectedValue));
            }
            else
            {
                listStr.Add(new SqlParameter("@areaNo", null));
            }
            if (this.drpInstallation.SelectedValue != BLL.Const._Null)
            {
                listStr.Add(new SqlParameter("@installationId", this.drpInstallation.SelectedValue));
            }
            else
            {
                listStr.Add(new SqlParameter("@installationId", null));
            }
            if (!string.IsNullOrEmpty(this.txtStarTime.Text))
            {
                listStr.Add(new SqlParameter("@date1", this.txtStarTime.Text.Trim()));
            }
            else
            {
                listStr.Add(new SqlParameter("@date1", null));
            }
            if (!string.IsNullOrEmpty(this.txtEndTime.Text))
            {
                listStr.Add(new SqlParameter("@date2", this.txtEndTime.Text.Trim()));
            }
            else
            {
                listStr.Add(new SqlParameter("@date2", null));
            }
            if (this.drpSteType.SelectedValue!=BLL.Const._Null&&!string.IsNullOrEmpty(this.drpSteType.SelectedValue))
            {
                listStr.Add(new SqlParameter("@ste_steeltype", this.drpSteType.SelectedValue));
            }
            else
            {
                listStr.Add(new SqlParameter("@ste_steeltype", null));
            }
            if (BLL.WorkAreaService.IsSupervisor(this.CurrUser.UnitId, this.CurrUser.LoginProjectId))
            {
                listStr.Add(new SqlParameter("@supervisorUnitId", this.CurrUser.UnitId));
            }
            else
            {
                listStr.Add(new SqlParameter("@supervisorUnitId", null));
            }
            if (this.cb_IsStandard.SelectedIndexArray != null && this.cb_IsStandard.SelectedIndexArray.Length == 1)
            {
                listStr.Add(new SqlParameter("@IsStandard", this.cb_IsStandard.SelectedValueArray[0]));
            }
            else
            {
                listStr.Add(new SqlParameter("@IsStandard", null));
            }
            SqlParameter[] parameter = listStr.ToArray();
            tb = SQLHelper.GetDataTableRunProc("sp_rpt_UnitWorkAreaQuality", parameter);
            this.Grid1.RecordCount = tb.Rows.Count;
            var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = table;
            Grid1.DataBind();
        }
        /// 
        /// 改变索引事件
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            BindGrid();
        }
        /// 
        /// 分页下拉选择事件
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e)
        {
            this.BindGrid();
        }
        #endregion
        #region 计算合计
        /// 
        /// 计算合计
        /// 
        protected void OutputSummaryData()
        {
            if (tb != null)
            {
                int total_jotSum = 0;//总焊口数          
                int total_sjotSum = 0;
                int total_fjotSum = 0;
                int finished_total_jotSum = 0;
                int finished_total_sjotSum = 0;
                int finished_total_fjotSum = 0;
                int current_total_filmSum = 0;
                int current_pass_filmSum = 0;
                int current_point_total_filmSum = 0;
                int current_point_pass_filmSum = 0;
                int current_ext_total_filmSum = 0;
                int current_ext_pass_filmSum = 0;
                int current_trust_count_totalSum = 0;
                int current_check_count_totalSum = 0;
                int total_filmSum = 0;
                int pass_filmSum = 0;
                int point_total_filmSum = 0;
                int point_pass_filmSum = 0;
                int ext_total_filmSum = 0;
                int ext_pass_filmSum = 0;
                int trust_count_totalSum = 0;
                int point_count_totalSum = 0;
                int extend_count_totalSum = 0;
                int repair_count_totalSum = 0;
                int trust_check_totalSum = 0;
                double tofd_total_donedin = 0;
                double tofd_pass_donedin = 0;
                double tofd_no_pass_donedin = 0;
                double tdfw_total_donedin = 0;
                double tdfw_pass_donedin = 0;
                double tdfw_no_pass_donedin = 0;
                double ut_total_donedin = 0;
                double ut_pass_donedin = 0;
                double ut_no_pass_donedin = 0;
                foreach (DataRow row in tb.Rows)
                {
                    total_jotSum += Funs.GetNewIntOrZero(row["total_jot"].ToString());
                    total_sjotSum += Funs.GetNewIntOrZero(row["total_sjot"].ToString());
                    total_fjotSum += Funs.GetNewIntOrZero(row["total_fjot"].ToString());
                    finished_total_jotSum += Funs.GetNewIntOrZero(row["finished_total_jot"].ToString());
                    finished_total_sjotSum += Funs.GetNewIntOrZero(row["finished_total_sjot"].ToString());
                    finished_total_fjotSum += Funs.GetNewIntOrZero(row["finished_total_fjot"].ToString());
                    current_total_filmSum += Funs.GetNewIntOrZero(row["current_total_film"].ToString());
                    current_pass_filmSum += Funs.GetNewIntOrZero(row["current_pass_film"].ToString());
                    current_point_total_filmSum += Funs.GetNewIntOrZero(row["current_point_total_film"].ToString());
                    current_point_pass_filmSum += Funs.GetNewIntOrZero(row["current_point_pass_film"].ToString());
                    current_ext_total_filmSum += Funs.GetNewIntOrZero(row["current_ext_total_film"].ToString());
                    current_ext_pass_filmSum += Funs.GetNewIntOrZero(row["current_ext_pass_film"].ToString());
                    current_trust_count_totalSum += Funs.GetNewIntOrZero(row["current_trust_count_total"].ToString());
                    current_check_count_totalSum += Funs.GetNewIntOrZero(row["current_check_count_total"].ToString());
                    total_filmSum += Funs.GetNewIntOrZero(row["total_film"].ToString());
                    pass_filmSum += Funs.GetNewIntOrZero(row["pass_film"].ToString());
                    point_total_filmSum += Funs.GetNewIntOrZero(row["point_total_film"].ToString());
                    point_pass_filmSum += Funs.GetNewIntOrZero(row["point_pass_film"].ToString());
                    ext_total_filmSum += Funs.GetNewIntOrZero(row["ext_total_film"].ToString());
                    ext_pass_filmSum += Funs.GetNewIntOrZero(row["ext_pass_film"].ToString());
                    trust_count_totalSum += Funs.GetNewIntOrZero(row["trust_count_total"].ToString());
                    point_count_totalSum += Funs.GetNewIntOrZero(row["point_count_total"].ToString());
                    extend_count_totalSum += Funs.GetNewIntOrZero(row["extend_count_total"].ToString());
                    repair_count_totalSum += Funs.GetNewIntOrZero(row["repair_count_total"].ToString());
                    trust_check_totalSum += Funs.GetNewIntOrZero(row["trust_check_total"].ToString());
                    tofd_total_donedin += Funs.GetNewDoubleOrZero(row["tofd_total_donedin"].ToString());
                    tofd_pass_donedin += Funs.GetNewDoubleOrZero(row["tofd_pass_donedin"].ToString());
                    tofd_no_pass_donedin += Funs.GetNewDoubleOrZero(row["tofd_no_pass_donedin"].ToString());
                    tdfw_total_donedin += Funs.GetNewDoubleOrZero(row["tdfw_total_donedin"].ToString());
                    tdfw_pass_donedin += Funs.GetNewDoubleOrZero(row["tdfw_no_pass_donedin"].ToString());
                    tdfw_no_pass_donedin += Funs.GetNewDoubleOrZero(row["tdfw_no_pass_donedin"].ToString());
                    ut_total_donedin += Funs.GetNewDoubleOrZero(row["ut_total_donedin"].ToString());
                    ut_pass_donedin += Funs.GetNewDoubleOrZero(row["ut_pass_donedin"].ToString());
                    ut_no_pass_donedin += Funs.GetNewDoubleOrZero(row["ut_no_pass_donedin"].ToString());
                }
                JObject summary = new JObject
            {
                { "tfNumber", "合计:" },
                { "total_jot", total_jotSum },
                { "total_sjot", total_sjotSum },
                { "total_fjot", total_fjotSum },
                { "finished_total_jot", finished_total_jotSum },
                { "finished_total_sjot", finished_total_sjotSum },
                { "finished_total_fjot", finished_total_fjotSum },
                { "current_total_film", current_total_filmSum },
                { "current_pass_film", current_pass_filmSum },
                { "current_point_total_film", current_point_total_filmSum },
                { "current_ext_total_film", current_ext_total_filmSum },
                { "current_ext_pass_film", current_ext_pass_filmSum },
                { "current_trust_count_total", current_trust_count_totalSum },
                { "current_check_count_total", current_check_count_totalSum },
                { "total_film", total_filmSum },
                { "pass_film", pass_filmSum },
                { "point_total_film", point_total_filmSum },
                { "point_pass_film", point_pass_filmSum },
                { "point_passreate", point_total_filmSum >0? (100.0*point_pass_filmSum/point_total_filmSum).ToString("0.000")+"%":""},
                { "ext_total_film", ext_total_filmSum },
                { "ext_pass_film", ext_pass_filmSum },
                { "trust_count_total", trust_count_totalSum },
                { "point_count_total", point_count_totalSum },
                { "extend_count_total", extend_count_totalSum },
                { "repair_count_total", repair_count_totalSum },
                { "trust_check_total", trust_check_totalSum },
                    { "tofd_total_donedin", tofd_total_donedin },
                { "tofd_pass_donedin", tofd_pass_donedin },
                { "tofd_no_pass_donedin", tofd_no_pass_donedin },
                 { "tofdpassrate", tofd_pass_donedin >0? (100*(tofd_pass_donedin-tofd_no_pass_donedin)/tofd_pass_donedin).ToString("0.000")+"%":""},
                    { "tdfw_total_donedin", tdfw_total_donedin },
                { "tdfw_pass_donedin", tdfw_pass_donedin },
                { "tdfw_no_pass_donedin", tdfw_no_pass_donedin },
                 { "tdfwpassrate", tdfw_pass_donedin >0? (100*(tdfw_pass_donedin-tdfw_no_pass_donedin)/tdfw_pass_donedin).ToString("0.000")+"%":""},
                    { "ut_total_donedin", ut_total_donedin },
                { "ut_pass_donedin", ut_pass_donedin },
                { "ut_no_pass_donedin", ut_no_pass_donedin },
                     { "utpassrate", ut_pass_donedin >0? (100*(ut_pass_donedin-ut_no_pass_donedin)/ut_pass_donedin).ToString("0.000")+"%":""}
                };
                Grid1.SummaryData = summary;
            }
        }
        #endregion
        #region 选择要显示列
        /// 
        /// 选择显示列
        /// 
        /// 
        /// 
        protected void btnSelectColumn_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("ShowColumn.aspx?menuId={0}", BLL.Const.UnitAreaQualityMenuId, "显示列 - ")));
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            this.BindGrid();
            // 显示列
            //var c = UserShowColumnsService.GetColumnsByUserId(this.CurrUser.UserId, Const.UnitAreaQualityMenuId);
            //if (c != null)
            //{
            //    this.GetShowColumn(c.Columns);
            //}
        }
        /// 
        /// 显示的列
        /// 
        /// 
        private void GetShowColumn(string column)
        {
            if (!string.IsNullOrEmpty(column))
            {
                List colomn = new List();
                for (int i = 0; i < Grid1.Columns.Count; i++)
                {
                    if (this.Grid1.Columns[i] is GroupField)
                    {
                        var gf = this.Grid1.Columns[i] as GroupField;
                        
                        for (int j = 0; j < gf.Columns.Count; j++)
                        {
                            gf.Columns[i].Hidden = true;
                            colomn.Add(gf.Columns[i]);
                        }
                    }
                    else
                    {
                        this.Grid1.Columns[i].Hidden = true;
                        colomn.Add(this.Grid1.Columns[i]);
                    }
                }
                List columns = column.Split(',').ToList();
                foreach (var item in columns)
                {
                    if (Convert.ToInt32(item) < colomn.Count)
                    {
                        colomn[Convert.ToInt32(item)].Hidden = false;
                    }
                }
                for (int i = 0; i < Grid1.Columns.Count; i++)
                {
                    if (this.Grid1.Columns[i] is GroupField)
                    {
                        var gf = this.Grid1.Columns[i] as GroupField;
                        for (int j = 0; j < gf.Columns.Count; j++)
                        {
                            if (!gf.Columns[i].Hidden)
                            {
                                this.Grid1.Columns[i].Hidden = false;
                                break;
                            }
                        }
                    }
                    
                }
            }
        }
        #endregion
        #region 统计按钮事件
        /// 
        /// 统计
        /// 
        /// 
        /// 
        protected void BtnAnalyse_Click(object sender, EventArgs e)
        {
            BindGrid();
            OutputSummaryData();
        }
        #endregion
        #region 导出按钮
        /// 导出按钮
        /// 
        /// 
        /// 
        protected void btnOut_Click(object sender, EventArgs e)
        {
            Response.ClearContent();
            string filename = Funs.GetNewFileName();
            Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("单位工区质量分析" + filename, System.Text.Encoding.UTF8) + ".xls");
            Response.ContentType = "application/excel";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.Write(GetGridTableHtml(Grid1));
            Response.End();
        }
        ///  
        /// 导出方法
        /// 
        /// 
        /// 
        private string GetGridTableHtml(Grid grid)
        {
            StringBuilder sb = new StringBuilder();
            MultiHeaderTable mht = new MultiHeaderTable();
            mht.ResolveMultiHeaderTable(Grid1.Columns);
            sb.Append("");
            sb.Append("");
            foreach (List
");
            return sb.ToString();
        }
        #endregion
        #region 下拉选择事件
        /// 
        /// 单位下拉选择事件
        /// 
        ///  param >
        /// < param name="e">
        protected void drpUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.drpInstallation.Items.Clear();      ///装置
            this.drpWorkArea.Items.Clear(); ///区域
            Project_InstallationService.InitInstallationDropDownList(this.drpInstallation, this.CurrUser.LoginProjectId, true);
            this.drpInstallation.SelectedValue = BLL.Const._Null;
            Funs.FineUIPleaseSelect(this.drpWorkArea);
            this.drpWorkArea.SelectedValue = BLL.Const._Null;
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        protected void drpInstallation_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.drpWorkArea.Items.Clear();
            WorkAreaService.InitWorkAreaProjectInstallUnitDropDownList(this.drpWorkArea, this.CurrUser.LoginProjectId, this.drpInstallation.SelectedValue, this.drpUnit.SelectedValue, true);        
            this.drpWorkArea.SelectedValue = BLL.Const._Null;
        }
        #endregion
    }
}