using BLL;
using System;
namespace FineUIPro.Web.HSSE.Hazard
{
    public partial class OtherHazardEdit : PageBase
    {
        #region 定义变量
        /// 
        /// 主键
        /// 
        public string OtherHazardId
        {
            get
            {
                return (string)ViewState["OtherHazardId"];
            }
            set
            {
                ViewState["OtherHazardId"] = value;
            }
        }
        /// 
        /// 项目主键
        /// 
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }
        #endregion
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ProjectId = this.CurrUser.LoginProjectId;
                this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                this.OtherHazardId = Request.Params["OtherHazardId"];
                var otherHazard = BLL.Hazard_OtherHazardService.GetOtherHazardByOtherHazardId(this.OtherHazardId);
                if (otherHazard != null)
                {
                    this.ProjectId = otherHazard.ProjectId;
                    this.txtOtherHazardCode.Text = BLL.CodeRecordsService.ReturnCodeByDataId(this.OtherHazardId);
                    this.txtOtherHazardName.Text = otherHazard.OtherHazardName;
                    this.txtCompileMan.Text = BLL.UserService.GetUserNameByUserId(otherHazard.CompileMan);
                    this.txtCompileDate.Text = string.Format("{0:yyyy-MM-dd}", otherHazard.CompileDate);
                }
                else
                {
                    ////自动生成编码
                    this.txtOtherHazardCode.Text = BLL.CodeRecordsService.ReturnCodeByMenuIdProjectId(BLL.Const.OtherHazardMenuId, this.ProjectId, this.CurrUser.UnitId);
                    this.txtCompileMan.Text = this.CurrUser.UserName;
                    this.txtCompileDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                }
            }
        }
        #endregion
        #region 保存按钮
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.SaveData(BLL.Const.BtnSave);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        /// 
        /// 保存数据
        /// 
        /// 
        private void SaveData(string type)
        {
            Model.Hazard_OtherHazard otherHazard = new Model.Hazard_OtherHazard
            {
                OtherHazardCode = this.txtOtherHazardCode.Text.Trim(),
                OtherHazardName = this.txtOtherHazardName.Text.Trim(),
                CompileDate = Funs.GetNewDateTime(this.txtCompileDate.Text.Trim()),
                ProjectId = this.ProjectId,
                ////单据状态
                States = BLL.Const.State_2
            };
            if (!string.IsNullOrEmpty(this.OtherHazardId))
            {
                otherHazard.OtherHazardId = this.OtherHazardId;
                BLL.Hazard_OtherHazardService.UpdateOtherHazard(otherHazard);
                BLL.LogService.AddSys_Log(this.CurrUser, otherHazard.OtherHazardCode, otherHazard.OtherHazardId, BLL.Const.OtherHazardMenuId, BLL.Const.BtnModify);
            }
            else
            {
                otherHazard.OtherHazardId = SQLHelper.GetNewID(typeof(Model.Hazard_OtherHazard));
                otherHazard.CompileMan = this.CurrUser.UserId;
                this.OtherHazardId = otherHazard.OtherHazardId;
                BLL.Hazard_OtherHazardService.AddOtherHazard(otherHazard);
                BLL.LogService.AddSys_Log(this.CurrUser, otherHazard.OtherHazardCode, otherHazard.OtherHazardId, BLL.Const.OtherHazardMenuId, BLL.Const.BtnAdd);
            }
        }
        private void SaveNew()
        {
            Model.Hazard_OtherHazard otherHazard = new Model.Hazard_OtherHazard
            {
                OtherHazardCode = this.txtOtherHazardCode.Text.Trim(),
                OtherHazardName = this.txtOtherHazardName.Text.Trim(),
                CompileDate = Funs.GetNewDateTime(this.txtCompileDate.Text.Trim()),
                ProjectId = this.ProjectId,
                ////单据状态
                States = BLL.Const.State_0,
                OtherHazardId = SQLHelper.GetNewID(typeof(Model.Hazard_OtherHazard)),
                CompileMan = this.CurrUser.UserId
            };
            this.OtherHazardId = otherHazard.OtherHazardId;
            BLL.Hazard_OtherHazardService.AddOtherHazard(otherHazard);
            BLL.LogService.AddSys_Log(this.CurrUser, otherHazard.OtherHazardCode, otherHazard.OtherHazardId, BLL.Const.OtherHazardMenuId, BLL.Const.BtnAdd);
        }
        #endregion
        #region 附件上传
        /// 
        /// 上传附件
        /// 
        /// 
        /// 
        protected void btnAttachUrl_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.OtherHazardId))
            {
                SaveNew();
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/OtherHazard&menuId={1}", this.OtherHazardId, BLL.Const.OtherHazardMenuId)));
        }
        #endregion        
    }
}