using BLL;
using System;
using System.Linq;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.HSSE.Law
{
    public partial class RulesRegulationsEdit : PageBase
    {
        #region 定义变量
        /// 
        /// 主键
        /// 
        public string RulesRegulationsId
        {
            get
            {
                return (string)ViewState["RulesRegulationsId"];
            }
            set
            {
                ViewState["RulesRegulationsId"] = value;
            }
        }      
        #endregion
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.GetButtonPower();
                LoadData();
                //加载规章制度类别下拉选项
                this.ddlRulesRegulationsTypeId.DataTextField = "RulesRegulationsTypeName";
                this.ddlRulesRegulationsTypeId.DataValueField = "RulesRegulationsTypeId";
                this.ddlRulesRegulationsTypeId.DataSource = BLL.RulesRegulationsTypeService.GetRulesRegulationsTypeList();
                ddlRulesRegulationsTypeId.DataBind();
                Funs.FineUIPleaseSelect(this.ddlRulesRegulationsTypeId);
                this.RulesRegulationsId = Request.Params["RulesRegulationsId"];
                if (!string.IsNullOrEmpty(this.RulesRegulationsId))
                {
                    var rulesRegulation = BLL.RulesRegulationsService.GetRulesRegulationsById(this.RulesRegulationsId);
                    if (rulesRegulation != null)
                    {
                        this.txtRulesRegulationsCode.Text = rulesRegulation.RulesRegulationsCode;
                        this.txtRulesRegulationsName.Text = rulesRegulation.RulesRegulationsName;
                        if (!string.IsNullOrEmpty(rulesRegulation.RulesRegulationsTypeId))
                        {
                            this.ddlRulesRegulationsTypeId.SelectedValue = rulesRegulation.RulesRegulationsTypeId;
                        }
                        if (rulesRegulation.CustomDate != null)
                        {
                            this.dpkCustomDate.Text = string.Format("{0:yyyy-MM-dd}", rulesRegulation.CustomDate);
                        }
                        this.txtApplicableScope.Text = rulesRegulation.ApplicableScope;
                        //if (!string.IsNullOrEmpty(rulesRegulation.AttachUrl))
                        //{
                        //    this.FullAttachUrl = rulesRegulation.AttachUrl;
                        //    this.lbAttachUrl.Text = rulesRegulation.AttachUrl.Substring(rulesRegulation.AttachUrl.IndexOf("~") + 1);
                        //}
                        this.txtRemark.Text = rulesRegulation.Remark;
                    }
                }
               
            }
        }
        /// 
        /// 加载页面
        /// 
        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveData(true);
        }
        
        /// 
        /// 保存数据
        /// 
        private void SaveData(bool isClose)
        {
            Model.Law_RulesRegulations rulesRegulations = new Model.Law_RulesRegulations
            {
                RulesRegulationsCode = this.txtRulesRegulationsCode.Text.Trim(),
                RulesRegulationsName = this.txtRulesRegulationsName.Text.Trim()
            };
            if (this.ddlRulesRegulationsTypeId.SelectedValue != BLL.Const._Null)
            {
                rulesRegulations.RulesRegulationsTypeId = this.ddlRulesRegulationsTypeId.SelectedValue;
            }
            if (!string.IsNullOrEmpty(this.dpkCustomDate.Text.Trim()))
            {
                rulesRegulations.CustomDate = Convert.ToDateTime(this.dpkCustomDate.Text.Trim());
            }
            rulesRegulations.ApplicableScope = this.txtApplicableScope.Text.Trim();
            rulesRegulations.Remark = this.txtRemark.Text.Trim();
            //rulesRegulations.AttachUrl = this.FullAttachUrl;
            if (string.IsNullOrEmpty(this.RulesRegulationsId))
            {
                rulesRegulations.IsPass = true;
                rulesRegulations.CompileMan = this.CurrUser.UserName;
                rulesRegulations.CompileDate = System.DateTime.Now;
                rulesRegulations.UnitId = string.IsNullOrEmpty(this.CurrUser.UnitId) ? Const.UnitId_CWCEC : this.CurrUser.UnitId;
                this.RulesRegulationsId = SQLHelper.GetNewID();
                rulesRegulations.RulesRegulationsId = this.RulesRegulationsId;
                BLL.RulesRegulationsService.AddRulesRegulations(rulesRegulations);
                BLL.LogService.AddSys_Log(this.CurrUser, rulesRegulations.RulesRegulationsCode, rulesRegulations.RulesRegulationsId, BLL.Const.RulesRegulationsMenuId, BLL.Const.BtnAdd);
            }
            else
            {
                rulesRegulations.RulesRegulationsId = this.RulesRegulationsId;
                BLL.RulesRegulationsService.UpdateRulesRegulations(rulesRegulations);
                BLL.LogService.AddSys_Log(this.CurrUser, rulesRegulations.RulesRegulationsCode, rulesRegulations.RulesRegulationsId, BLL.Const.RulesRegulationsMenuId, BLL.Const.BtnModify);
            }
            if (isClose)
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
        }
        #endregion
        #region 按钮权限
        /// 
        /// 获取权限
        /// 
        private void GetButtonPower()
        {
            if (Request.Params["value"] == "0")
            {
                return;
            }
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.RulesRegulationsMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion
        #region 验证规章制度名称是否存在
        /// 
        /// 验证规章制度名称是否存在
        /// 
        /// 
        /// 
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            var standard = Funs.DB.Law_RulesRegulations.FirstOrDefault(x => x.IsPass == true && x.RulesRegulationsName == this.txtRulesRegulationsName.Text.Trim() && (x.RulesRegulationsId != this.RulesRegulationsId || (this.RulesRegulationsId == null && x.RulesRegulationsId != null)));
            if (standard != null)
            {
                ShowNotify("输入的规章名称已存在!", MessageBoxIcon.Warning);
            }
        }
        #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/RulesRegulations&type=-1", RulesRegulationsId)));
            }
            else
            {
                if (string.IsNullOrEmpty(this.RulesRegulationsId))
                {
                    SaveData(false);
                }
                PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("~/AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/RulesRegulations&menuId={1}", RulesRegulationsId, BLL.Const.RulesRegulationsMenuId)));
            }
        }
        #endregion
    }
}