using BLL;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using AspNet = System.Web.UI.WebControls;
namespace FineUIPro.Web.HJGL.WeldingReport
{
    public partial class PrefabricatedInstall : PageBase
    {
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Funs.DropDownPageSize(this.ddlPageSize);
                this.ddlPageSize.SelectedValue = this.Grid1.PageSize.ToString();
                //单位
                //var unit = BLL.UnitService.GetUnitByUnitId(this.CurrUser.UnitId);
                if (this.CurrUser.UnitId!=Const.UnitId_TCC && this.CurrUser.UnitId != Const.hfnbdId)
                {
                    BLL.UnitService.InitSubUnitNameDownList(this.drpUnit, this.CurrUser.LoginProjectId, this.CurrUser.UnitId, true);
                    this.drpUnit.SelectedValue = this.CurrUser.UnitId;
                    this.drpUnit.Enabled = false;
                    BLL.WorkAreaService.InitWorkAreaProjectUnitDropDownList(this.drpWorkArea, this.CurrUser.LoginProjectId, this.CurrUser.UnitId, true);
                }
                else
                {
                    //var supervisonUnit = BLL.WorkAreaService.IsSupervisor(this.CurrUser.UnitId, this.CurrUser.LoginProjectId);
                    //if (supervisonUnit)
                    //{
                    //    BLL.UnitService.InitSubUnitNameDownList(this.drpUnit, this.CurrUser.LoginProjectId, this.CurrUser.UnitId, true);
                    //}
                    //else
                    {
                        BLL.UnitService.InitUnitByProjectIdUnitTypeDropDownList(this.drpUnit, this.CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_2, true);
                    }
                    Funs.FineUIPleaseSelect(this.drpWorkArea);
                }
                BLL.Base_MaterialService.InitMaterialDropDownList(this.drpMaterialId, true, this.CurrUser.LoginProjectId);//材质
                //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 && !string.IsNullOrEmpty(this.drpUnit.SelectedValue))
            {
                listStr.Add(new SqlParameter("@unitNo", this.drpUnit.SelectedValue));
            }
            else
            {
                listStr.Add(new SqlParameter("@unitNo", null));
            }
            if (this.drpWorkArea.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpWorkArea.SelectedValue))
            {
                listStr.Add(new SqlParameter("@areaNo", this.drpWorkArea.SelectedValue));
            }
            else
            {
                listStr.Add(new SqlParameter("@areaNo", null));
            }
            if (this.drpMaterialId.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpMaterialId.SelectedValue))
            {
                listStr.Add(new SqlParameter("@steel", this.drpMaterialId.SelectedValue));
            }
            else
            {
                listStr.Add(new SqlParameter("@steel", 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));
            }
            SqlParameter[] parameter = listStr.ToArray();
            tb = SQLHelper.GetDataTableRunProc("sp_rpt_iso_yzazjd", 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)
            {
                decimal? max_din = 0;
                decimal? total_din = 0;
                decimal? finished_total_din = 0;
                decimal? total_Sdin = 0;
                decimal? finished_total_Sdin = 0;
                decimal? total_Fdin = 0;
                decimal? finished_total_Fdin = 0;
                int? total_hot = 0;
                int? finish_hot = 0;
                decimal? hot_proess_f = 0;
                foreach (DataRow row in tb.Rows)
                {
                    max_din += Funs.GetNewDecimalOrZero(row["max_din"].ToString());
                    total_din += Funs.GetNewDecimalOrZero(row["total_din"].ToString());
                    finished_total_din += Funs.GetNewDecimalOrZero(row["finished_total_din"].ToString());
                    total_Sdin += Funs.GetNewDecimalOrZero(row["total_Sdin"].ToString());
                    finished_total_Sdin += Funs.GetNewDecimalOrZero(row["finished_total_Sdin"].ToString());
                    total_Fdin += Funs.GetNewDecimalOrZero(row["total_Fdin"].ToString());
                    finished_total_Fdin += Funs.GetNewDecimalOrZero(row["finished_total_Fdin"].ToString());
                    total_hot += Funs.GetNewIntOrZero(row["total_hot_jot"].ToString());
                    finish_hot += Funs.GetNewIntOrZero(row["finished_hot_jot"].ToString());
                }
                if(total_hot.HasValue&& total_hot.Value > 0.1)
                {
                    hot_proess_f = finish_hot / total_hot;
                }
                JObject summary = new JObject
            {
                { "tfNumber", "合计" },
                { "max_din", max_din },
                { "total_din", total_din },
                { "finished_total_din", finished_total_din },
                { "total_Sdin", total_Sdin },
                { "finished_total_Sdin", finished_total_Sdin },
                { "total_Fdin", total_Fdin },
                { "finished_total_Fdin", finished_total_Fdin },
                { "total_hot_jot", total_hot },
                { "finished_hot_jot", finish_hot },
                { "hot_proess_f", hot_proess_f }
            };
                Grid1.SummaryData = summary;
            }
        }
        #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();
            grid.PageSize = this.Grid1.RecordCount;
            BindGrid();
            sb.Append("");
            sb.Append("");
            sb.Append("");
            foreach (GridColumn column in grid.Columns)
            {
                sb.AppendFormat("| {0} | ", column.HeaderText);
            }
            sb.Append("
");
            foreach (GridRow row in grid.Rows)
            {
                sb.Append("");
                foreach (GridColumn column in grid.Columns)
                {
                    string html = row.Values[column.ColumnIndex].ToString();
                    if (column.ColumnID == "tfNumber")
                    {
                        html = (row.FindControl("labNumber") as AspNet.Label).Text;
                    }
                    sb.AppendFormat("| {0} | ", html);
                }
                sb.Append("
");
            }
            var sumary = grid.SummaryData;
            if (sumary != null)
            {
                foreach (GridColumn column in grid.Columns)
                {
                    try
                    {
                        if (sumary.ContainsKey(column.ColumnID))
                        {
                            sb.AppendFormat("{0} | ", sumary.GetValue(column.ColumnID).ToString());
                        }
                        else
                        {
                            sb.AppendFormat("{0} | ", "");
                        }
                    }
                    catch (Exception e)
                    {
                        sb.AppendFormat("{0} | ", "");
                    }
                }
            }
            sb.Append("
");
            return sb.ToString();
        }
        #endregion
        #region DropDownList下拉选择事件
        /// 
        /// 单位下拉选择事件
        /// 
        /// 
        /// 
        protected void drpUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.drpWorkArea.Items.Clear();
            if (this.drpUnit.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpUnit.SelectedValue))
            {
                BLL.WorkAreaService.InitWorkAreaProjectUnitDropDownList(this.drpWorkArea, this.CurrUser.LoginProjectId, this.drpUnit.SelectedValue, true);
                this.drpWorkArea.SelectedIndex = 0;
            }
        }
        #endregion
    }
}