using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FineUIPro.Web.HJGL.WeldingManage
{
    public partial class PCFJointEdit : PageBase
    {
        public static Dictionary<string, PW_JointInfo> dic = new Dictionary<string, PW_JointInfo>();

        #region 定义项
        /// <summary>
        /// 管线主键
        /// </summary>
        private string ISO_ID
        {
            get
            {
                return (string)ViewState["ISO_ID"];
            }
            set
            {
                ViewState["ISO_ID"] = value;
            }
        }
        /// <summary>
        /// 区域信息主键
        /// </summary>
        public string JOT_ID
        {
            get
            {
                return (string)ViewState["JOT_ID"];
            }
            set
            {
                ViewState["JOT_ID"] = value;
            }
        }

       
        #endregion

        #region 加载
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();

                #region 初始化下拉框
                BLL.Base_MaterialService.InitMaterialDropDownList(this.ddlSTE1, true, this.CurrUser.LoginProjectId);//材质1
                BLL.Base_MaterialService.InitMaterialDropDownList(this.ddlSTE2, true, this.CurrUser.LoginProjectId);//材质2
                BLL.Base_ComponentsService.InitComponentsDropDownList(this.ddlComponent1, true);//组件1
                BLL.Base_ComponentsService.InitComponentsDropDownList(this.ddlComponent2, true);//组件2
                BLL.Base_WeldTypeService.InitWeldTypeDropDownList(this.ddlJOTY_ID, true);//焊缝类型
                BLL.Base_GrooveTypeService.InitGrooveTypeDropDownList(this.ddlJST_ID, true); //坡口类型
                BLL.Base_TemperatureSetService.InitTemperatureDropDownList(this.drpCellTemp, "2", true);//层间温度
                BLL.Base_TemperatureSetService.InitTemperatureDropDownList(this.drpPrepareTemp, "1", true);//预热温度
                BLL.Base_WeldingMethodService.InitWeldingMethodDropDownList(this.ddlWME_ID, true);//焊接方法
                BLL.Base_ConsumablesService.InitConsumablesDropDownList(this.ddlWeldSilk, true, "1");//焊丝
                BLL.Base_ConsumablesService.InitConsumablesDropDownList(this.ddlWeldMat, true, "2");//焊条
 
                //焊口状态
                this.ddlJointStatus.DataTextField = "Text";
                this.ddlJointStatus.DataValueField = "Value";
                this.ddlJointStatus.DataSource = BLL.DropListService.HJGL_JointStatus();
                this.ddlJointStatus.DataBind();
                 
                #endregion
                BLL.Base_MaterialTypeService.InitMaterialDropDownList(this.dpMaterialType, true);//材质类型
                BLL.Base_DetectionRateService.InitDetectionRateDropDownList(this.drpNDTRate, true);//探伤比例
                BLL.Base_DetectionTypeService.InitDetectionTypeDropDownList(this.drpNDTName, true);//探伤类型
                this.ISO_ID = Request.Params["ISO_ID"];
                this.JOT_ID = Request.Params["JOT_ID"];
                if (!string.IsNullOrEmpty(this.JOT_ID))
                {
                    Model.PW_JointInfo jointInfo = dic[this.JOT_ID];
                    if (jointInfo != null)
                    {
                        this.ISO_ID = jointInfo.ISO_ID;
                        ////初始化页面信息
                        this.LoadPageInfo(jointInfo);
                    }
                }
                 
                 
                 
            }
        }
        #endregion

        #region 文本框是否可编辑
        /// <summary>
        /// 文本框是否可编辑
        /// </summary>
        /// <param name="readOnly"></param>
        private void TextIsReadOnly(bool readOnly)
        {
            this.txtJointNo.Enabled = !readOnly;
            this.ddlWLO_CODE.Enabled = !readOnly;
            this.txtBelongPipe.Enabled = !readOnly;
            if (this.CurrUser.Account != Const.sysglyId)
            {
                this.txtSize.Enabled = !readOnly; //兰化项目寸径存在空的情况,需要修改为可修改。
            }
            this.ddlJointAttribute.Enabled = !readOnly;
        }
        #endregion

        #region 初始化页面信息
        /// <summary>
        /// 初始化页面信息
        /// </summary>
        /// <param name="jointInfo"></param>
        private void LoadPageInfo(PW_JointInfo jointInfo)
        {
            if (jointInfo != null)
            {
                this.txtJointNo.Text = jointInfo.JOT_JointNo;
                this.txtWallBoard.Text = jointInfo.WallBoard;
                if (!string.IsNullOrEmpty(jointInfo.WLO_Code))
                {
                    this.ddlWLO_CODE.SelectedValue = jointInfo.WLO_Code;
                }
                if (!string.IsNullOrEmpty(jointInfo.MaterialId))
                {
                    this.ddlSTE1.SelectedValue = jointInfo.MaterialId;
                }
                if (!string.IsNullOrEmpty(jointInfo.MaterialId2))
                {
                    this.ddlSTE2.SelectedValue = jointInfo.MaterialId2;
                }
                this.txtBelongPipe.Text = jointInfo.JOT_BelongPipe;
                if (!string.IsNullOrEmpty(jointInfo.JOT_Component1))
                {
                    this.ddlComponent1.SelectedValue = jointInfo.JOT_Component1;
                }
                if (!string.IsNullOrEmpty(jointInfo.JOT_Component2))
                {
                    this.ddlComponent2.SelectedValue = jointInfo.JOT_Component2;
                }
                this.txtJointDesc.Text = jointInfo.JOT_JointDesc;
                this.txtHeartNo1.Text = jointInfo.JOT_HeartNo1;
                this.txtHeartNo2.Text = jointInfo.JOT_HeartNo2;
                if (!string.IsNullOrEmpty(jointInfo.DetectionRateId))
                {
                    this.drpNDTRate.SelectedValue = jointInfo.DetectionRateId;
                }
                if (!string.IsNullOrEmpty(jointInfo.DetectionTypeId))
                {
                    this.drpNDTName.SelectedValue = jointInfo.DetectionTypeId;
                }
                if (!string.IsNullOrEmpty(jointInfo.JOTY_ID))
                {
                    this.ddlJOTY_ID.SelectedValue = jointInfo.JOTY_ID;
                }
                this.txtSize.Text = jointInfo.JOT_Size.HasValue ? jointInfo.JOT_Size.ToString() : "";
                this.txtDia.Text = jointInfo.JOT_Dia.HasValue ? jointInfo.JOT_Dia.ToString() : "";
                if (!string.IsNullOrEmpty(jointInfo.JST_ID))
                {
                    this.ddlJST_ID.SelectedValue = jointInfo.JST_ID;
                }
                this.txtExtendLength.Text = jointInfo.Extend_Length;
                this.txtSch.Text = jointInfo.JOT_Sch;
                this.txtFactSch.Text = jointInfo.JOT_FactSch.HasValue ? jointInfo.JOT_FactSch.ToString() : "";
                this.txtLastTemp.Text = jointInfo.JOT_LastTemp.HasValue ? jointInfo.JOT_LastTemp.ToString() : "";
                if (jointInfo.JOT_CellTemp.HasValue)
                {
                    var tem = BLL.Base_TemperatureSetService.GetTemperatureByTemAndType(jointInfo.JOT_CellTemp, "2");
                    if (tem != null)
                    {
                        this.drpCellTemp.SelectedValue = tem.TemperatureSetId;
                    }
                }
                if (jointInfo.JOT_PrepareTemp.HasValue)
                {
                    var tem = BLL.Base_TemperatureSetService.GetTemperatureByTemAndType(jointInfo.JOT_PrepareTemp, "1");
                    if (tem != null)
                    {
                        this.drpPrepareTemp.SelectedValue = tem.TemperatureSetId;
                    }
                }
                if (!string.IsNullOrEmpty(jointInfo.JOT_JointAttribute))
                {
                    this.ddlJointAttribute.SelectedValue = jointInfo.JOT_JointAttribute;
                }
                if (!string.IsNullOrEmpty(jointInfo.WME_ID))
                {
                    this.ddlWME_ID.SelectedValue = jointInfo.WME_ID;
                }
                if (!string.IsNullOrEmpty(jointInfo.JOT_WeldSilk))
                {
                    this.ddlWeldSilk.SelectedValue = jointInfo.JOT_WeldSilk;
                }
                if (!string.IsNullOrEmpty(jointInfo.JOT_WeldMat))
                {
                    this.ddlWeldMat.SelectedValue = jointInfo.JOT_WeldMat;
                }
                this.txtElectricity.Text = jointInfo.JOT_Electricity;
                this.txtVoltage.Text = jointInfo.JOT_Voltage;
                if (!string.IsNullOrEmpty(jointInfo.IS_Proess))
                {
                    this.drpIS_Proess.SelectedValue = jointInfo.IS_Proess;
                }
                this.txtRemark.Text = jointInfo.JOT_Remark;

                 
                 
                if (!String.IsNullOrEmpty(jointInfo.JOT_CellWelder))
                {
                    var cellWelder = BLL.WelderService.GetWelderById(jointInfo.JOT_CellWelder);
                   
                }
                   
                if (!string.IsNullOrEmpty(jointInfo.JOT_JointStatus))
                {
                    this.ddlJointStatus.SelectedValue = jointInfo.JOT_JointStatus;
                }
                this.txtWeldingGasProtection.Text = jointInfo.WeldingGasProtection;
                this.txtWeldingSpeed.Text = jointInfo.WeldingSpeed;
               
                this.txtComponentNum.Text = jointInfo.ComponentNum;
                if (!string.IsNullOrEmpty(jointInfo.MaterialType))
                {
                    this.ddlJointStatus.SelectedValue = jointInfo.MaterialType;
                }
                 
            }
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
        
            Model.PW_JointInfo jointInfo =  dic[this.JOT_ID];
            jointInfo.ProjectId = this.CurrUser.LoginProjectId;
            jointInfo.JOT_JointNo = this.txtJointNo.Text.Trim();
            jointInfo.ISO_ID = this.ISO_ID;
            jointInfo.WallBoard = this.txtWallBoard.Text.Trim();
            if (this.drpNDTName.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpNDTName.SelectedValue))
            {
                jointInfo.DetectionTypeId = this.drpNDTName.SelectedValue;
            }
            if (this.drpNDTRate.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpNDTRate.SelectedValue))
            {
                jointInfo.DetectionRateId = this.drpNDTRate.SelectedValue;
            }
             
            if (this.ddlSTE1.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.ddlSTE1.SelectedValue))
            {
                jointInfo.MaterialId = this.ddlSTE1.SelectedValue;
            }
            if (this.ddlSTE2.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.ddlSTE2.SelectedValue))
            {
                jointInfo.MaterialId2 = this.ddlSTE2.SelectedValue;
            }
            jointInfo.WLO_Code = this.ddlWLO_CODE.SelectedValue;
            if (this.ddlComponent1.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.ddlComponent1.SelectedValue))
            {
                jointInfo.JOT_Component1 = this.ddlComponent1.SelectedValue;
            }
            if (this.ddlComponent2.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.ddlComponent2.SelectedValue))
            {
                jointInfo.JOT_Component2 = this.ddlComponent2.SelectedValue;
            }
            jointInfo.JOT_HeartNo1 = this.txtHeartNo1.Text.Trim();
            jointInfo.JOT_HeartNo2 = this.txtHeartNo2.Text.Trim();
            if (this.ddlWeldMat.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.ddlWeldMat.SelectedValue))
            {
                jointInfo.JOT_WeldMat = this.ddlWeldMat.SelectedValue;
            }
            
            jointInfo.JOT_Dia = Funs.GetNewDecimal(this.txtDia.Text.Trim());
            jointInfo.JOT_Size = Funs.GetNewDecimal(this.txtSize.Text.Trim());
            jointInfo.JOT_Sch = this.txtSch.Text.Trim();
            jointInfo.Extend_Length = this.txtExtendLength.Text.Trim();
            jointInfo.JOT_FactSch = Funs.GetNewDecimal(this.txtFactSch.Text.Trim());
            jointInfo.JOT_JointDesc = this.txtJointDesc.Text.Trim();
            if (this.ddlWeldSilk.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.ddlWeldSilk.SelectedValue))
            {
                jointInfo.JOT_WeldSilk = this.ddlWeldSilk.SelectedValue.ToString();
            }
            if (this.ddlJOTY_ID.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.ddlJOTY_ID.SelectedValue))
            {
                jointInfo.JOTY_ID = this.ddlJOTY_ID.SelectedValue.ToString();
            }
            jointInfo.JOT_RepairFlag = null;
            if (ddlWME_ID.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.ddlWME_ID.SelectedValue))
            {
                jointInfo.WME_ID = this.ddlWME_ID.SelectedValue;
            }
            if (ddlJST_ID.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.ddlJST_ID.SelectedValue))
            {
                jointInfo.JST_ID = this.ddlJST_ID.SelectedValue;
            }
            if (this.drpPrepareTemp.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpPrepareTemp.SelectedValue))
            {
                var tem = BLL.Base_TemperatureSetService.GetTemperatureSetById(this.drpPrepareTemp.SelectedValue);
                if (tem != null)
                {
                    jointInfo.JOT_PrepareTemp = tem.Temperature;
                }
            }
            if (this.drpCellTemp.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpCellTemp.SelectedValue))
            {
                var tem = BLL.Base_TemperatureSetService.GetTemperatureSetById(this.drpCellTemp.SelectedValue);
                if (tem != null)
                {
                    jointInfo.JOT_CellTemp = tem.Temperature;
                }
            }
            if (!string.IsNullOrEmpty(this.txtLastTemp.Text.Trim()))
            {
                jointInfo.JOT_LastTemp = Convert.ToDecimal(this.txtLastTemp.Text.Trim());
            }
            jointInfo.JOT_JointAttribute = this.ddlJointAttribute.SelectedValue;
            jointInfo.IS_Proess = this.drpIS_Proess.SelectedValue;
            jointInfo.JOT_Remark = this.txtRemark.Text.Trim();
            jointInfo.WeldingGasProtection = this.txtWeldingGasProtection.Text.Trim();
            jointInfo.WeldingSpeed = this.txtWeldingSpeed.Text.Trim();
            jointInfo.JOT_Electricity = this.txtElectricity.Text.Trim();
            jointInfo.JOT_Voltage = this.txtVoltage.Text.Trim();
            jointInfo.ComponentNum = this.txtComponentNum.Text.Trim();
            jointInfo.JOT_JointStatus = this.ddlJointStatus.SelectedValue;
              
            ShowNotify("保存成功!", MessageBoxIcon.Success);
 
            if (ckAll.Checked)
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(JOT_ID) + ActiveWindow.GetHidePostBackReference());
            }
            else
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference("") + ActiveWindow.GetHidePostBackReference());

            }

        }


        #endregion

        
    }
}