using BLL;
using System;
using System.Linq;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.HSSE.Technique
{
    public partial class ConstructionStandardsEdit : PageBase
    {
        #region 定义变量
        /// 
        /// 防护标准ID
        /// 
        public string ConstructionStandardsId
        {
            get
            {
                return (string)ViewState["ConstructionStandardsId"];
            }
            set
            {
                ViewState["ConstructionStandardsId"] = value;
            }
        }
        #endregion
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ////权限按钮方法
                this.GetButtonPower();
                btnClose.OnClientClick = ActiveWindow.GetHideReference();
                //人员              
                UserService.InitUserDropDownList(this.ddlCompileMan, string.Empty, true);
                //加载默认整理人、整理日期
                this.ddlCompileMan.SelectedValue = this.CurrUser.UserId;
                this.dpkCompileDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                this.ConstructionStandardsId = Request.Params["ConstructionStandardsId"];
                if (!string.IsNullOrEmpty(this.ConstructionStandardsId))
                {
                    var ConstructionStandards = BLL.ConstructionStandardsService.GetConstructionStandardsListById(this.ConstructionStandardsId);
                    if (ConstructionStandards != null)
                    {
                        this.txtConstructionStandardsCode.Text = ConstructionStandards.ConstructionStandardsCode;
                        this.txtConstructionStandardsName.Text = ConstructionStandards.ConstructionStandardsName;
                        this.txtRemark.Text = ConstructionStandards.Remark;
                        this.dpkCompileDate.Text = string.Format("{0:yyyy-MM-dd}", ConstructionStandards.CompileDate);
                        
                        if (!string.IsNullOrEmpty(ConstructionStandards.CompileMan))
                        {
                            this.ddlCompileMan.SelectedItem.Text = ConstructionStandards.CompileMan;
                        }
                    }
                }
            }
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveData();
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        
        /// 
        /// 保存数据
        /// 
        private void SaveData()
        {
            Model.Technique_ConstructionStandards ConstructionStandards = new Model.Technique_ConstructionStandards();           
            if (!string.IsNullOrEmpty(this.txtConstructionStandardsCode.Text.Trim()))
            {
                ConstructionStandards.ConstructionStandardsCode = this.txtConstructionStandardsCode.Text.Trim();
            }
            else
            {
                ShowNotify("请输入编号", MessageBoxIcon.Warning);
                return;
            }
            if (!string.IsNullOrEmpty(this.txtConstructionStandardsName.Text.Trim()))
            {
                ConstructionStandards.ConstructionStandardsName = this.txtConstructionStandardsName.Text.Trim();
            }
            else
            {
                ShowNotify("请输入名称", MessageBoxIcon.Warning);
                return;
            }
            
            if (this.ddlCompileMan.SelectedValue != null)
            {
                ConstructionStandards.CompileMan = this.ddlCompileMan.SelectedItem.Text;
            }
            else
            {
                ConstructionStandards.CompileMan = this.CurrUser.UserName;
            }
            if (!string.IsNullOrEmpty(this.dpkCompileDate.Text))
            {
                ConstructionStandards.CompileDate = Convert.ToDateTime(this.dpkCompileDate.Text);
            }
            else
            {
                ConstructionStandards.CompileDate = DateTime.Now;
            }
            if (txtRemark.Text.Trim() != string.Empty)
            {
                ConstructionStandards.Remark = txtRemark.Text.Trim();
            }
            if (string.IsNullOrEmpty(this.ConstructionStandardsId))
            {
                ConstructionStandards.IsPass = true;
                this.ConstructionStandardsId = ConstructionStandards.ConstructionStandardsId = SQLHelper.GetNewID(typeof(Model.Technique_ConstructionStandards));
                BLL.ConstructionStandardsService.AddConstructionStandardsList(ConstructionStandards);
                BLL.LogService.AddSys_Log(this.CurrUser, ConstructionStandards.ConstructionStandardsCode, ConstructionStandards.ConstructionStandardsId, BLL.Const.ConstructionStandardsMenuId, Const.BtnAdd);
            }
            else
            {
                ConstructionStandards.ConstructionStandardsId = this.ConstructionStandardsId;
                BLL.ConstructionStandardsService.UpdateConstructionStandardsList(ConstructionStandards);
                BLL.LogService.AddSys_Log(this.CurrUser, ConstructionStandards.ConstructionStandardsCode, ConstructionStandards.ConstructionStandardsId, BLL.Const.ConstructionStandardsMenuId, Const.BtnModify);                
            }
        }
        #endregion
        
        #region 附件上传
        /// 
        /// 上传附件资源
        /// 
        /// 
        /// 
        protected void btnUploadResources_Click(object sender, EventArgs e)
        {
            if (this.btnSave.Hidden)
            {
                PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("~/AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/ConstructionStandards&type=-1", this.ConstructionStandardsId)));
            }
            else
            {
                if (string.IsNullOrEmpty(this.txtConstructionStandardsCode.Text.Trim()))
                {
                    ShowNotify("请输入方案编号", MessageBoxIcon.Warning);
                    return;
                }
                if (string.IsNullOrEmpty(this.txtConstructionStandardsName.Text.Trim()))
                {
                    ShowNotify("请输入方案名称", MessageBoxIcon.Warning);
                    return;
                }
                
                if (string.IsNullOrEmpty(this.ConstructionStandardsId))
                {
                    SaveData();
                }
                PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("~/AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/ConstructionStandards&menuId={1}", this.ConstructionStandardsId, BLL.Const.ConstructionStandardsMenuId)));
            }
        }
        #endregion
        #region 验证方案编号、名称是否存在
        /// 
        /// 验证方案编号、名称是否存在
        /// 
        /// 
        /// 
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            var q = Funs.DB.Technique_ConstructionStandards.FirstOrDefault(x => x.IsPass == true && x.ConstructionStandardsCode == this.txtConstructionStandardsCode.Text.Trim() && (x.ConstructionStandardsId != this.ConstructionStandardsId || (this.ConstructionStandardsId == null && x.ConstructionStandardsId != null)));
            if (q != null)
            {
                ShowNotify("输入的方案编号已存在!", MessageBoxIcon.Warning);
            }
            var q2 = Funs.DB.Technique_ConstructionStandards.FirstOrDefault(x => x.IsPass == true && x.ConstructionStandardsName == this.txtConstructionStandardsName.Text.Trim() && (x.ConstructionStandardsId != this.ConstructionStandardsId || (this.ConstructionStandardsId == null && x.ConstructionStandardsId != null)));
            if (q2 != null)
            {
                ShowNotify("输入的方案名称已存在!", MessageBoxIcon.Warning);
            }
        }
        #endregion
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            if (Request.Params["value"] == "0")
            {
                return;
            }
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.ConstructionStandardsMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion
    }
}