using BLL;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
namespace FineUIPro.Web.CQMS.Comprehensive
{
    public partial class PersonItem : PageBase
    {
        #region 主键
        /// 
        /// 人员id
        /// 
        public string InspectionPersonId
        {
            get
            {
                return (string)ViewState["InspectionPersonId"];
            }
            set
            {
                ViewState["InspectionPersonId"] = value;
            }
        }
        #endregion
        private static List steelItemLists = null;
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.InspectionPersonId = Request.Params["InspectionPersonId"];
                BindGrid1();
                BindGrid2();
            }
        }
        /// 
        /// 材质数据绑定
        /// 
        private void BindGrid1()
        {
            steelItemLists = new List();
            List lists = new List();
            lists = BLL.SteelService.GetAllSteelList();
            foreach (var item in lists)
            {
                var personSteel = BLL.PersonSteelService.getInspectionPersonSteelBySteelId(item.STE_ID, this.InspectionPersonId);
                if (personSteel!=null)
                {
                    Model.SteelItem newSteel = new Model.SteelItem();
                    newSteel.STE_ID = item.STE_ID;
                    newSteel.STE_Code = item.STE_Code;
                    newSteel.STE_Name = item.STE_Name;
                    newSteel.STE_SteelType = item.STE_SteelType;
                    newSteel.STE_Remark = item.STE_Remark;
                    newSteel.ThicknessMin = personSteel.ThicknessMin;
                    newSteel.ThicknessMax = personSteel.ThicknessMax;
                    newSteel.SizesMin = personSteel.SizesMin;
                    newSteel.SizesMax = personSteel.SizesMax;
                    steelItemLists.Add(newSteel);
                }
                else
                {
                    Model.SteelItem newSteel = new Model.SteelItem();
                    newSteel.STE_ID = item.STE_ID;
                    newSteel.STE_Code = item.STE_Code;
                    newSteel.STE_Name = item.STE_Name;
                    newSteel.STE_SteelType = item.STE_SteelType;
                    newSteel.STE_Remark = item.STE_Remark;
                    steelItemLists.Add(newSteel);
                }
            }
            this.Grid1.DataSource = steelItemLists;
            this.Grid1.DataBind();
            //选中保存项
            string steIds = string.Empty;
            for (int i = 0; i < steelItemLists.Count; i++)
            {
                string id = this.Grid1.DataKeys[i][0].ToString();
                var ins = BLL.PersonSteelService.getInspectionPersonSteelBySteelId(id, InspectionPersonId);
                if (ins != null)
                {
                    steIds += ins.SteelId + ",";
                }
                if (!string.IsNullOrEmpty(steIds))
                {
                    this.Grid1.SelectedRowIDArray = steIds.Split(',').ToArray();
                }
            }
        }
        /// 
        /// 焊接方法绑定数据
        /// 
        private void BindGrid2()
        {
            string strSql = @"SELECT WME_ID,WME_Code,WME_Name,WME_Remark FROM BS_WeldMethod order by WME_Code";
            List listStr = new List();
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            var table = this.GetPagedDataTable(Grid2, tb);
            Grid2.DataSource = table;
            Grid2.DataBind();
            //选中保存项
            string weldMethodIds = string.Empty;
            for (int i = 0; i < table.Rows.Count; i++)
            {
                string id = this.Grid2.DataKeys[i][0].ToString();
                var ins = BLL.PersonWeldMethodService.getInspectionPersonWeldMethodByWeldMethodId(id, InspectionPersonId);
                if (ins != null)
                {
                    weldMethodIds += ins.WeldMethodId + ",";
                }
                if (!string.IsNullOrEmpty(weldMethodIds))
                {
                    this.Grid2.SelectedRowIDArray = weldMethodIds.Split(',').ToArray();
                }
            }
        }
        #endregion
        #region 保存
        /// 
        /// 保存
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            BLL.PersonSteelService.DelPersonItem(this.InspectionPersonId);
            BLL.PersonWeldMethodService.DelInspectionPersonWeldMethod(this.InspectionPersonId);
            if (this.Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("没有选中任何材质操作项!", MessageBoxIcon.Warning);
                return;
            }
            if (this.Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("没有选中任何焊接方法操作项!", MessageBoxIcon.Warning);
                return;
            }
            //保存材质
            JArray mergedData = Grid1.GetMergedData();
            int[] steelLists = this.Grid1.SelectedRowIndexArray;
            foreach (JObject mergedRow in mergedData)
            {
                int rowIndex = mergedRow.Value("index");
                if (steelLists.Contains(rowIndex))
                {
                    JObject values = mergedRow.Value("values");
                    string steId = values.Value("STE_ID").ToString();
                    string txtThicknessMin = values.Value("ThicknessMin").ToString();
                    string txtThicknessMax = values.Value("ThicknessMax").ToString();
                    string txtSizesMin = values.Value("SizesMin").ToString();
                    string txtSizesMax = values.Value("SizesMax").ToString();
                    Model.Comprehensive_InspectionPersonSteel InspectionPersonItem = new Model.Comprehensive_InspectionPersonSteel();
                    InspectionPersonItem.InspectionPersonSteelId = SQLHelper.GetNewID(typeof(Model.Comprehensive_InspectionPersonSteel));
                    InspectionPersonItem.InspectionPersonId = this.InspectionPersonId;
                    InspectionPersonItem.SteelId = steId;
                    InspectionPersonItem.ThicknessMin = Funs.GetNewDecimal(txtThicknessMin.Trim());
                    InspectionPersonItem.ThicknessMax = Funs.GetNewDecimal(txtThicknessMax.Trim());
                    InspectionPersonItem.SizesMin = Funs.GetNewDecimal(txtSizesMin.Trim());
                    InspectionPersonItem.SizesMax = Funs.GetNewDecimal(txtSizesMax.Trim());
                    InspectionPersonItem.IsChecked = true;
                    BLL.PersonSteelService.AddDInspectionPersonItem(InspectionPersonItem);
                }
            }
            //保存焊接方法
            int weldMethodRowCount = Grid2.SelectedRowIndexArray.Length;
            for (int i = 0; i < weldMethodRowCount; i++)
            {
                string wmeId = Grid2.SelectedRowIDArray[i];
                Model.Comprehensive_InspectionPersonWeldMethod InspectionPersonWeldMethod = new Model.Comprehensive_InspectionPersonWeldMethod();
                InspectionPersonWeldMethod.InspectionPersonWeldMethodId = SQLHelper.GetNewID(typeof(Model.Comprehensive_InspectionPersonWeldMethod));
                InspectionPersonWeldMethod.InspectionPersonId = this.InspectionPersonId;
                InspectionPersonWeldMethod.WeldMethodId = wmeId;
                InspectionPersonWeldMethod.IsChecked = true;
                BLL.PersonWeldMethodService.AddInspectionPersonWeldMethod(InspectionPersonWeldMethod);
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}