using BLL;
using FineUIPro.Web.HJGL.WeldingManage;
using Model;
using Newtonsoft.Json.Linq;
using NPOI.HPSF;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.UI.HtmlControls;
namespace FineUIPro.Web.HJGL.TrustManage
{
    public partial class PMITrustManageEditAdd : PageBase
    {
        #region 定义项
        /// 
        /// 无损委托主键
        /// 
        public string PMITrustID
        {
            get
            {
                return (string)ViewState["PMITrustID"];
            }
            set
            {
                ViewState["PMITrustID"] = value;
            }
        }
        #endregion
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.PMITrustID = Request.Params["PMITrustID"];
                //drpInstallationDropDownList(this.drpInstallation,true);
                //drpCH_TrustUnitDropDownList(this.drpCH_TrustUnit, true);
                BLL.Project_InstallationService.InitInstallationDropDownList(this.drpInstallation, this.CurrUser.LoginProjectId, true);
                Funs.FineUIPleaseSelect(this.drpWorkAreaId);
                BLL.Base_DetectionTypeService.InitDetectionTypeDropDownList(this.ddlTestMethod, true);//检测方法
                BLL.Base_DetectionRateService.InitDetectionRateDropDownList(this.ddlTesTratio, true);//检测比例
                List GetTrustItem = BLL.PMITrustManageItemService.GetView_PMI_TrustItemByCH_TrustID(this.PMITrustID);
                this.BindGrid(GetTrustItem);  // 初始化页面 
                this.PageInfoLoad(); // 加载页面                
            }
        }
        #endregion
        #region 加载页面输入提交信息
        /// 
        /// 加载页面输入提交信息
        /// 
        private void PageInfoLoad()
        {
            Model.PMI_TrustManage newPMI_TrustManage = BLL.PMITrustManageService.GetCH_TrustByID(this.PMITrustID);
            if (newPMI_TrustManage != null)
            {
                if (!string.IsNullOrEmpty(newPMI_TrustManage.UnitCode))
                {
                    this.drpInstallation.SelectedValue = newPMI_TrustManage.UnitCode;
                    //InitAreaIdDropDownList(drpInstallation, newPMI_TrustManage.UnitCode, false);
                    BLL.WorkAreaService.InitWorkAreaByProjectIdAndInstallationId(this.drpWorkAreaId, this.CurrUser.LoginProjectId, this.drpInstallation.SelectedValue, true);
                    if (!string.IsNullOrEmpty(newPMI_TrustManage.AreaIdCode))
                    {
                        this.drpWorkAreaId.SelectedValue = newPMI_TrustManage.AreaIdCode;
                    }
                }
                //工区--AreaId
                //if (!string.IsNullOrEmpty(newPMI_TrustManage.AreaIdCode))
                //{
                //    this.drpCH_TrustUnit.SelectedValue = newPMI_TrustManage.AreaIdCode;
                //}
                ////工程名称--PMIProjectName
                //this.txtPMIProjectName.Text = newPMI_TrustManage.PMIProjectName;
                ////单位工程名称--PMIUnitWorksName
                //this.txtPMIUnitWorksName.Text = newPMI_TrustManage.PMIUnitWorksName;
                //编号--SerialNum
                this.txtSerialNum.Text = newPMI_TrustManage.SerialNum;
                //检测方法--TestMethod
                if (!string.IsNullOrEmpty(newPMI_TrustManage.TestMethod))
                {
                    this.ddlTestMethod.SelectedItem.Text = newPMI_TrustManage.TestMethod;
                }
                //检测标准--TestStandard                
                this.txtTestStandard.Text = newPMI_TrustManage.TestStandard;
                //检测比例--TesTratio
                if (!string.IsNullOrEmpty(newPMI_TrustManage.TesTratio))
                {
                    this.ddlTesTratio.SelectedItem.Text = newPMI_TrustManage.TesTratio;
                }
                //合格要求--QualifiedRequire
                this.txtQualifiedRequire.Text = newPMI_TrustManage.QualifiedRequire;
                //委托编号--EntrustmentNo
                this.txtEntrustmentNo.Text = newPMI_TrustManage.EntrustmentNo;
                ////装置
                //this.drpInstallation.SelectedValue = newPMI_TrustManage.UnitCode;
                ////工区
                //this.drpCH_TrustUnit.SelectedValue = newPMI_TrustManage.AreaIdCode;
            }
            else
            {
                //工程名称--PMIProjectName
                //this.txtPMIProjectName.Text =string.Empty;
                ////单位工程名称--PMIUnitWorksName
                //this.txtPMIUnitWorksName.Text = string.Empty;
                //编号--SerialNum
                this.txtSerialNum.Text = "SH/T3543-G128";
                //检测标准--TestStandard                
                this.txtTestStandard.Text = string.Empty;
                //合格要求--QualifiedRequire
                this.txtQualifiedRequire.Text = string.Empty;
                //委托编号--EntrustmentNo
                this.txtEntrustmentNo.Text = string.Empty;
                //装置
                this.drpInstallation.SelectedValue = BLL.Const._Null;
                //工区
                this.drpWorkAreaId.SelectedValue = BLL.Const._Null;
            }
        }
        //public void drpInstallationDropDownList(DropDownList dropName, bool isShowPlease)
        //{
        //    dropName.DataValueField = "InstallationId";
        //    dropName.DataTextField = "InstallationName";
        //    dropName.DataSource = (from x in Funs.DB.PIM_View_PmiMaterialTemplate where x.ProjectId == this.CurrUser.LoginProjectId select new { x.InstallationId, x.InstallationName }).Distinct().ToList();
        //    dropName.DataBind();
        //    if (isShowPlease)
        //    {
        //        Funs.FineUIPleaseSelect(dropName);
        //    }
        //}
        //public void drpCH_TrustUnitDropDownList(DropDownList dropName, bool isShowPlease)
        //{
        //    dropName.DataValueField = "UnitId";
        //    dropName.DataTextField = "UnitName";
        //    dropName.DataSource = (from x in Funs.DB.PIM_View_PmiMaterialTemplate where x.ProjectId == this.CurrUser.LoginProjectId select new { x.UnitId, x.UnitName }).Distinct().ToList();
        //    dropName.DataBind();
        //    if (isShowPlease)
        //    {
        //        Funs.FineUIPleaseSelect(dropName);
        //    }
        //}
        //public void InitAreaIdDropDownList(DropDownList dropName, string ddlstringName, bool isShowPlease)
        //{
        //    this.drpCH_TrustUnit.DataValueField = "UnitId";
        //    this.drpCH_TrustUnit.DataTextField = "UnitName";
        //    if (isShowPlease || string.IsNullOrEmpty(ddlstringName))
        //    {
        //        Funs.FineUIPleaseSelect(dropName);
        //    }
        //    else
        //    {
        //        var ddlunitString = ddlstringName;
        //        this.drpCH_TrustUnit.DataValueField = "UnitId";
        //        this.drpCH_TrustUnit.DataTextField = "UnitName";
        //        this.drpCH_TrustUnit.DataSource = (from x in Funs.DB.PIM_View_PmiMaterialTemplate where x.ProjectId == this.CurrUser.LoginProjectId && x.InstallationId == ddlunitString select new { x.UnitId, x.UnitName }).Distinct().ToList();
        //        this.drpCH_TrustUnit.DataBind();
        //    }
        //}
        #endregion
        #region 数据绑定
        /// 
        /// 数据绑定
        /// 
        private void BindGrid(List GetTrustItem)
        {
            DataTable tb = this.LINQToDataTable(GetTrustItem);
            // 2.获取当前分页数据
            //var table = this.GetPagedDataTable(GridNewDynamic, tb1);
            Grid1.RecordCount = tb.Rows.Count;
            //tb = GetFilteredTable(Grid1.FilteredData, tb);
            var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = table;
            Grid1.DataBind();
        }
        #endregion
        #region 排序
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            List GetTrustItem = this.CollectGridJointInfo();
            this.BindGrid(GetTrustItem);
        }
        #endregion
        #region 无损委托 提交事件
        /// 
        /// 编辑无损委托
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_PMITrustManageMenuId, Const.BtnSave))
            {
                if (string.IsNullOrEmpty(this.txtEntrustmentNo.Text.Trim()))
                {
                    Alert.ShowInTop("请输入委托单号!", MessageBoxIcon.Warning);
                    return;
                }
                if (string.IsNullOrEmpty(this.drpInstallation.SelectedValue) || this.drpInstallation.SelectedValue == BLL.Const._Null)
                {
                    Alert.ShowInTop("请选择装置名称!", MessageBoxIcon.Warning);
                    return;
                }
                if (string.IsNullOrEmpty(this.drpWorkAreaId.SelectedValue) || this.drpWorkAreaId.SelectedValue == BLL.Const._Null)
                {
                    Alert.ShowInTop("请选择工区!", MessageBoxIcon.Warning);
                    return;
                }
                if (string.IsNullOrEmpty(this.ddlTestMethod.SelectedValue) || this.ddlTestMethod.SelectedValue == BLL.Const._Null)
                {
                    Alert.ShowInTop("请选择检测方法!", MessageBoxIcon.Warning);
                    return;
                }
                if (string.IsNullOrEmpty(this.ddlTesTratio.SelectedValue) || this.ddlTesTratio.SelectedValue == BLL.Const._Null)
                {
                    Alert.ShowInTop("请选择检测比例!", MessageBoxIcon.Warning);
                    return;
                }
                Model.PMI_TrustManage newPMI_TrustManage = new Model.PMI_TrustManage();
                newPMI_TrustManage.ProjectId = this.CurrUser.LoginProjectId;
                //工程名称--PMIProjectName
                //newPMI_TrustManage.PMIProjectName = this.txtPMIProjectName.Text.Trim();
                ////单位工程名称--PMIUnitWorksName
                //newPMI_TrustManage.PMIUnitWorksName = this.txtPMIUnitWorksName.Text.Trim();
                //编号--SerialNum
                newPMI_TrustManage.SerialNum = this.txtSerialNum.Text.Trim();
                //检测方法--TestMethod
                newPMI_TrustManage.TestMethod = this.ddlTestMethod.SelectedText.Trim();
                //检测标准--TestStandard
                newPMI_TrustManage.TestStandard = this.txtTestStandard.Text.Trim();
                //检测比例--TesTratio
                newPMI_TrustManage.TesTratio = this.ddlTesTratio.SelectedText.Trim();
                //合格要求--QualifiedRequire
                newPMI_TrustManage.QualifiedRequire = this.txtQualifiedRequire.Text.Trim();
                //委托编号--EntrustmentNo
                newPMI_TrustManage.EntrustmentNo = this.txtEntrustmentNo.Text.Trim();
                //装置
                newPMI_TrustManage.UnitCode = this.drpInstallation.SelectedValue.Trim();
                //工区
                newPMI_TrustManage.AreaIdCode = this.drpWorkAreaId.SelectedValue.Trim();
                if (!string.IsNullOrEmpty(this.PMITrustID))
                {
                    newPMI_TrustManage.PMITrustId = this.PMITrustID;
                    BLL.PMITrustManageService.UpdateCH_Trust(newPMI_TrustManage);
                    BLL.LogService.AddSys_Log(this.CurrUser, newPMI_TrustManage.PMITrustId, this.PMITrustID, BLL.Const.HJGL_PMITrustManageMenuId, "修改PMI委托信息");
                }
                else
                {
                    if (BLL.PMITrustManageService.IsExistTrustCode(this.CurrUser.LoginProjectId, this.txtEntrustmentNo.Text.Trim()))
                    {
                        ShowNotify("此委托单号已经存在!", MessageBoxIcon.Warning);
                        return;
                    }
                    this.PMITrustID = SQLHelper.GetNewID(typeof(Model.PMI_TrustManage));
                    newPMI_TrustManage.PMITrustId = this.PMITrustID;
                    BLL.PMITrustManageService.AddCH_Trust(newPMI_TrustManage);
                    BLL.LogService.AddSys_Log(this.CurrUser, newPMI_TrustManage.PMITrustId, this.PMITrustID, BLL.Const.HJGL_PMITrustManageMenuId, "添加无损委托信息");
                }
                List GetTrustItem = this.CollectGridJointInfo();
                List TrustItem = new List();
                foreach (var item in GetTrustItem)
                {
                    Model.PMI_TrustMangeItem newitem = new Model.PMI_TrustMangeItem();
                    newitem.PMITrustID = this.PMITrustID;
                    newitem.PMITrustItemID = item.PMITrustItemID;
                    newitem.MaterialId = item.MaterialId;
                    newitem.PMITrustID = this.PMITrustID;
                    newitem.PipelineNum = item.PipelineNum;
                    newitem.PipelineName = item.PipelineName;
                    newitem.QualityNum = item.QualityNum;
                    newitem.LotNumber = item.LotNumber;
                    newitem.PipeIineSpecs = item.PipeIineSpecs;
                    newitem.PipelineMaterial = item.PipelineMaterial;
                    newitem.HotStatus = item.HotStatus;
                    newitem.AcceptanceCriteria = item.AcceptanceCriteria;
                    newitem.SamplingNum = item.SamplingNum;
                    newitem.RemarkNum = item.RemarkNum;
                    TrustItem.Add(newitem);
                }
                BLL.PMITrustManageItemService.upDataCH_TrustItem(this.PMITrustID, TrustItem);
                ShowNotify("提交成功!", MessageBoxIcon.Success);
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
            else
            {
                ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                return;
            }
        }
        #endregion
        #region 收集Grid页面信息
        /// 
        /// 收集Grid页面信息
        /// 
        /// 
        private List CollectGridJointInfo()
        {
            List GetTrustItem = null;
            List getNewTrustItem = new List();
            List getNewPMI_MaterialTemplate = new List();
            if (!string.IsNullOrEmpty(this.hdItemsString.Text))
            {
                getNewPMI_MaterialTemplate = BLL.PMI_MaterialTemplateService.GetPMI_MaterialTemplateAddItem(this.hdItemsString.Text.Trim());
                if (getNewPMI_MaterialTemplate != null || getNewPMI_MaterialTemplate.Count != 0)
                {
                    foreach (var item in getNewPMI_MaterialTemplate)
                    {
                        PMI_TrustMangeItem newItem = new PMI_TrustMangeItem();
                        newItem.PMITrustItemID = SQLHelper.GetNewID(typeof(Model.PMI_TrustMangeItem));
                        newItem.MaterialId = item.MaterialId;
                        newItem.PMITrustID = this.PMITrustID;
                        newItem.PipelineNum = item.Pipeline;
                        newItem.PipelineName = item.IdentName;
                        newItem.QualityNum = item.QuantityFileNum;
                        newItem.LotNumber = item.BatchNumber;
                        newItem.PipeIineSpecs = item.Size1;
                        newItem.PipelineMaterial = item.Material;
                        newItem.HotStatus = item.HotState;
                        newItem.AcceptanceCriteria = item.AcceptanceCriteria;
                        newItem.SamplingNum = Convert.ToString(item.Quantity);
                        newItem.RemarkNum = item.QuantitySum;
                        getNewTrustItem.Add(newItem);
                    }
                }
            }
            else if (string.IsNullOrEmpty(this.hdItemsString.Text) && this.PMITrustID != null)
            {
                GetTrustItem = BLL.PMITrustManageItemService.GetView_PMI_TrustItemByCH_TrustID(this.PMITrustID);
            }
            JArray mergedData = Grid1.GetMergedData();
            foreach (JObject mergedRow in mergedData)
            {
                string status = mergedRow.Value("status");
                JObject values = mergedRow.Value("values");
                string rowID = values.Value("PMITrustItemID").ToString();
                if (GetTrustItem != null)
                {
                    var item = GetTrustItem.FirstOrDefault(x => x.PMITrustItemID == rowID);
                    if (item != null)
                    {
                        getNewTrustItem.Add(item);
                    }
                }
            }
            return getNewTrustItem;
        }
        #endregion
        #region Grid 关闭弹出窗口事件
        /// 
        /// 关闭弹出窗口
        /// 
        /// 
        /// 
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            List GetTrustItem = new List();
            List getNewPMI_MaterialTemplate = BLL.PMI_MaterialTemplateService.GetPMI_MaterialTemplateAddItem(this.hdItemsString.Text.Trim());
            if (getNewPMI_MaterialTemplate != null || getNewPMI_MaterialTemplate.Count != 0)
            {
                foreach (var item in getNewPMI_MaterialTemplate)
                {
                    PMI_TrustMangeItem newItem = new PMI_TrustMangeItem();
                    newItem.PMITrustItemID = SQLHelper.GetNewID(typeof(Model.PMI_TrustMangeItem));
                    newItem.MaterialId = item.MaterialId;
                    newItem.PMITrustID = this.PMITrustID;
                    newItem.PipelineNum = item.Pipeline;
                    newItem.PipelineName = item.IdentName;
                    newItem.QualityNum = item.QuantityFileNum;
                    newItem.LotNumber = item.BatchNumber;
                    newItem.PipeIineSpecs = item.Size1;
                    newItem.PipelineMaterial = item.Material;
                    newItem.HotStatus = item.HotState;
                    newItem.AcceptanceCriteria = item.AcceptanceCriteria;
                    newItem.SamplingNum = Convert.ToString(item.Quantity);
                    newItem.RemarkNum = item.QuantitySum;
                    GetTrustItem.Add(newItem);
                }
            }
            this.BindGrid(GetTrustItem);
            //this.hdItemsString.Text = string.Empty;
        }
        #endregion
        #region 右键删除事件
        /// 
        /// 右键删除事件
        /// 
        /// 
        /// 
        protected void btnMenuDelete_Click(object sender, EventArgs e)
        {
            if (Grid1.SelectedRowIndexArray.Length > 0)
            {
                List GetTrustItem = this.CollectGridJointInfo();
                foreach (int rowIndex in Grid1.SelectedRowIndexArray)
                {
                    System.Web.UI.WebControls.HiddenField lblMaterialId = (System.Web.UI.WebControls.HiddenField)(Grid1.Rows[rowIndex].FindControl("hidMaterialId"));
                    string[] jot = lblMaterialId.Value.Split(',');
                    string jotId = jot[0];
                    var item = GetTrustItem.FirstOrDefault(x => x.MaterialId == jotId);
                    GetTrustItem.Remove(item);
                }
                BindGrid(GetTrustItem);
                ShowNotify("操作完成!", MessageBoxIcon.Success);
            }
        }
        #endregion
        #region 查找
        /// 
        /// 查找未焊接焊口
        /// 
        /// 
        /// 
        protected void ckSelect_Click(object sender, EventArgs e)
        {
            string MaterialId = string.Empty;
            for (int i = 0; i < Grid1.Rows.Count; i++)
            {
                System.Web.UI.WebControls.HiddenField lblMaterialId = (System.Web.UI.WebControls.HiddenField)(Grid1.Rows[i].FindControl("hidMaterialId"));
                string[] jot = lblMaterialId.Value.Split(',');
                string jotId = jot[0];
                //string jotId = Grid1.DataKeys[i][0].ToString();
                MaterialId += jotId + "|";
            }
            if (MaterialId != string.Empty)
            {
                MaterialId = MaterialId.Substring(0, MaterialId.Length - 1);
            }
            if (!string.IsNullOrEmpty(this.drpWorkAreaId.SelectedValue) && this.drpWorkAreaId.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpInstallation.SelectedValue) && this.drpInstallation.SelectedValue != BLL.Const._Null)
            {
                string strList = this.drpWorkAreaId.SelectedValue + "|" + this.PMITrustID + "|" + this.drpInstallation.SelectedValue;
                string window = String.Format("PMIShowTrustSearch.aspx?strList={0}&MaterialId={1}", strList, MaterialId, "编辑 - ");
                PageContext.RegisterStartupScript(Window1.GetSaveStateReference(hdItemsString.ClientID) + Window1.GetShowReference(window));
            }
            else
            {
                Alert.ShowInTop("请选择装置和工区!", MessageBoxIcon.Warning);
            }
        }
        #endregion
        protected void drpInstallation_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.drpWorkAreaId.Items.Clear();
            if (this.drpInstallation.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpInstallation.SelectedValue))
            {
                BLL.WorkAreaService.InitWorkAreaByProjectIdAndInstallationId(this.drpWorkAreaId, this.CurrUser.LoginProjectId, this.drpInstallation.SelectedValue, true);
            }
            else
            {
                Funs.FineUIPleaseSelect(this.drpWorkAreaId);
            }
            this.drpWorkAreaId.SelectedIndex = 0;
            //if (this.drpInstallation.SelectedValue == BLL.Const._Null || string.IsNullOrEmpty(this.drpInstallation.SelectedValue))
            //{
            //    Funs.FineUIPleaseSelect(this.drpInstallation);
            //}
            //else
            //{
            //    var ddlunitString = this.drpInstallation.SelectedValue;
            //    this.drpCH_TrustUnit.DataValueField = "UnitId";
            //    this.drpCH_TrustUnit.DataTextField = "UnitName";
            //    this.drpCH_TrustUnit.DataSource = (from x in Funs.DB.PIM_View_PmiMaterialTemplate where x.ProjectId == this.CurrUser.LoginProjectId && x.InstallationId == ddlunitString select new { x.UnitId, x.UnitName }).Distinct().ToList();
            //    this.drpCH_TrustUnit.DataBind();
            //}
        }
    }
}