using BLL;
using System;
using System.Linq;
namespace FineUIPro.Web.CQMS.Comprehensive
{
    public partial class GeneralPlanApprovalEdit : PageBase
    {
        #region 定义变量
        /// 
        /// 主键
        /// 
        public string GeneralPlanApprovalId
        {
            get
            {
                return (string)ViewState["GeneralPlanApprovalId"];
            }
            set
            {
                ViewState["GeneralPlanApprovalId"] = value;
            }
        }
        #endregion
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetButtonPower();
                BLL.UnitWorkService.InitUnitWorkDownList(this.drpUnitWorkIds, this.CurrUser.LoginProjectId, false);
                BLL.CNProfessionalService.InitCNProfessionalDownList(this.drpCNProfessionalId, true);
                BLL.UnitService.InitUnitDownList(this.drpUnitId, this.CurrUser.LoginProjectId, true);
                this.GeneralPlanApprovalId = Request.Params["GeneralPlanApprovalId"];
                Model.Comprehensive_GeneralPlanApproval generalPlanApproval = BLL.GeneralPlanApprovalService.GetGeneralPlanApprovalById(this.GeneralPlanApprovalId);
                if (generalPlanApproval != null)
                {
                    this.GeneralPlanApprovalId = generalPlanApproval.GeneralPlanApprovalId;
                    this.hdAttachUrl.Text = this.GeneralPlanApprovalId;
                    if (!string.IsNullOrEmpty(generalPlanApproval.UnitId))
                    {
                        this.drpUnitId.SelectedValue = generalPlanApproval.UnitId;
                    }
                    if (!string.IsNullOrEmpty(generalPlanApproval.CNProfessionalId))
                    {
                        this.drpCNProfessionalId.SelectedValue = generalPlanApproval.CNProfessionalId;
                    }
                    this.txtPlanCode.Text = generalPlanApproval.PlanCode;
                    this.txtPlanName.Text = generalPlanApproval.PlanName;
                    if (generalPlanApproval.ApprovalDate != null)
                    {
                        this.txtApprovalDate.Text = string.Format("{0:yyyy-MM-dd}", generalPlanApproval.ApprovalDate);
                    }
                    if (!string.IsNullOrEmpty(generalPlanApproval.UnitWorkId))
                    {
                        this.drpUnitWorkIds.SelectedValueArray = generalPlanApproval.UnitWorkId.Split(',');
                    }
                    this.txtAuditMan.Text = generalPlanApproval.AuditMan;
                    this.txtApprovalMan.Text = generalPlanApproval.ApprovalMan;
                    this.txtImplementationDeviation.Text = generalPlanApproval.ImplementationDeviation;
                }
                else
                {
                    this.txtApprovalDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                }
            }
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        private void SaveData()
        {
            if (drpCNProfessionalId.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择专业!", MessageBoxIcon.Warning);
                return;
            }
            Model.Comprehensive_GeneralPlanApproval generalPlanApproval = new Model.Comprehensive_GeneralPlanApproval();
            generalPlanApproval.ProjectId = this.CurrUser.LoginProjectId;
            if (this.drpUnitId.SelectedValue != BLL.Const._Null)
            {
                generalPlanApproval.UnitId = this.drpUnitId.SelectedValue;
            }
            if (this.drpCNProfessionalId.SelectedValue != BLL.Const._Null)
            {
                generalPlanApproval.CNProfessionalId = this.drpCNProfessionalId.SelectedValue;
            }
            generalPlanApproval.PlanCode = this.txtPlanCode.Text.Trim();
            generalPlanApproval.PlanName = this.txtPlanName.Text.Trim();
            if (!string.IsNullOrEmpty(this.txtApprovalDate.Text))
            {
                generalPlanApproval.ApprovalDate = Convert.ToDateTime(this.txtApprovalDate.Text);
            }
            generalPlanApproval.AuditMan = this.txtAuditMan.Text.Trim();
            generalPlanApproval.ApprovalMan = this.txtApprovalMan.Text.Trim();
            generalPlanApproval.ImplementationDeviation = this.txtImplementationDeviation.Text.Trim();
            string ids = string.Empty;
            var lists = this.drpUnitWorkIds.SelectedValueArray;
            foreach (var item in lists)
            {
                ids += item + ",";
            }
            if (!string.IsNullOrEmpty(ids))
            {
                ids = ids.Substring(0, ids.LastIndexOf(","));
            }
            generalPlanApproval.UnitWorkId = ids;
            if (string.IsNullOrEmpty(this.GeneralPlanApprovalId))
            {
                if (!string.IsNullOrEmpty(this.hdAttachUrl.Text))
                {
                    generalPlanApproval.GeneralPlanApprovalId = this.hdAttachUrl.Text;
                }
                else
                {
                    generalPlanApproval.GeneralPlanApprovalId = SQLHelper.GetNewID(typeof(Model.Comprehensive_GeneralPlanApproval));
                    this.hdAttachUrl.Text = generalPlanApproval.GeneralPlanApprovalId;
                }
                generalPlanApproval.CompileMan = this.CurrUser.UserId;
                generalPlanApproval.CompileDate = DateTime.Now;
                BLL.GeneralPlanApprovalService.AddGeneralPlanApproval(generalPlanApproval);
            }
            else
            {
                generalPlanApproval.GeneralPlanApprovalId = this.GeneralPlanApprovalId;
                BLL.GeneralPlanApprovalService.UpdateGeneralPlanApproval(generalPlanApproval);
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
        #region 附件上传
        /// 
        /// 附件上传
        /// 
        /// 
        /// 
        protected void btnAttach_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdAttachUrl.Text))   //新增记录
            {
                this.hdAttachUrl.Text = SQLHelper.GetNewID(typeof(Model.Comprehensive_GeneralPlanApproval));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/CQMS/GeneralPlanApproval&menuId={1}", this.hdAttachUrl.Text, BLL.Const.GeneralPlanApprovalMenuId)));
        }
        #endregion
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            if (Request.Params["value"] == BLL.Const._Null)
            {
                return;
            }
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.GeneralPlanApprovalMenuId);
            if (buttonList.Count() > 0)
            {                
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion
    }
}