using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using BLL;
using Newtonsoft.Json.Linq;
namespace FineUIPro.Web.HJGL.MaterialManage
{
    public partial class ElectrodeBakeRecordEditNew : PageBase
    {
        #region 定义项
        /// 
        /// 主键
        /// 
        public string ElectrodeID
        {
            get
            {
                return (string)ViewState["ElectrodeID"];
            }
            set
            {
                ViewState["ElectrodeID"] = value;
            }
        }
        public string UnitId
        {
            get
            {
                return (string)ViewState["UnitId"];
            }
            set
            {
                ViewState["UnitId"] = value;
            }
        }
        private bool AppendToEnd = false;
        #endregion
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ElectrodeID = Request.Params["ElectrodeID"];
                string perfix = string.Empty;
                perfix = "HK-";
                this.txtBatchCode.Text = BLL.SQLHelper.RunProcNewId("SpGetNewCode5ByProjectId", "dbo.HJGL_ElectrodeBake", "BatchCode", this.CurrUser.LoginProjectId, perfix);
                var units = UnitService.GetUnitByProjectIdUnitTypeList(this.CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_2);
                this.dpUnit.DataTextField = "UnitName";
                this.dpUnit.DataValueField = "UnitId";
                this.dpUnit.DataSource = units;
                this.dpUnit.DataBind();
                Funs.FineUIPleaseSelect(dpUnit);
                ///焊条 
                this.drpWME_ID.DataTextField = "ConsumablesName";
                this.drpWME_ID.DataValueField = "ConsumablesId";
                var totalWeldMaterials = from x in Funs.DB.Base_Consumables select x;
                this.drpWME_ID.DataSource = totalWeldMaterials;
                this.drpWME_ID.DataBind();
                string strSql = @"SELECT  EMaterialRegist.[EMaterialRegistId]
                                  ,EMaterialRegist.[EMaterialRegistCode]
                                  ,EMaterialRegist.[EMaterialRegistDate]
                                  ,EMaterialRegist.[DeliveryMan]
                                  ,EMaterialRegist.[UnitName]
                                  ,EMaterialRegist.[CompileMan]
                                  ,EMaterialRegist.[CompileDate]
                                  ,EMaterialRegist.[ProjectId]
                                  ,EMaterialRegist.[SystemCode]
                                  ,EMaterialRegist.[PartACode]
                                  ,EMaterialRegist.[Condition]
                                  ,EMaterialRegist.[Name]
                                  ,EMaterialRegist.[SpecificationsModel]
                                  ,EMaterialRegist.[Standard]
                                  ,EMaterialRegist.[Material]
                                  ,EMaterialRegist.[SpecialInstructions]
                                  ,EMaterialRegist.[Unit]
                                  ,EMaterialRegist.[Amount]
                                  ,EMaterialRegist.[FourLocation]
                                  ,EMaterialRegist.[Manufacturer]
                                  ,EMaterialRegist.[DesignUnit]
                                  ,EMaterialRegist.[RealArriveAmount]
                                  ,EMaterialRegist.[OutProve]
                                  ,EMaterialRegist.[OutPrices]
                                  ,CompileManUser.UserName as CompileManName
                                  ,EMaterialRegist.[Supply], Consumables.ConsumablesName "
               + @" FROM HJGL_EMaterialRegist AS EMaterialRegist"
               + @" LEFT JOIN Sys_User AS CompileManUser ON EMaterialRegist.CompileMan=CompileManUser.UserId"
               + @" LEFT JOIN Base_Consumables AS Consumables ON Consumables.ConsumablesId=EMaterialRegist.WMT_ID"
               + @" LEFT JOIN Base_Project AS Project ON EMaterialRegist.ProjectId=Project.ProjectId"
               + @" WHERE EMaterialRegist.ProjectId= @ProjectId ";
                List listStr = new List();
                listStr.Add(new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId));
                //if (!string.IsNullOrEmpty(this.txtEMaterialRegistCode.Text.Trim()))
                //{
                //    strSql += " AND EMaterialRegist.EMaterialRegistCode LIKE @EMaterialRegistCode";
                //    listStr.Add(new SqlParameter("@EMaterialRegistCode", "%" + this.txtEMaterialRegistCode.Text.Trim() + "%"));
                //}
                strSql += " order by EMaterialRegistDate desc ";
                SqlParameter[] parameter = listStr.ToArray();
                DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
                this.gvRegist.DataSource = tb;
                this.gvRegist.DataBind();
                if (!string.IsNullOrEmpty(this.ElectrodeID))
                {
                    var electrode = BLL.HJGL_ElectrodeBakeService.GetElecrodeBakeByElectrodeId(this.ElectrodeID);
                    if (electrode != null)
                    {
                        if (!string.IsNullOrEmpty(electrode.Specifications))
                        {
                            this.dpSpecificationsModel.Values = electrode.Specifications.Split(',');
                        }
                        ;
                        if (!string.IsNullOrEmpty(electrode.UnitId))
                        {
                            this.dpUnit.SelectedValue = electrode.UnitId;
                        }
                        var records = Funs.DB.HJGL_EMInventoryRecords.Where(x => x.WMT_ID == electrode.WMT_ID && x.UnitId == electrode.UnitId && x.ProjectId == electrode.ProjectId).FirstOrDefault();
                        if (records != null && records.MaterialCount.HasValue)
                        {
                            this.txtReElectrodeCount.Text = records.MaterialCount.Value.ToString("0.##");
                        }
                        //txtCardCode.Text = electrode.CardCode;
                        this.drpWME_ID.SelectedValue = electrode.WMT_ID;
                        txtBatchCode.Text = electrode.BatchCode;
                        if (!string.IsNullOrEmpty(electrode.InLibCode))
                        {
                            var eMaterialRegist = BLL.HJGL_EMaterialRegistService.GetEMaterialRegistByID(electrode.InLibCode);
                            if (!string.IsNullOrEmpty(eMaterialRegist.SpecificationsModel))
                            {
                                var items = eMaterialRegist.SpecificationsModel.Split(',');
                                List res = new List();
                                foreach (string item in items)
                                {
                                    res.Add(new ListItem(item, item));
                                }
                                this.cbJOTSch.DataTextField = "Text";
                                this.cbJOTSch.DataValueField = "Value";
                                this.cbJOTSch.DataSource = res;
                                this.cbJOTSch.DataBind();
                            }
                        }
                        txtInLibCode.Value = electrode.InLibCode;
                        //   txtSpecifications.Text = electrode.Specifications;
                        if (electrode.ElectrodeCount.HasValue)
                            txtElectrodeCount.Text = electrode.ElectrodeCount.Value.ToString();
                        if (electrode.OvenElectricHours.HasValue && electrode.OvenElectricMinute.HasValue)
                            tpOvenElectricTime.Text = electrode.OvenElectricHours.Value.ToString("D2") + ":" + electrode.OvenElectricMinute.Value.ToString("D2");
                        if (electrode.OvenElectricTemperature.HasValue)
                            txtOvenElectricTemperature.Text = electrode.OvenElectricTemperature.Value.ToString();
                        if (electrode.ConstantTemperature.HasValue)
                            txtConstantTemperature.Text = electrode.ConstantTemperature.Value.ToString();
                        if (electrode.ConstantStartHours.HasValue && electrode.ConstantStartMinute.HasValue)
                            tpConstantStartTime.Text = electrode.ConstantStartHours.Value.ToString("D2") + ":" + electrode.ConstantStartMinute.Value.ToString("D2");
                        if (electrode.ConstantEndHours.HasValue && electrode.ConstantEndMinute.HasValue)
                            tpConstantEndTime.Text = electrode.ConstantEndHours.Value.ToString("D2") + ":" + electrode.ConstantEndMinute.Value.ToString("D2");
                        if (electrode.MoveInBoxHours.HasValue && electrode.MoveInBoxMinute.HasValue)
                            tpMoveInBoxHours.Text = electrode.MoveInBoxHours.Value.ToString("D2") + ":" + electrode.MoveInBoxMinute.Value.ToString("D2");
                        if (electrode.MoveInTemperature.HasValue)
                            txtMoveInTemperature.Text = electrode.MoveInTemperature.Value.ToString();
                        txtBakeHead.Text = electrode.BakeHead;
                    }
                }
                else
                {
                }
                // 删除选中单元格的客户端脚本
            }
        }
        #endregion
        protected void txtInLibCode_OnTextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtInLibCode.Value))
            {
                var regist = Funs.DB.HJGL_EMaterialRegist.Where(x => x.EMaterialRegistId == this.txtInLibCode.Value).FirstOrDefault();
                if (regist != null)
                {
                    if (!string.IsNullOrEmpty(regist.SpecificationsModel))
                    {
                        var items = regist.SpecificationsModel.Split(',');
                        List res = new List();
                        foreach (string item in items)
                        {
                            res.Add(new ListItem(item, item));
                        }
                        this.cbJOTSch.DataTextField = "Text";
                        this.cbJOTSch.DataValueField = "Value";
                        this.cbJOTSch.DataSource = res;
                        this.cbJOTSch.DataBind();
                    }
                    this.drpWME_ID.SelectedValue = regist.WMT_ID;
                    this.dpUnit.SelectedValue = regist.Unit;
                    var records = Funs.DB.HJGL_EMInventoryRecords.Where(x => x.WMT_ID == regist.WMT_ID && x.UnitId == regist.Unit && x.ProjectId == regist.ProjectId).FirstOrDefault();
                    if (records.MaterialCount.HasValue)
                    {
                        this.txtReElectrodeCount.Text = records.MaterialCount.Value.ToString("0.##");
                    }
                }
            }
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_ElectrodeBakeMenuId, Const.BtnSave))
            {
                ShowNotify("您没有这个权限,请与管理员联系!");
                return;
            }
            Model.HJGL_ElectrodeBake newElectrodeBake = new Model.HJGL_ElectrodeBake();
            newElectrodeBake.CompileDate = DateTime.Now;
            newElectrodeBake.CompileMan = this.CurrUser.UserId;
            newElectrodeBake.UnitId = this.dpUnit.SelectedValue;
            
            if (this.dpSpecificationsModel.Values != null && this.dpSpecificationsModel.Values.Length > 0)
            {
                newElectrodeBake.Specifications = string.Join(",", this.dpSpecificationsModel.Values);
            }
            //newElectrodeBake.CardCode = txtCardCode.Text.Trim();
            newElectrodeBake.WMT_ID = this.drpWME_ID.SelectedValue;
            newElectrodeBake.BatchCode = txtBatchCode.Text.Trim();
            newElectrodeBake.InLibCode = txtInLibCode.Value;
            //newElectrodeBake.Specifications = txtSpecifications.Text.Trim();
            newElectrodeBake.ElectrodeCount = Funs.GetNewDouble(txtElectrodeCount.Text.Trim());
            newElectrodeBake.OvenElectricHours = Funs.GetNewInt(tpOvenElectricTime.Text.Split(':')[0]);
            newElectrodeBake.OvenElectricMinute = Funs.GetNewInt(tpOvenElectricTime.Text.Split(':')[1]);
            newElectrodeBake.OvenElectricTemperature = Funs.GetNewInt(txtOvenElectricTemperature.Text.Trim());
            newElectrodeBake.ConstantTemperature = Funs.GetNewInt(txtConstantTemperature.Text.Trim());
            newElectrodeBake.ConstantStartHours = Funs.GetNewInt(tpConstantStartTime.Text.Split(':')[0]);  
            newElectrodeBake.ConstantStartMinute = Funs.GetNewInt(tpConstantStartTime.Text.Split(':')[1]); 
            newElectrodeBake.ConstantEndHours = Funs.GetNewInt(tpConstantEndTime.Text.Split(':')[0]);
            newElectrodeBake.ConstantEndMinute = Funs.GetNewInt(tpConstantEndTime.Text.Split(':')[1]);
            newElectrodeBake.MoveInBoxHours = Funs.GetNewInt(tpMoveInBoxHours.Text.Split(':')[0]);
            newElectrodeBake.MoveInBoxMinute = Funs.GetNewInt(tpMoveInBoxHours.Text.Split(':')[1]);
            newElectrodeBake.MoveInTemperature = Funs.GetNewInt(txtMoveInTemperature.Text.Trim());
            newElectrodeBake.BakeHead =  txtBakeHead.Text.Trim();
            newElectrodeBake.ProjectId = this.CurrUser.LoginProjectId;
            if (!string.IsNullOrEmpty(ElectrodeID))
            {
                newElectrodeBake.ElectrodeID = ElectrodeID;
                var oldEelectrode = BLL.HJGL_ElectrodeBakeService.GetElecrodeBakeByElectrodeId(this.ElectrodeID);
                BLL.HJGL_EMInventoryRecordsService.UpdateEMInventoryRecords(this.CurrUser.LoginProjectId, oldEelectrode.UnitId, oldEelectrode.WMT_ID, "", "", decimal.Parse(oldEelectrode.ElectrodeCount.Value.ToString("0.##")));
                BLL.HJGL_ElectrodeBakeService.UpdateElectrodeBake(newElectrodeBake); //修改焊丝烘烤记录
                BLL.HJGL_EMInventoryRecordsService.UpdateEMInventoryRecords(this.CurrUser.LoginProjectId, newElectrodeBake.UnitId, newElectrodeBake.WMT_ID, "", "", -decimal.Parse(newElectrodeBake.ElectrodeCount.Value.ToString("0.##")));
                // BLL.HJGL_ElectrodeBakeService.DeleteElectrodeBakeItem(this.ElectrodeID);
                //BLL.Sys_LogService.AddLog(BLL.Const.System_7, this.CurrUser.LoginProjectId, this.CurrUser.UserId, "修改焊丝烘烤记录!");
            }
            else
            {
                BLL.HJGL_EMInventoryRecordsService.UpdateEMInventoryRecords(this.CurrUser.LoginProjectId, newElectrodeBake.UnitId, newElectrodeBake.WMT_ID, "", "", -decimal.Parse(newElectrodeBake.ElectrodeCount.Value.ToString("0.##")));
                newElectrodeBake.ElectrodeID = SQLHelper.GetNewID(typeof(Model.HJGL_ElectrodeBake));
                BLL.HJGL_ElectrodeBakeService.AddElectrodeBake(newElectrodeBake); //添加焊丝烘烤记录主表信息  
                // BLL.Sys_LogService.AddLog(BLL.Const.System_7, this.CurrUser.LoginProjectId, this.CurrUser.UserId, "添加焊丝烘烤记录!");
            }
           // Alert.ShowInTop("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }
    }
    
}