using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.HSSE.InformationProject
{
    public partial class EPSummaryReportEdit : PageBase
    {
        #region 定义项
        /// 
        /// 主键
        /// 
        private string EPSummaryReportId
        {
            get
            {
                return (string)ViewState["EPSummaryReportId"];
            }
            set
            {
                ViewState["EPSummaryReportId"] = 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.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                this.ProjectId = this.CurrUser.LoginProjectId;
                if (!string.IsNullOrEmpty(Request.Params["projectId"]) && Request.Params["projectId"] != this.CurrUser.LoginProjectId)
                {
                    this.ProjectId = Request.Params["projectId"];
                }
                BLL.ConstValue.InitConstValueDropDownList(this.ddlYearId, BLL.ConstValue.Group_0008, true);
                BLL.ConstValue.InitConstValueDropDownList(this.ddlQuarter, BLL.ConstValue.Group_0011, true);
                this.EPSummaryReportId = Request.Params["EPSummaryReportId"];
                if (!string.IsNullOrEmpty(this.EPSummaryReportId))
                {
                    var report = BLL.ProjectEPSummaryReportService.GetEPSummaryReportById(this.EPSummaryReportId);
                    if (report != null)
                    {
                        this.ProjectId = report.ProjectId;
                        #region 赋值
                        if (report.YearId != null)
                        {
                            this.ddlYearId.SelectedValue = report.YearId.ToString();
                            if (report.Quarter.HasValue)
                            {
                                this.ddlQuarter.SelectedValue = report.Quarter.ToString();
                            }
                            this.txtReportDate.Text = report.ReportDate.HasValue ? string.Format("{0:yyyy-MM-dd}", report.ReportDate) : "";
                            this.txtResponsiblePerson.Text = report.ResponsiblePerson;
                            this.txtResponsiblePersonTel.Text = report.ResponsiblePersonTel;
                            this.txtContactPerson.Text = report.ContactPerson;
                            this.txtContactPersonTel.Text = report.ContactPersonTel;
                            this.txtDescription1.Text = report.Description1;
                            this.txtDescription2.Text = report.Description2;
                            this.txtDescription3.Text = report.Description3;
                            this.txtDescription4.Text = report.Description4;
                            this.txtDescription5.Text = report.Description5;
                            this.txtDescription6.Text = report.Description6;
                            this.txtDescription7.Text = report.Description7;
                            this.txtDescription8.Text = report.Description8;
                            this.txtDescription9.Text = report.Description9;
                        }
                        #endregion
                    }
                }
                else
                {
                    DateTime showDate = DateTime.Now.AddMonths(-3);
                    this.ddlYearId.SelectedValue = DateTime.Now.Year.ToString();
                    this.ddlQuarter.SelectedValue = Funs.GetNowQuarterlyByTime(showDate).ToString();
                    this.txtReportDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                }
            }
        }
        #endregion        
        #region 保存、提交
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.ddlYearId.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择年度", MessageBoxIcon.Warning);
                return;
            }
            this.SaveData(BLL.Const.BtnSave);
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }
        /// 
        /// 提交数据
        /// 
        /// 
        private void SaveData(string type)
        {
            Model.InformationProject_EPSummaryReport report = new Model.InformationProject_EPSummaryReport
            {
                ProjectId = this.ProjectId,
                UnitId = string.IsNullOrEmpty(this.CurrUser.UnitId) ? CommonService.GetThisUnitId() : this.CurrUser.UnitId,
            };
            if (this.ddlYearId.SelectedValue != BLL.Const._Null)
            {
                report.YearId = Funs.GetNewIntOrZero(this.ddlYearId.SelectedValue);
            }
            if (this.ddlQuarter.SelectedValue != BLL.Const._Null)
            {
                report.Quarter = Funs.GetNewIntOrZero(this.ddlQuarter.SelectedValue);
            }
            report.ResponsiblePerson = this.txtResponsiblePerson.Text.Trim();
            report.ResponsiblePersonTel = this.txtResponsiblePersonTel.Text.Trim();
            report.ContactPerson = this.txtContactPerson.Text.Trim();
            report.ContactPersonTel = this.txtContactPersonTel.Text.Trim();
            report.ReportDate = Funs.GetNewDateTime(this.txtReportDate.Text.Trim());
            report.Description1 = txtDescription1.Text.Trim();
            report.Description2 = this.txtDescription2.Text.Trim();
            report.Description3 = this.txtDescription3.Text.Trim();
            report.Description4 = this.txtDescription4.Text.Trim();
            report.Description5 = this.txtDescription5.Text.Trim();
            report.Description6 = this.txtDescription6.Text.Trim();
            report.Description7 = this.txtDescription7.Text.Trim();
            report.Description8 = this.txtDescription8.Text.Trim();
            report.Description9 = this.txtDescription9.Text.Trim();
            if (!string.IsNullOrEmpty(this.EPSummaryReportId))
            {
                report.EPSummaryReportId = this.EPSummaryReportId;
                BLL.ProjectEPSummaryReportService.UpdateEPSummaryReport(report);
                BLL.LogService.AddSys_Log(this.CurrUser, report.YearId.ToString(), report.EPSummaryReportId, BLL.Const.ProjectEPSummaryReportMenuId, BLL.Const.BtnModify);
            }
            else
            {
                Model.InformationProject_EPSummaryReport oldReport = (from x in Funs.DB.InformationProject_EPSummaryReport
                                                                      where x.ProjectId == report.ProjectId && x.YearId == report.YearId && x.Quarter == report.Quarter
                                                                      select x).FirstOrDefault();
                if (oldReport == null)
                {
                    this.EPSummaryReportId = SQLHelper.GetNewID(typeof(Model.InformationProject_EPSummaryReport));
                    report.EPSummaryReportId = this.EPSummaryReportId;
                    BLL.ProjectEPSummaryReportService.AddEPSummaryReport(report);
                    BLL.LogService.AddSys_Log(this.CurrUser, report.YearId.ToString(), report.EPSummaryReportId, BLL.Const.ProjectEPSummaryReportMenuId, BLL.Const.BtnAdd);
                }
                else
                {
                    Alert.ShowInTop("该季度记录已存在", MessageBoxIcon.Warning);
                    return;
                }
            }
        }
        #endregion
    }
}