using BLL;
using System;
using System.Web;
namespace FineUIPro.Web.HSSE.License
{
    public partial class ApplyEdit : PageBase
    {
        #region 定义项
        /// 
        /// 主键
        /// 
        public string LicenseApplyId
        {
            get
            {
                return (string)ViewState["LicenseApplyId"];
            }
            set
            {
                ViewState["LicenseApplyId"] = 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.InitDropDownList();
                this.LicenseApplyId = Request.Params["LicenseApplyId"];
                if (!string.IsNullOrEmpty(this.LicenseApplyId))
                {
                    Model.License_Apply licenseManager = BLL.LicensePublicService.GetApplyById(this.LicenseApplyId);
                    if (licenseManager != null)
                    {
                        this.ProjectId = licenseManager.ProjectId;
                        if (this.ProjectId != this.CurrUser.LoginProjectId)
                        {
                            this.InitDropDownList();
                        }
                         //this.txtLicenseManageName.Text = licenseManager.LicenseManageName;
                        if (!string.IsNullOrEmpty(licenseManager.ApplyUnitId))
                        {
                            this.drpUnitId.SelectedValue = licenseManager.ApplyUnitId;
                        }
                        if (!string.IsNullOrEmpty(licenseManager.LicenseTypeId))
                        {
                            this.drpLicenseTypeId.SelectedValue = licenseManager.LicenseTypeId;
                        }
                        this.txtApplicantMan.Text = UserService.getUserNamesUserIds( licenseManager.ApplyManId);
                        this.txtCompileDate.Text = string.Format("{0:yyyy-MM-dd}", licenseManager.ApplyDate);
                      
                        this.txtStartDate.Text = string.Format("{0:yyyy-MM-dd HH:mm:ss}", licenseManager.ApplyStartTime);
                        this.txtEndDate.Text = string.Format("{0:yyyy-MM-dd}", licenseManager.EndTime);
                     }
                }
                else
                {
                    this.txtApplicantMan.Text = this.CurrUser.UserName;
                    this.drpUnitId.SelectedValue = this.CurrUser.UnitId;
                    this.txtCompileDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                    this.txtStartDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                    //this.txtEndDate.Text = string.Format("{0:yyyy-MM-dd}", "");
                    //var pcodeTemplateRule = BLL.ProjectData_CodeTemplateRuleService.GetProjectData_CodeTemplateRuleByMenuIdProjectId(BLL.Const.ProjectLicenseManagerMenuId, this.ProjectId);
                    //if (pcodeTemplateRule != null)
                    //{                  
                    //    this.txtLicenseManageContents.Text = HttpUtility.HtmlDecode(pcodeTemplateRule.Template);
                    //}  
                    ////自动生成编码
                 }
                ///初始化审核菜单
                this.ctlAuditFlow.MenuId = BLL.Const.ProjectLicenseApplyMenuId;
                this.ctlAuditFlow.DataId = this.LicenseApplyId;
                this.ctlAuditFlow.ProjectId = this.ProjectId;
                this.ctlAuditFlow.UnitId = this.CurrUser.UnitId;
            }
        }
        #endregion
        /// 
        ///  初始化下拉框
        /// 
        private void InitDropDownList()
        {
            UnitService.InitUnitDropDownList(this.drpUnitId, this.ProjectId, true);
            LicenseTypeService.InitLicenseTypeDropDownList(this.drpLicenseTypeId, true);
         }
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {              
            this.SaveData(BLL.Const.BtnSave);
           // PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }
        /// 
        /// 提交按钮
        /// 
        /// 
        /// 
        protected void btnSubmit_Click(object sender, EventArgs e)
        {                  
            if (this.ctlAuditFlow.NextStep == BLL.Const.State_1 && this.ctlAuditFlow.NextPerson == BLL.Const._Null)
            {
                ShowNotify("请选择下一步办理人!", MessageBoxIcon.Warning);
                return;
            }
            this.SaveData(BLL.Const.BtnSubmit);
           // PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }
        /// 
        /// 保存数据
        /// 
        /// 
        private void SaveData(string type)
        {
            if (this.drpLicenseTypeId.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择许可证类型!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpUnitId.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择申请单位!", MessageBoxIcon.Warning);
                return;
            }
            Model.License_Apply licenseManager = new Model.License_Apply
            {
                ProjectId = this.ProjectId,
             };
            //licenseManager.LicenseManageName = this.txtLicenseManageName.Text.Trim();
            if (this.drpLicenseTypeId.SelectedValue != BLL.Const._Null)
            {
                licenseManager.LicenseTypeId = this.drpLicenseTypeId.SelectedValue;
            }
            if (this.drpUnitId.SelectedValue != BLL.Const._Null)
            {
                licenseManager.ApplyUnitId = this.drpUnitId.SelectedValue;
            }
            
           // licenseManager.ApplyManId = this.txtApplicantMan.Text.Trim();
         
            licenseManager.ApplyStartTime = Funs.GetNewDateTime(this.txtStartDate.Text);
            licenseManager.EndTime = Funs.GetNewDateTime(this.txtEndDate.Text);
            licenseManager.ApplyDate = Funs.GetNewDateTime(this.txtCompileDate.Text.Trim());
             licenseManager.States = BLL.Const.State_0;
       
            if (type == BLL.Const.BtnSubmit)
            {
                licenseManager.States = this.ctlAuditFlow.NextStep;
             
            }
            if (!string.IsNullOrEmpty(this.LicenseApplyId))
            {
                licenseManager.LicenseApplyId = this.LicenseApplyId;
                BLL.LicensePublicService.UpdateApply(licenseManager);
                BLL.LogService.AddSys_Log(this.CurrUser, licenseManager.LicenseApplyId, licenseManager.LicenseApplyId, BLL.Const.ProjectLicenseManagerMenuId, BLL.Const.BtnModify);
            }
            else
            {
                licenseManager.ApplyManId = this.CurrUser.UserId;
                this.LicenseApplyId = SQLHelper.GetNewID(typeof(Model.License_LicenseManager));
                licenseManager.LicenseApplyId = this.LicenseApplyId;
                BLL.LicensePublicService.AddApply(licenseManager);
                BLL.LogService.AddSys_Log(this.CurrUser, licenseManager.LicenseApplyId, licenseManager.LicenseApplyId, BLL.Const.ProjectLicenseManagerMenuId, BLL.Const.BtnAdd);
            }
            ////保存流程审核数据         
            this.ctlAuditFlow.btnSaveData(this.ProjectId, BLL.Const.ProjectLicenseManagerMenuId, this.LicenseApplyId, (type == BLL.Const.BtnSubmit ? true : false), "作业票申请", "../License/ApplyView.aspx?LicenseApplyId={0}");
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }
        #endregion
     
     
    }
}