using BLL;
using System;
using System.Linq;
namespace FineUIPro.Web.CQMS.ProcessControl
{
    public partial class NondestructiveTest_NewEdit : PageBase
    {
        #region 定义变量
        /// 
        /// 主键
        /// 
        private string Id
        {
            get
            {
                return (string)ViewState["Id"];
            }
            set
            {
                ViewState["Id"] = value;
            }
        }
        #endregion
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetButtonPower();
                BLL.UnitService.InitUnitByProjectIdUnitTypeDropDownList(drpUnitId, this.CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_2, true);
                this.Id = Request.Params["Id"];
                if (!string.IsNullOrEmpty(this.Id))
                {
                    var nondestructiveTest = BLL.NondestructiveTestNewService.Detail(this.Id);
                    if (nondestructiveTest != null)
                    {
                        hdAttachUrl.Text = this.Id;
                      
                        if (!string.IsNullOrEmpty(nondestructiveTest.UnitId))
                        {
                            this.drpUnitId.SelectedValue = nondestructiveTest.UnitId;
                        }
                        
                        if (!string.IsNullOrEmpty(nondestructiveTest.ProfessionalName))
                        {
                            this.drpProfessionalName.SelectedValue = nondestructiveTest.ProfessionalName;
                        }
                        
                        this.txtMonthQuantity.Text = nondestructiveTest.MonthQuantity.ToString();
                        this.txtTotalQuantity.Text = nondestructiveTest.TotalQuantity.ToString();
                        this.txtMonthRate.Text = nondestructiveTest.MonthRate.ToString();
                        this.txtTotalRate.Text = nondestructiveTest.TotalRate.ToString();
                    }
                }
            }
        }
        #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.NondestructiveTestNewMenuId)));
        }
        #endregion
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            if (Request.Params["value"] == "0")
            {
                return;
            }
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.NondestructiveTestNewMenuId);
            if (buttonList.Count > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.drpUnitId.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择施工单位!", MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrEmpty(this.drpProfessionalName.SelectedValue))
            {
                Alert.ShowInTop("请选择专业!", MessageBoxIcon.Warning);
                return;
            }
            var model = new Model.ProcessControl_NondestructiveTest_New();
            model.ProjectId = this.CurrUser.LoginProjectId;
           
            if (this.drpUnitId.SelectedValue != BLL.Const._Null)
            {
                model.UnitId = this.drpUnitId.SelectedValue;
            }
            model.ProfessionalName = drpProfessionalName.SelectedValue;
            model.CreateDate = DateTime.Now;
            model.CreateMan = CurrUser.UserId;
            model.MonthQuantity = Convert.ToInt32(txtMonthQuantity.Text.Trim());
            model.TotalQuantity = Convert.ToInt32(txtTotalQuantity.Text.Trim());
            model.MonthRate = txtMonthRate.Text.Trim();
            model.TotalRate = txtTotalRate.Text.Trim();
            
            if (string.IsNullOrEmpty(this.Id))
            {
               
                if (!string.IsNullOrEmpty(hdAttachUrl.Text))
                {
                    model.Id = hdAttachUrl.Text;
                }
                else
                {
                    model.Id = SQLHelper.GetNewID(typeof(Model.Comprehensive_InspectionPerson));
                    hdAttachUrl.Text = model.Id;
                }
                BLL.NondestructiveTestNewService.Insert(model);
            }
            else
            {
                model.Id = this.Id;
                BLL.NondestructiveTestNewService.Update(model);
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}