using BLL;
using System;
using System.Linq;

namespace FineUIPro.Web.SES
{
    public partial class OneTimeContractsEdit : PageBase
    {
        #region 加载
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                string view = Request.Params["view"];
                if (view == "1")
                {
                    this.btnSave.Hidden = true;
                }
                else
                {
                    GetButtonPower();//按钮权限
                }

                BLL.DisciplineService.InitDropDownList(this.drpDisciplineId, true);//专业
                BLL.Sys_UserService.InitUserDropDownList(this.drpContractAdmin, true);//合同员
                BLL.Sys_UserService.InitUserDropDownList(this.drpCostEngineer, true);//费用工程师
                BLL.Sys_UserService.InitUserDropDownList(this.drpBuyer, true);//采购员
                BLL.Sys_UserService.InitUserDropDownList(this.drpProjectManager, true);//项目经理
                BLL.Sys_UserService.InitUserDropDownList(this.drpConstructionManager, true);//施工经理
                BLL.Sys_UserService.InitUserDropDownList(this.drpConstructionEngineer, true);//施工工程师
                BLL.PriceSchemeService.InitDropDownList(this.drpPriceSchemeId, true);//价格方案
                BLL.ContractorService.InitDropDownList(this.drpContractorId, true);//承包商

                this.drpStatus.DataTextField = "ConstText";
                this.drpStatus.DataValueField = "ConstValue";
                this.drpStatus.DataSource= BLL.ConstValue.drpConstItemList(ConstValue.Group_Status);//状态
                this.drpStatus.DataBind();
                Funs.FineUIPleaseSelect(this.drpStatus);

                string oneTimeContractsId = Request.Params["oneTimeContractsId"];
                if (!string.IsNullOrEmpty(oneTimeContractsId))
                {
                    Model.EMC_OneTimeContracts oneTimeContracts = BLL.OneTimeContractsService.GetOneTimeContractsById(oneTimeContractsId);
                    if (oneTimeContracts != null)
                    {
                        this.txtItemNo.Text = oneTimeContracts.ItemNumber;
                        if (!string.IsNullOrEmpty(oneTimeContracts.DisciplineId))
                        {
                            this.drpDisciplineId.SelectedValue = oneTimeContracts.DisciplineId;
                        }
                        this.txtItemName.Text = oneTimeContracts.ItemName;
                        this.txtRegistrationDate.Text = oneTimeContracts.RegistrationDate != null ? string.Format("{0:yyyy-MM-dd}", oneTimeContracts.RegistrationDate) : "";
                        if (!string.IsNullOrEmpty(oneTimeContracts.ContractAdmin))
                        {
                            this.drpContractAdmin.SelectedValue = oneTimeContracts.ContractAdmin;
                        }
                        if (!string.IsNullOrEmpty(oneTimeContracts.CostEngineer))
                        {
                            this.drpCostEngineer.SelectedValue = oneTimeContracts.CostEngineer;
                        }
                        if (!string.IsNullOrEmpty(oneTimeContracts.Buyer))
                        {
                            this.drpBuyer.SelectedValue = oneTimeContracts.Buyer;
                        }
                        if (!string.IsNullOrEmpty(oneTimeContracts.ProjectManager))
                        {
                            this.drpProjectManager.SelectedValue = oneTimeContracts.ProjectManager;
                        }
                        if (!string.IsNullOrEmpty(oneTimeContracts.ConstructionManager))
                        {
                            this.drpConstructionManager.SelectedValue = oneTimeContracts.ConstructionManager;
                        }
                        if (!string.IsNullOrEmpty(oneTimeContracts.ConstructionEngineer))
                        {
                            this.drpConstructionEngineer.SelectedValue = oneTimeContracts.ConstructionEngineer;
                        }
                        this.txtTotalBudget.Text = oneTimeContracts.TotalBudget != null ? oneTimeContracts.TotalBudget.ToString() : "";
                        if (!string.IsNullOrEmpty(oneTimeContracts.PriceSchemeId))
                        {
                            this.drpPriceSchemeId.SelectedValue = oneTimeContracts.PriceSchemeId;
                        }
                        if (!string.IsNullOrEmpty(oneTimeContracts.ContractorId))
                        {
                            this.drpContractorId.SelectedValue = oneTimeContracts.ContractorId;
                        }
                        this.txtDesignUnits.Text = oneTimeContracts.DesignUnits;
                        if (!string.IsNullOrEmpty(oneTimeContracts.Progress))
                        {
                            this.drpStatus.SelectedValue = oneTimeContracts.Progress;
                        }                        
                        this.txtEndDate.Text = oneTimeContracts.EndDate != null ? string.Format("{0:yyyy-MM-dd}", oneTimeContracts.EndDate) : "";
                        this.txtRemark.Text = oneTimeContracts.Remark;
                    }
                }
                else
                {
                    this.txtRegistrationDate.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)
        {
            string oneTimeContractsId = Request.Params["oneTimeContractsId"];
            Model.EMC_OneTimeContracts oneTimeContracts = new Model.EMC_OneTimeContracts();
            oneTimeContracts.ItemNumber = this.txtItemNo.Text.Trim();
            if (this.drpDisciplineId.SelectedValue!=BLL.Const._Null&&!string.IsNullOrEmpty(this.drpDisciplineId.SelectedValue))
            {
                oneTimeContracts.DisciplineId = this.drpDisciplineId.SelectedValue;
            }
            oneTimeContracts.ItemName = this.txtItemName.Text.Trim();
            oneTimeContracts.RegistrationDate = Funs.GetNewDateTime(this.txtRegistrationDate.Text.Trim());
            if (this.drpContractAdmin.SelectedValue!=BLL.Const._Null&&!string.IsNullOrEmpty(this.drpContractAdmin.SelectedValue))
            {
                oneTimeContracts.ContractAdmin = this.drpContractAdmin.SelectedValue;
            }
            if (this.drpCostEngineer.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpCostEngineer.SelectedValue))
            {
                oneTimeContracts.CostEngineer = this.drpCostEngineer.SelectedValue;
            }
            if (this.drpBuyer.SelectedValue!=BLL.Const._Null&&!string.IsNullOrEmpty(this.drpBuyer.SelectedValue))
            {
                oneTimeContracts.Buyer = this.drpBuyer.SelectedValue;
            }
            if (this.drpProjectManager.SelectedValue!=BLL.Const._Null&&!string.IsNullOrEmpty(this.drpProjectManager.SelectedValue))
            {
                oneTimeContracts.ProjectManager = this.drpProjectManager.SelectedValue;
            }
            if (this.drpConstructionManager.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpConstructionManager.SelectedValue))
            {
                oneTimeContracts.ConstructionManager = this.drpConstructionManager.SelectedValue;
            }
            if (this.drpConstructionEngineer.SelectedValue!=BLL.Const._Null&&!string.IsNullOrEmpty(this.drpConstructionEngineer.SelectedValue))
            {
                oneTimeContracts.ConstructionEngineer = this.drpConstructionEngineer.SelectedValue;
            }
            oneTimeContracts.TotalBudget = Funs.GetNewDecimalOrZero(this.txtTotalBudget.Text.Trim());
            if (this.drpPriceSchemeId.SelectedValue!=BLL.Const._Null&&!string.IsNullOrEmpty(this.drpPriceSchemeId.SelectedValue))
            {
                oneTimeContracts.PriceSchemeId = this.drpPriceSchemeId.SelectedValue;
            }
            if (this.drpContractorId.SelectedValue!=BLL.Const._Null&&!string.IsNullOrEmpty(this.drpContractorId.SelectedValue))
            {
                oneTimeContracts.ContractorId = this.drpContractorId.SelectedValue;
            }
            oneTimeContracts.DesignUnits = this.txtDesignUnits.Text.Trim();
            if (this.drpStatus.SelectedValue!=BLL.Const._Null)
            {
                oneTimeContracts.Progress = this.drpStatus.SelectedValue;
            }            
            oneTimeContracts.EndDate = Funs.GetNewDateTime(this.txtEndDate.Text.Trim());
            oneTimeContracts.Remark = this.txtRemark.Text.Trim();
            if (!string.IsNullOrEmpty(oneTimeContractsId))
            {
                oneTimeContracts.OneTimeContractsId = oneTimeContractsId;
                BLL.OneTimeContractsService.UpdateOneTimeContracts(oneTimeContracts);
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Modify One Time Contracts!");
                ShowNotify("Save successfully!", MessageBoxIcon.Success);
            }
            else
            {
                oneTimeContracts.OneTimeContractsId = SQLHelper.GetNewID(typeof(Model.EMC_OneTimeContracts));
                BLL.OneTimeContractsService.AddOneTimeContracts(oneTimeContracts);
                BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Add One Time Contracts!");
                ShowNotify("Save successfully!", MessageBoxIcon.Success);
            }
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

        #region 权限设置
        /// <summary>
        /// 菜单按钮权限
        /// </summary>
        private void GetButtonPower()
        {
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.UserId, BLL.Const.OneTimeContractsMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion
    }
}