using BLL;
using System;
namespace FineUIPro.Web.CQMS.ProcessControl
{
    public partial class NondestructiveTestEdit : PageBase
    {
        #region 定义变量
        /// <summary>
        /// 主键
        /// </summary>
        private string NondestructiveTestId
        {
            get
            {
                return (string)ViewState["NondestructiveTestId"];
            }
            set
            {
                ViewState["NondestructiveTestId"] = value;
            }
        }
        #endregion

        #region 加载
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetButtonPower();
                BLL.UnitWorkService.InitUnitWorkDownList(drpUnitWorkId, this.CurrUser.LoginProjectId, true);
                BLL.UnitService.InitUnitByProjectIdUnitTypeDropDownList(drpUnitId, this.CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_2, true);
                BLL.InspectionAreaService.InitInspectionAreaDownList(drpInspectionAreaId, true);
                BLL.SteelService.InitSteel(drpSteel, true);

                this.NondestructiveTestId = Request.Params["NondestructiveTestId"];
                if (!string.IsNullOrEmpty(this.NondestructiveTestId))
                {
                    Model.ProcessControl_NondestructiveTest nondestructiveTest = BLL.NondestructiveTestService.GetNondestructiveTestById(this.NondestructiveTestId);
                    if (nondestructiveTest != null)
                    {
                        hdAttachUrl.Text = this.NondestructiveTestId;
                        if (!string.IsNullOrEmpty(nondestructiveTest.UnitWorkId))
                        {
                            this.drpUnitWorkId.SelectedValue = nondestructiveTest.UnitWorkId;
                        }
                        if (!string.IsNullOrEmpty(nondestructiveTest.UnitId))
                        {
                            this.drpUnitId.SelectedValue = nondestructiveTest.UnitId;
                            InspectionPersonService.InitInspectionPerson(drpWelderName, this.CurrUser.LoginProjectId, nondestructiveTest.UnitId, true);
                            if (!string.IsNullOrEmpty(nondestructiveTest.WED_ID))
                            {
                                this.drpWelderName.SelectedValue = nondestructiveTest.WED_ID;
                            }
                        }
                        this.txtWelderCode.Text = nondestructiveTest.WelderCode;
                        if (!string.IsNullOrEmpty(nondestructiveTest.STE_ID))
                        {
                            this.drpSteel.SelectedValue = nondestructiveTest.STE_ID;
                        }
                        if (!string.IsNullOrEmpty(nondestructiveTest.InspectionAreaId))
                        {
                            this.drpInspectionAreaId.SelectedValue = nondestructiveTest.InspectionAreaId;
                        }
                        this.txtNondestructiveTestCode.Text = nondestructiveTest.NondestructiveTestCode;
                        if (nondestructiveTest.PieceCount != null)
                        {
                            this.txtPieceCount.Text = Convert.ToString(nondestructiveTest.PieceCount);
                        }
                        if (nondestructiveTest.OnceQualifiedCount != null)
                        {
                            this.txtOnceQualifiedCount.Text = Convert.ToString(nondestructiveTest.OnceQualifiedCount);
                        }
                        if (nondestructiveTest.NondestructiveTestDate != null)
                        {
                            this.txtNondestructiveTestDate.Text = string.Format("{0:yyyy-MM-dd}", nondestructiveTest.NondestructiveTestDate);
                        }
                    }
                }
                else
                {
                    this.txtNondestructiveTestDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                }
            }
        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.drpUnitId.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择施工单位!", MessageBoxIcon.Warning);
                return;
            }
            Model.ProcessControl_NondestructiveTest nondestructiveTest = new Model.ProcessControl_NondestructiveTest();
            nondestructiveTest.ProjectId = this.CurrUser.LoginProjectId;
            if (this.drpUnitWorkId.SelectedValue != BLL.Const._Null)
            {
                nondestructiveTest.UnitWorkId = this.drpUnitWorkId.SelectedValue;
            }
            if (this.drpUnitId.SelectedValue != BLL.Const._Null)
            {
                nondestructiveTest.UnitId = this.drpUnitId.SelectedValue;
            }
            nondestructiveTest.WelderCode = this.txtWelderCode.Text.Trim();
            nondestructiveTest.NondestructiveTestCode = this.txtNondestructiveTestCode.Text.Trim();
            nondestructiveTest.PieceCount = Funs.GetNewInt(this.txtPieceCount.Text.Trim());
            nondestructiveTest.OnceQualifiedCount = Funs.GetNewInt(this.txtOnceQualifiedCount.Text.Trim());
            nondestructiveTest.NondestructiveTestDate = Funs.GetNewDateTime(this.txtNondestructiveTestDate.Text);
            if (this.drpWelderName.SelectedValue != BLL.Const._Null)
            {
                nondestructiveTest.WED_ID = this.drpWelderName.SelectedValue;
            }
            if (this.drpSteel.SelectedValue != BLL.Const._Null)
            {
                nondestructiveTest.STE_ID = this.drpSteel.SelectedValue;
            }
            if (this.drpInspectionAreaId.SelectedValue != BLL.Const._Null)
            {
                nondestructiveTest.InspectionAreaId = this.drpInspectionAreaId.SelectedValue;
            }
            if (string.IsNullOrEmpty(this.NondestructiveTestId))
            {
                nondestructiveTest.CompileMan = this.CurrUser.UserId;
                if (!string.IsNullOrEmpty(hdAttachUrl.Text))
                {
                    nondestructiveTest.NondestructiveTestId = hdAttachUrl.Text;
                }
                else
                {
                    nondestructiveTest.NondestructiveTestId = SQLHelper.GetNewID(typeof(Model.Comprehensive_InspectionPerson));
                    hdAttachUrl.Text = nondestructiveTest.NondestructiveTestId;
                }
                BLL.NondestructiveTestService.AddNondestructiveTest(nondestructiveTest);
            }
            else
            {
                nondestructiveTest.NondestructiveTestId = this.NondestructiveTestId;
                BLL.NondestructiveTestService.UpdateNondestructiveTest(nondestructiveTest);
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

        #region 焊工选择事件
        /// <summary>
        /// 焊工的选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void drpWelderName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpWelderName.SelectedValue != BLL.Const._Null)
            {
                Model.Comprehensive_InspectionPerson person = BLL.InspectionPersonService.GetInspectionPersonById(this.drpWelderName.SelectedValue);
                if (person != null)
                {
                    this.txtWelderCode.Text = person.CertificateNumber;
                }
            }
            else
            {
                this.txtWelderCode.Text = string.Empty;
            }
        }
        #endregion

        #region 施工单位的选择事件
        /// <summary>
        /// 施工单位的选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void drpUnitId_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpUnitId.SelectedValue != BLL.Const._Null)
            {
                this.drpWelderName.Items.Clear();
                InspectionPersonService.InitInspectionPerson(drpWelderName, this.CurrUser.LoginProjectId, drpUnitId.SelectedValue, true);
                this.drpWelderName.SelectedIndex = 0;
                this.txtWelderCode.Text = string.Empty;
            }
            else
            {
                this.drpWelderName.Items.Clear();
                Funs.FineUIPleaseSelect(this.drpWelderName);
                this.txtWelderCode.Text = string.Empty;
            }
        }
        #endregion

        #region 附件上传
        protected void btnAttach_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdAttachUrl.Text))   //新增记录
            {
                this.hdAttachUrl.Text = SQLHelper.GetNewID(typeof(Model.ProcessControl_NondestructiveTest));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/CQMS/NondestructiveTest&menuId={1}", this.hdAttachUrl.Text, BLL.Const.NondestructiveTestMenuId)));
        }
        #endregion

        #region 获取按钮权限
        /// <summary>
        /// 获取按钮权限
        /// </summary>
        /// <param name="button"></param>
        /// <returns></returns>
        private void GetButtonPower()
        {
            if (Request.Params["value"] == "0")
            {
                return;
            }
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.NondestructiveTestMenuId);
            if (buttonList.Count > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion
    }
}