using BLL;
using System;
using System.Linq;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.ZHGL.Information
{
    public partial class EPSummaryReportEdit : PageBase
    {
        #region 定义变量
        /// 
        /// 主键
        /// 
        public string EPSummaryReportId
        {
            get
            {
                return (string)ViewState["EPSummaryReportId"];
            }
            set
            {
                ViewState["EPSummaryReportId"] = value;
            }
        }
        #endregion
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ddlUnitId.DataTextField = "UnitName";
                this.ddlUnitId.DataValueField = "UnitId";
                this.ddlUnitId.DataSource = BLL.UnitService.GetThisUnitDropDownList();
                this.ddlUnitId.DataBind();
                this.ddlYearId.DataTextField = "ConstText";
                ddlYearId.DataValueField = "ConstValue";
                ddlYearId.DataSource = BLL.ConstValue.drpConstItemList(ConstValue.Group_0008);
                ddlYearId.DataBind();
                this.ddlQuarter.DataTextField = "ConstText";
                ddlQuarter.DataValueField = "ConstValue";
                ddlQuarter.DataSource = BLL.ConstValue.drpConstItemList(ConstValue.Group_0011);
                ddlQuarter.DataBind();
                this.ddlUnitId.Readonly = true;
                string unitId = Request.Params["UnitId"];
                string year = Request.QueryString["Year"];
                string quarter = Request.QueryString["Quarter"];
                this.EPSummaryReportId = Request.Params["EPSummaryReportId"];
                if (!string.IsNullOrEmpty(this.EPSummaryReportId))
                {
                    var report = BLL.EPSummaryReportService.GetEPSummaryReportById(this.EPSummaryReportId);
                    if (report != null)
                    {
                        this.btnCopy.Hidden = true;
                        if (report.UpState == BLL.Const.UpState_3)  //已上报
                        {
                            this.btnSave.Hidden = true;
                            this.btnUpdata.Hidden = true;
                        }
                        #region 赋值
                        if (!string.IsNullOrEmpty(report.UnitId))
                        {
                            this.ddlUnitId.SelectedValue = report.UnitId;
                        }
                        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
                {
                    this.btnCopy.Hidden = false;
                    this.ddlUnitId.SelectedValue = unitId;
                    this.ddlYearId.SelectedValue = year;
                    this.ddlQuarter.SelectedValue = quarter;
                    this.txtReportDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                    #region 获取项目上报数据
                    string description1 = string.Empty;
                    string description2 = string.Empty;
                    string description3 = string.Empty;
                    string description4 = string.Empty;
                    string description5 = string.Empty;
                    string description6 = string.Empty;
                    string description7 = string.Empty;
                    string description8 = string.Empty;
                    string description9 = string.Empty;
                    int date = Convert.ToInt32(this.ddlYearId.SelectedValue);
                    var projects = (from x in Funs.DB.Base_Project
                                    where (x.ProjectState == BLL.Const.ProjectState_1 || x.ProjectState == null)
                                    && x.ProjectAttribute == "GONGCHENG"
                                    && x.StartDate.Value.Year <= date
                                    select x).ToList();
                    foreach (var item in projects)
                    {
                        var report = Funs.DB.InformationProject_EPSummaryReport.FirstOrDefault(x => x.ProjectId == item.ProjectId && x.YearId == date && x.Quarter.ToString() == quarter);
                        if (report != null)
                        {
                            description1 += item.ProjectName + ":\r\n" + report.Description1 + "\r\n";
                            description2 += item.ProjectName + ":\r\n" + report.Description2 + "\r\n";
                            description3 += item.ProjectName + ":\r\n" + report.Description3 + "\r\n";
                            description4 += item.ProjectName + ":\r\n" + report.Description4 + "\r\n";
                            description5 += item.ProjectName + ":\r\n" + report.Description5 + "\r\n";
                            description6 += item.ProjectName + ":\r\n" + report.Description6 + "\r\n";
                            description7 += item.ProjectName + ":\r\n" + report.Description7 + "\r\n";
                            description8 += item.ProjectName + ":\r\n" + report.Description8 + "\r\n";
                            description9 += item.ProjectName + ":\r\n" + report.Description9 + "\r\n";
                        }
                    }
                    this.txtDescription1.Text = description1;
                    this.txtDescription2.Text = description2;
                    this.txtDescription3.Text = description3;
                    this.txtDescription4.Text = description4;
                    this.txtDescription5.Text = description5;
                    this.txtDescription6.Text = description6;
                    this.txtDescription7.Text = description7;
                    this.txtDescription8.Text = description8;
                    this.txtDescription9.Text = description9;
                    #endregion
                }
            }
        }
        #endregion
        #region 保存数据
        private void Save(string type)
        {
            Model.Information_EPSummaryReport report = new Model.Information_EPSummaryReport();
            if (this.ddlUnitId.SelectedValue != BLL.Const._Null)
            {
                report.UnitId = this.ddlUnitId.SelectedValue;
            }
            else
            {
                ShowNotify("请选择单位!", MessageBoxIcon.Warning);
                return;
            }
            if (this.ddlYearId.SelectedValue != BLL.Const._Null)
            {
                report.YearId = Funs.GetNewIntOrZero(this.ddlYearId.SelectedValue);
            }
            else
            {
                ShowNotify("请选择年度!", MessageBoxIcon.Warning);
                return;
            }
            if (this.ddlQuarter.SelectedValue != BLL.Const._Null)
            {
                report.Quarter = Funs.GetNewIntOrZero(this.ddlQuarter.SelectedValue);
            }
            else
            {
                ShowNotify("请选择年度!", MessageBoxIcon.Warning);
                return;
            }
            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 = this.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();
            int quarter = Funs.GetNewIntOrZero(ddlQuarter.SelectedValue);
            if (string.IsNullOrEmpty(this.EPSummaryReportId))
            {
                var s = BLL.EPSummaryReportService.GetEPSummaryReportByUnitIdAndYearIdAndQuarters(this.ddlUnitId.SelectedValue, Funs.GetNewIntOrZero(this.ddlYearId.SelectedValue), quarter);
                if (s != null)
                {
                    ShowNotify("该单位的该年度的该生态环保工作总结报告已经存在,不能重复编制!", MessageBoxIcon.Warning);
                    return;
                }
                else
                {
                    this.EPSummaryReportId = SQLHelper.GetNewID(typeof(Model.Information_EPSummaryReport));
                    report.EPSummaryReportId = this.EPSummaryReportId;
                    report.UpState = BLL.Const.UpState_2;
                    BLL.EPSummaryReportService.AddEPSummaryReport(report);
                    BLL.LogService.AddSys_Log(this.CurrUser, this.ddlYearId.SelectedText, report.EPSummaryReportId, BLL.Const.EPSummaryReportMenuId, BLL.Const.BtnAdd);
                }
            }
            else
            {
                report.EPSummaryReportId = this.EPSummaryReportId;
                report.UpState = BLL.Const.UpState_2;
                BLL.EPSummaryReportService.UpdateEPSummaryReport(report);
                BLL.LogService.AddSys_Log(this.CurrUser, this.ddlYearId.SelectedText, report.EPSummaryReportId, BLL.Const.EPSummaryReportMenuId, BLL.Const.BtnModify);
            }
            if (type == "updata")     //保存并上报
            {
                if (report.UpState == BLL.Const.UpState_2)
                {
                    string code = CNCECHSSEWebService.UpEPSummaryReport(report.EPSummaryReportId, this.CurrUser);
                    if (code == "1")
                    {
                        ShowNotify("同步成功!", MessageBoxIcon.Success);
                        PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
                        return;
                    }
                    else
                    {
                        Alert.ShowInParent("同步异常,请退出后重试!", MessageBoxIcon.Error);
                    }
                }
                else
                {
                    ShowNotify("当前单据状态不能同步!", MessageBoxIcon.Warning);
                    return;
                }
            }
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Save("add");
        }
        protected void btnUpdata_Click(object sender, EventArgs e)
        {
            Save("updata");
        }
        #endregion    
        #region 复制上个年度数据
        /// 
        /// 复制上个年度数据
        /// 
        /// 
        /// 
        protected void btnCopy_Click(object sender, EventArgs e)
        {
            int lastYear = 0, lastQuarter = 0;
            int year = Convert.ToInt32(this.ddlYearId.SelectedValue);
            int quarter = Convert.ToInt32(this.ddlQuarter.SelectedValue);
            if (quarter == 1)
            {
                lastYear = year - 1;
                lastQuarter = 4;
            }
            else
            {
                lastYear = year;
                lastQuarter = quarter - 1;
            }
            Model.Information_EPSummaryReport report = BLL.EPSummaryReportService.GetEPSummaryReportByUnitIdAndYearIdAndQuarters(this.ddlUnitId.SelectedValue, lastYear, lastQuarter);
            if (report != null)
            {
                Model.Information_EPSummaryReport newReport = new Model.Information_EPSummaryReport();
                this.EPSummaryReportId = SQLHelper.GetNewID(typeof(Model.Information_EPSummaryReport));
                newReport.EPSummaryReportId = this.EPSummaryReportId;
                newReport.UnitId = this.ddlUnitId.SelectedValue;
                newReport.YearId = Funs.GetNewIntOrZero(this.ddlYearId.SelectedValue);
                newReport.ResponsiblePerson = report.ResponsiblePerson;
                newReport.ResponsiblePersonTel = report.ResponsiblePersonTel;
                newReport.ContactPerson = report.ContactPerson;
                newReport.ContactPersonTel = report.ContactPersonTel;
                newReport.ReportDate = report.ReportDate;
                newReport.Description1 = report.Description1;
                newReport.Description2 = report.Description2;
                newReport.Description3 = report.Description3;
                newReport.Description4 = report.Description4;
                newReport.Description5 = report.Description5;
                newReport.Description6 = report.Description6;
                newReport.Description7 = report.Description7;
                newReport.Description8 = report.Description8;
                newReport.Description9 = report.Description9;
                newReport.UpState = BLL.Const.UpState_2;
                BLL.EPSummaryReportService.AddEPSummaryReport(newReport);
                GetValues(newReport.EPSummaryReportId);
            }
        }
        /// 
        /// 赋值
        ///  
        private void GetValues(string ePSummaryReportId)
        {
            var report = BLL.EPSummaryReportService.GetEPSummaryReportById(ePSummaryReportId);
            if (report != null)
            {
                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
    }
}