using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
namespace FineUIPro.Web.HSSE.Check
{
    public partial class RectifyNoticesFinalDocEdit : PageBase
    {
        public string RectifyNoticesFinalDocId
        {
            get
            {
                return (string)ViewState["RectifyNoticesFinalDocId"];
            }
            set
            {
                ViewState["RectifyNoticesFinalDocId"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitDropDownList();
                RectifyNoticesFinalDocId=Request.Params["RectifyNoticesFinalDocId"];
                var model =BLL.Check_RectifyNoticesFinalDocService.GetCheck_RectifyNoticesFinalDocById(RectifyNoticesFinalDocId);
                if (model!=null)
                {
                    txtCode.Text = model.RectifyNoticesFinalDocCode;
                    drpWorkAreaId.SelectedValueArray = model.WorkAreaId.Split(',');
                    drpUnitId.SelectedValue = model.UnitId;
                    dropCheckMan.SelectedValue = model.CheckManId;
                    txtCheckDate.SelectedDate = model.CheckManDate;
                    drpReformMan.SelectedValue=model.ReformManId;
                    txtReformDate.SelectedDate=model.ReformManDate;
                }
                else
                {
                    this.txtCode.Text = BLL.CodeRecordsService.ReturnCodeByMenuIdProjectId(BLL.Const.ProjectRectifyNoticesFinalDocEditMenuId, this.CurrUser.LoginProjectId, this.CurrUser.UnitId);
                    txtCheckDate.SelectedDate = DateTime.Now;
                }
            }
        }
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.SaveData();
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }
        /// 
        /// 保存数据
        /// 
        /// 
        private void SaveData()
        {
            Model.Check_RectifyNoticesFinalDoc model = new Model.Check_RectifyNoticesFinalDoc();
            model.RectifyNoticesFinalDocCode=txtCode.Text;
            model.State = 1;
            model.WorkAreaId = string .Join (",", drpWorkAreaId.SelectedValueArray);
            model.UnitId = drpUnitId.SelectedValue;
            model.CheckManId = dropCheckMan.SelectedValue;
            model.CheckManDate = txtCheckDate.SelectedDate;
            model.ReformManId = drpReformMan.SelectedValue;
            model.ReformManDate=txtReformDate.SelectedDate;
            model.CreateUserId = this.CurrUser.UserId;
            model.CreateDate = DateTime.Now;
            model.ProjectId=this.CurrUser.LoginProjectId;
            if (string.IsNullOrEmpty(RectifyNoticesFinalDocId))
            {
                model.RectifyNoticesFinalDocId = SQLHelper.GetNewID(typeof(Model.Check_RectifyNoticesFinalDoc));
                RectifyNoticesFinalDocId = model.RectifyNoticesFinalDocId;
                BLL.Check_RectifyNoticesFinalDocService.AddCheck_RectifyNoticesFinalDoc(model);
            }
            else
            {
                model.RectifyNoticesFinalDocId = RectifyNoticesFinalDocId;
                 BLL.Check_RectifyNoticesFinalDocService.UpdateCheck_RectifyNoticesFinalDoc(model);
            }
           
        }
        #endregion
        private void InitDropDownList()
        {
            //受检单位            
            BLL.UnitService.InitUnitByProjectIdUnitTypeDropDownList(this.drpUnitId, this.CurrUser.LoginProjectId, Const.ProjectUnitType_2, false);
            //单位工程
            BLL.UnitWorkService.InitUnitWorkDownList(this.drpWorkAreaId, this.CurrUser.LoginProjectId, true);
            BLL.UserService.InitFlowOperateControlUserDropDownList(this.dropCheckMan, this.CurrUser.LoginProjectId, null, true);
            BLL.UserService.InitFlowOperateControlUserDropDownList(this.drpReformMan, this.CurrUser.LoginProjectId, null, true);
            
        }
        protected void drpWorkAreaId_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.drpWorkAreaId.SelectedValueArray = Funs.RemoveDropDownListNull(this.drpWorkAreaId.SelectedValueArray);
        }
        #region 附件上传
        /// 
        /// 上传附件
        /// 
        /// 
        /// 
        protected void btnAttachUrl_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.RectifyNoticesFinalDocId))
            {
                SaveData();
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/Chect_RectifyNoticesFinalDocAttFile&menuId={1}", RectifyNoticesFinalDocId, BLL.Const.ProjectRectifyNoticesFinalDocEditMenuId)));
        }
        #endregion
    }
}