using BLL;
using System;
namespace FineUIPro.Web.HJGL.PersonManage
{
    public partial class TestApplicationEdit : PageBase
    {
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                BLL.UnitService.InitUnitByProjectIdUnitTypeDropDownList(this.drpUnitId, this.CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_2, true);//施工单位
                BLL.UnitService.InitUnitByProjectIdUnitTypeDropDownList(this.drpCheckUnit, this.CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_5, true);//检测单位
                this.drpWeldingMethod.DataTextField = "WeldingMethodCode";
                this.drpWeldingMethod.DataValueField = "WeldingMethodId";
                this.drpWeldingMethod.DataSource = BLL.WeldMethodItemService.GetWeldMethodList();
                this.drpWeldingMethod.DataBind();
                Funs.FineUIPleaseSelect(this.drpWelderCondition);
                Funs.FineUIPleaseSelect(this.drpConUnitWeldingEngineer);
                Funs.FineUIPleaseSelect(this.drpArea);
                string applicationTestId = Request.Params["applicationTestId"];
                if (!string.IsNullOrEmpty(applicationTestId))
                {
                    this.hdId.Text = applicationTestId;
                    Model.Welder_TestApplication testApplication = BLL.TestApplicationService.GetTestApplicationById(this.hdId.Text);
                    if (testApplication != null)
                    {
                        if (!string.IsNullOrEmpty(testApplication.ConUnit))
                        {
                            this.drpUnitId.SelectedValue = testApplication.ConUnit;
                            this.drpConUnitWeldingEngineer.Items.Clear();
                            this.drpArea.Items.Clear();
                            BLL.UserService.InitUserByUnitIdAndRoleIdDropDownList(this.drpConUnitWeldingEngineer, this.CurrUser.LoginProjectId, this.drpUnitId.SelectedValue, BLL.Const.SubWeldingEngineer, true);
                            BLL.WorkAreaService.InitWorkAreaProjectUnitDropDownList(this.drpArea, this.CurrUser.LoginProjectId, this.drpUnitId.SelectedValue, true);
                        }
                        if (!string.IsNullOrEmpty(testApplication.ConUnitWeldingEngineer))
                        {
                            this.drpConUnitWeldingEngineer.SelectedValue = testApplication.ConUnitWeldingEngineer;
                        }
                        this.txtCertificateValidity.Text = testApplication.CertificateValidity.HasValue ? string.Format("{0:yyyy-MM-dd}", testApplication.CertificateValidity) : "";
                        this.txtPreTestDate.Text = testApplication.PreTestDate.HasValue ? string.Format("{0:yyyy-MM-dd}", testApplication.PreTestDate) : "";
                        if (!string.IsNullOrEmpty(testApplication.WelderClass))
                        {
                            this.drpWelderClass.SelectedValue = testApplication.WelderClass;
                            if (this.drpWelderClass.SelectedValue == "土建")
                            {
                                this.drpConUnitWeldingEngineer.Items.Clear();
                                BLL.UserService.InitUserByUnitIdAndRoleIdDropDownList(this.drpConUnitWeldingEngineer, this.CurrUser.LoginProjectId, this.drpUnitId.SelectedValue, BLL.Const.CVWeldingManager, true);
                                this.drpConUnitWeldingEngineer.SelectedIndex = 0;
                            }
                        }
                        this.txtWelderCode.Text = testApplication.WelderCode;
                        this.txtWelderName.Text = testApplication.WelderName;
                        this.txtIdentityCard.Text = testApplication.IdentityCard;
                        this.txtWeldingPosition.Text = testApplication.WeldingPosition;
                        if (!string.IsNullOrEmpty(testApplication.WeldingMethod))
                        {
                            this.drpWeldingMethod.SelectedValue = testApplication.WeldingMethod;
                        }
                        if (!string.IsNullOrEmpty(testApplication.SpecimenSize))
                        {
                            this.drpSpecimenSize.SelectedValue = testApplication.SpecimenSize;
                        }
                        this.drpMaterialCategory.SelectedValue = testApplication.MaterialCategory;
                        this.drpWelderCondition.SelectedValue = testApplication.WelderCondition;
                        this.txtAppearanceEvaluation.Text = testApplication.AppearanceEvaluation;
                        this.txtFirstExamination.Text = testApplication.FirstExamination.HasValue ? testApplication.FirstExamination.ToString() : "";
                        this.txtSecondExamination.Text = testApplication.SecondExamination.HasValue ? testApplication.SecondExamination.ToString() : "";
                        this.txtExamDate.Text = testApplication.ExamDate.HasValue ? string.Format("{0:yyyy-MM-dd}", testApplication.ExamDate) : "";
                        this.txtInvigilatorConfirmed.Text = testApplication.InvigilatorConfirmed;
                        if (!string.IsNullOrEmpty(testApplication.Area))
                        {
                            this.drpArea.SelectedValue = testApplication.Area;
                        }
                        this.txtExamTrustDate.Text = testApplication.ExamTrustDate.HasValue ? string.Format("{0:yyyy-MM-dd}", testApplication.ExamTrustDate) : "";
                        this.txtExamTrustCode.Text = testApplication.ExamTrustCode;
                        this.txtCheckDate.Text = testApplication.CheckDate.HasValue ? string.Format("{0:yyyy-MM-dd}", testApplication.CheckDate) : "";
                        this.txtCheckResult.Text = testApplication.CheckResult;
                        this.txtNDTFilm.Text = testApplication.NDTFilm.HasValue ? testApplication.NDTFilm.ToString() : "";
                        if (!string.IsNullOrEmpty(testApplication.CheckUnit))
                        {
                            this.drpCheckUnit.SelectedValue = testApplication.CheckUnit;
                        }
                        this.txtOwnerNum.Text = testApplication.OwnerNum;
                        this.txtAdditionalTestCategories1.Text = testApplication.AdditionalTestCategories1;
                        this.txtAdditionalTestCategories2.Text = testApplication.AdditionalTestCategories2;
                        this.txtAdditionalTestScore1.Text = testApplication.AdditionalTestScore1.HasValue ? testApplication.AdditionalTestScore1.ToString() : "";
                        this.txtAdditionalTestScore2.Text = testApplication.AdditionalTestScore2.HasValue ? testApplication.AdditionalTestScore2.ToString() : "";
                    }
                }
            }
        }
        #endregion
        #region 附件上传
        /// 
        /// 上传身份证
        /// 
        /// 
        /// 
        protected void btnAttachUrl_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdId.Text))
            {
                this.hdId.Text = SQLHelper.GetNewID(typeof(Model.Welder_TestApplication));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("~/AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/HJGL/PersonManage/TestApplication/&menuId={1}&strParam=1", this.hdId.Text, BLL.Const.HJGL_TestApplicationMenuId)));
        }
        /// 
        /// 上传IC卡
        /// 
        /// 
        /// 
        protected void btnAttachUrl2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdId.Text))
            {
                this.hdId.Text = SQLHelper.GetNewID(typeof(Model.Welder_TestApplication));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("~/AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/HJGL/PersonManage/TestApplication/&menuId={1}&strParam=2", this.hdId.Text, BLL.Const.HJGL_TestApplicationMenuId)));
        }
        /// 
        /// 上传焊工证
        /// 
        /// 
        /// 
        protected void btnAttachUrl3_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdId.Text))
            {
                this.hdId.Text = SQLHelper.GetNewID(typeof(Model.Welder_TestApplication));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("~/AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/HJGL/PersonManage/TestApplication/&menuId={1}&strParam=3", this.hdId.Text, BLL.Const.HJGL_TestApplicationMenuId)));
        }
        /// 
        /// 上传网络查询证明
        /// 
        /// 
        /// 
        protected void btnAttachUrl4_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdId.Text))
            {
                this.hdId.Text = SQLHelper.GetNewID(typeof(Model.Welder_TestApplication));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("~/AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/HJGL/PersonManage/TestApplication/&menuId={1}&strParam=4", this.hdId.Text, BLL.Const.HJGL_TestApplicationMenuId)));
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.drpUnitId.SelectedValue == BLL.Const._Null || string.IsNullOrEmpty(this.drpUnitId.SelectedValue))
            {
                Alert.ShowInTop("请选择施工单位", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpConUnitWeldingEngineer.SelectedValue == BLL.Const._Null || string.IsNullOrEmpty(this.drpConUnitWeldingEngineer.SelectedValue))
            {
                Alert.ShowInTop("请选择施工单位焊接工程师", MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrEmpty(this.drpWelderClass.SelectedValue))
            {
                Alert.ShowInTop("请选择焊工类别", MessageBoxIcon.Warning);
                return;
            }
            if (!IDCardValid.CheckIDCard(this.txtIdentityCard.Text.Trim()))
            {
                Alert.ShowInTop("该身份证号非法", MessageBoxIcon.Warning);
                return;
            }
            string applicationTestId = Request.Params["applicationTestId"];
            Model.Welder_TestApplication testApplication = new Model.Welder_TestApplication();
            if (this.drpUnitId.SelectedValue != BLL.Const._Null)
            {
                testApplication.ConUnit = this.drpUnitId.SelectedValue;
            }
            if (this.drpConUnitWeldingEngineer.SelectedValue != BLL.Const._Null)
            {
                testApplication.ConUnitWeldingEngineer = this.drpConUnitWeldingEngineer.SelectedValue;
            }
            testApplication.CertificateValidity = Funs.GetNewDateTime(this.txtCertificateValidity.Text);
            testApplication.PreTestDate = Funs.GetNewDateTime(this.txtPreTestDate.Text);
            testApplication.WelderClass = this.drpWelderClass.SelectedValue;
            testApplication.WelderCode = this.txtWelderCode.Text.Trim();
            testApplication.WelderName = this.txtWelderName.Text.Trim();
            testApplication.IdentityCard = this.txtIdentityCard.Text.Trim();
            testApplication.WeldingPosition = this.txtWeldingPosition.Text.Trim();
            if (this.drpWeldingMethod.SelectedValue != BLL.Const._Null)
            {
                testApplication.WeldingMethod = this.drpWeldingMethod.SelectedValue;
            }
            if (!string.IsNullOrEmpty(this.drpSpecimenSize.SelectedValue))
            {
                testApplication.SpecimenSize = this.drpSpecimenSize.SelectedValue;
            }
            if (!string.IsNullOrEmpty(this.drpMaterialCategory.SelectedValue))
            {
                testApplication.MaterialCategory = this.drpMaterialCategory.SelectedValue;
            }
            testApplication.WelderCondition = this.drpWelderCondition.SelectedValue;
            testApplication.AppearanceEvaluation = this.txtAppearanceEvaluation.Text.Trim();
            testApplication.FirstExamination = Funs.GetNewInt(this.txtFirstExamination.Text.Trim());
            testApplication.SecondExamination = Funs.GetNewInt(this.txtSecondExamination.Text.Trim());
            testApplication.ExamDate = Funs.GetNewDateTime(this.txtExamDate.Text);
            testApplication.InvigilatorConfirmed = this.txtInvigilatorConfirmed.Text.Trim();
            if (!string.IsNullOrEmpty(this.drpArea.SelectedValue) && this.drpArea.SelectedValue != BLL.Const._Null)
            {
                testApplication.Area = this.drpArea.SelectedValue;
            }
            testApplication.ExamTrustDate = Funs.GetNewDateTime(this.txtExamTrustDate.Text);
            testApplication.ExamTrustCode = this.txtExamTrustCode.Text.Trim();
            testApplication.CheckDate = Funs.GetNewDateTime(this.txtCheckDate.Text);
            testApplication.CheckResult = this.txtCheckResult.Text.Trim();
            testApplication.NDTFilm = Funs.GetNewInt(this.txtNDTFilm.Text.Trim());
            if (this.drpCheckUnit.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpCheckUnit.SelectedValue))
            {
                testApplication.CheckUnit = this.drpCheckUnit.SelectedValue;
            }
            testApplication.OwnerNum = this.txtOwnerNum.Text.Trim();
            testApplication.AdditionalTestCategories1 = this.txtAdditionalTestCategories1.Text.Trim();
            testApplication.AdditionalTestCategories2 = this.txtAdditionalTestCategories2.Text.Trim();
            testApplication.AdditionalTestScore1 = Funs.GetNewDecimal(this.txtAdditionalTestScore1.Text);
            testApplication.AdditionalTestScore2 = Funs.GetNewDecimal(this.txtAdditionalTestScore2.Text);
            if (!string.IsNullOrEmpty(applicationTestId))
            {
                testApplication.ApplicationTestId = applicationTestId;
                BLL.TestApplicationService.UpdateTestApplication(testApplication);
            }
            else
            {
                testApplication.ProjectId = this.CurrUser.LoginProjectId;
                testApplication.ApplicationTestId = SQLHelper.GetNewID(typeof(Model.Welder_TestApplication));
                BLL.TestApplicationService.AddTestApplication(testApplication);
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
        #region DropDownList下拉选择事件
        /// 
        /// 单位下拉选择事件
        /// 
        /// 
        /// 
        protected void drpUnitId_SelectedIndexChanged(object sender, EventArgs e)
        {
            string trustCode = string.Empty;
            string unitCode = string.Empty;
            this.drpConUnitWeldingEngineer.Items.Clear();
            this.drpArea.Items.Clear();
            if (this.drpUnitId.SelectedValue != BLL.Const._Null)
            {
                if (this.drpWelderClass.SelectedValue == "土建")
                {
                    BLL.UserService.InitUserByUnitIdAndRoleIdDropDownList(this.drpConUnitWeldingEngineer, this.CurrUser.LoginProjectId, this.drpUnitId.SelectedValue, BLL.Const.CVWeldingManager, true);
                }
                else
                {
                    BLL.UserService.InitUserByUnitIdAndRoleIdDropDownList(this.drpConUnitWeldingEngineer, this.CurrUser.LoginProjectId, this.drpUnitId.SelectedValue, BLL.Const.SubWeldingEngineer, true);
                }
                BLL.WorkAreaService.InitWorkAreaProjectUnitDropDownList(this.drpArea, this.CurrUser.LoginProjectId, this.drpUnitId.SelectedValue, true);
                var unit = BLL.UnitService.GetUnitByUnitId(this.drpUnitId.SelectedValue);
                if (unit != null)
                {
                    unitCode = unit.UnitCode;
                }
            }
            else
            {
                Funs.FineUIPleaseSelect(this.drpConUnitWeldingEngineer);
                Funs.FineUIPleaseSelect(this.drpArea);
            }
            this.drpConUnitWeldingEngineer.SelectedIndex = 0;
            this.drpArea.SelectedIndex = 0;
            var welder = BLL.TestApplicationService.GetTestApplicationByIdentityCard(this.CurrUser.LoginProjectId, this.txtIdentityCard.Text.Trim());
            if (welder == null)
            {
                this.txtExamTrustCode.Text = string.Empty;
                string prefix = "HGKS-" + this.txtOwnerNum.Text.Trim() + "-" + unitCode + "-";
                this.txtExamTrustCode.Text = BLL.SQLHelper.RunProcNewId("SpGetNewCode3ByProjectPrefix", "dbo.Welder_TestApplication", "ExamTrustCode", this.CurrUser.LoginProjectId, prefix);
            }
        }
        /// 
        /// 焊工类别下拉选择事件
        /// 
        /// 
        /// 
        protected void drpWelderClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.drpWelderClass.SelectedValue))
            {
                string welderClass = this.drpWelderClass.SelectedValue;
                this.drpConUnitWeldingEngineer.Items.Clear();
                if (this.drpUnitId.SelectedValue != BLL.Const._Null)
                {
                    if (welderClass == "土建")
                    {
                        BLL.UserService.InitUserByUnitIdAndRoleIdDropDownList(this.drpConUnitWeldingEngineer, this.CurrUser.LoginProjectId, this.drpUnitId.SelectedValue, BLL.Const.CVWeldingManager, true);
                    }
                    else
                    {
                        BLL.UserService.InitUserByUnitIdAndRoleIdDropDownList(this.drpConUnitWeldingEngineer, this.CurrUser.LoginProjectId, this.drpUnitId.SelectedValue, BLL.Const.SubWeldingEngineer, true);
                    }
                    this.drpConUnitWeldingEngineer.SelectedIndex = 0;
                }
                string code = string.Empty;
                if (welderClass == "土建")
                {
                    code = "TJ";
                }
                else if (welderClass == "结构")
                {
                    code = "JG";
                }
                else if (welderClass == "设备")
                {
                    code = "FE";
                }
                else if (welderClass == "管道")
                {
                    code = "PD";
                }
                string prefix = code + "W";
                this.txtWelderCode.Text = BLL.SQLHelper.RunProcNewId("SpGetNewCode3ByProjectPrefix", "dbo.Welder_TestApplication", "WelderCode", this.CurrUser.LoginProjectId, prefix);
            }
            else
            {
                this.txtWelderCode.Text = string.Empty;
            }
        }
        /// 
        /// 根据身份证号获取焊工信息
        /// 
        /// 
        /// 
        protected void txtIdentityCard_TextChanged(object sender, EventArgs e)
        {
            this.txtExamTrustCode.Text = string.Empty;
            this.txtExamTrustDate.Text = string.Empty;
            this.txtCheckDate.Text = string.Empty;
            this.txtCheckResult.Text = string.Empty;
            this.txtNDTFilm.Text = string.Empty;
            this.drpCheckUnit.SelectedIndex = 0;
            if (!string.IsNullOrEmpty(this.txtIdentityCard.Text.Trim()))
            {
                var welder = BLL.TestApplicationService.GetTestApplicationByIdentityCard(this.CurrUser.LoginProjectId, this.txtIdentityCard.Text.Trim());
                if (welder != null)
                {
                    this.txtExamTrustCode.Text = welder.ExamTrustCode;
                    this.txtExamTrustDate.Text = string.Format("{0:yyyy-MM-dd}", welder.ExamTrustDate);
                    this.txtCheckDate.Text = string.Format("{0:yyyy-MM-dd}", welder.CheckDate);
                    this.txtCheckResult.Text = welder.CheckResult;
                    this.txtNDTFilm.Text = welder.NDTFilm.HasValue ? welder.NDTFilm.ToString() : "";
                    if (!string.IsNullOrEmpty(welder.CheckResult))
                    {
                        this.drpCheckUnit.SelectedValue = welder.CheckUnit;
                    }
                }
                else
                {
                    if (this.drpUnitId.SelectedValue != BLL.Const._Null)
                    {
                        var unit = BLL.UnitService.GetUnitByUnitId(this.drpUnitId.SelectedValue);
                        if (unit != null)
                        {
                            string prefix = "HGKS-" + this.txtOwnerNum.Text.Trim() + "-" + unit.UnitCode + "-";
                            this.txtExamTrustCode.Text = BLL.SQLHelper.RunProcNewId("SpGetNewCode3ByProjectPrefix", "dbo.Welder_TestApplication", "ExamTrustCode", this.CurrUser.LoginProjectId, prefix);
                        }
                    }
                }
            }
        }
        #endregion
    }
}