using BLL;
using System;
using System.Linq;

namespace FineUIPro.Web.HSSE.InformationProject
{
    public partial class WorkSummaryReportEdit : PageBase
    {
        #region 定义项
        /// <summary>
        /// 主键
        /// </summary>
        private string WorkSummaryReportId
        {
            get
            {
                return (string)ViewState["WorkSummaryReportId"];
            }
            set
            {
                ViewState["WorkSummaryReportId"] = value;
            }
        }

        /// <summary>
        /// 项目主键
        /// </summary>
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }
        #endregion        

        #region 加载
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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);

                this.WorkSummaryReportId = Request.Params["workSummaryReportId"];
                if (!string.IsNullOrEmpty(this.WorkSummaryReportId))
                {
                    var report = BLL.ProjectWorkSummaryReportService.GetWorkSummaryReportById(this.WorkSummaryReportId);
                    if (report != null)
                    {
                        this.ProjectId = report.ProjectId;
                        #region 赋值
                        if (report.YearId != null)
                        {
                            this.ddlYearId.SelectedValue = report.YearId.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.txtSafeLaborTime.Text = report.SafeLaborTime.HasValue ? report.SafeLaborTime.ToString() : "";
                            this.txtSafetyObjectives.Text = report.SafetyObjectives;
                            this.txtAccidentSituation.Text = report.AccidentSituation;
                            this.txtAwards.Text = report.Awards;
                            this.txtWorkDevelopment.Text = report.WorkDevelopment;
                            this.txtPersonnelTraining.Text = report.PersonnelTraining;
                            this.txtGovernanceSituation.Text = report.GovernanceSituation;
                            this.txtManagementActivity.Text = report.ManagementActivity;
                            this.txtWorkExperience.Text = report.WorkExperience;
                            this.txtCountermeasures.Text = report.Countermeasures;
                            this.txtNextYearWorkPlan.Text = report.NextYearWorkPlan;
                            this.txtJobSuggestion.Text = report.JobSuggestion;
                        }
                        #endregion
                    }
                }
                else
                {
                    this.ddlYearId.SelectedValue = DateTime.Now.Year.ToString();
                    this.txtReportDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                }
            }
        }
        #endregion        

        #region 保存、提交
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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());
        }

        ///// <summary>
        ///// 提交按钮
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //protected void btnSubmit_Click(object sender, EventArgs e)
        //{
        //    if (this.ddlYearId.SelectedValue == BLL.Const._Null)
        //    {
        //        Alert.ShowInTop("请选择年度", MessageBoxIcon.Warning);
        //        return;
        //    }
        //    if (this.ddlQuarter.SelectedValue == BLL.Const._Null)
        //    {
        //        Alert.ShowInTop("请选择季度", MessageBoxIcon.Warning);
        //        return;
        //    }
        //    this.SaveData(BLL.Const.BtnSubmit);
        //    PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        //}

        /// <summary>
        /// 提交数据
        /// </summary>
        /// <param name="p"></param>
        private void SaveData(string type)
        {
            Model.InformationProject_WorkSummaryReport report = new Model.InformationProject_WorkSummaryReport
            {
                ProjectId = this.ProjectId,
                UnitId = string.IsNullOrEmpty(this.CurrUser.UnitId) ? Const.UnitId_TCC : this.CurrUser.UnitId,
            };
            if (this.ddlYearId.SelectedValue != BLL.Const._Null)
            {
                report.YearId = Funs.GetNewIntOrZero(this.ddlYearId.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.SafeLaborTime = Funs.GetNewDecimal(this.txtSafeLaborTime.Text);
            report.SafetyObjectives = this.txtSafetyObjectives.Text.Trim();
            report.AccidentSituation = this.txtAccidentSituation.Text.Trim();
            report.Awards = this.txtAwards.Text.Trim();
            report.WorkDevelopment = this.txtWorkDevelopment.Text.Trim();
            report.PersonnelTraining = this.txtPersonnelTraining.Text.Trim();
            report.GovernanceSituation = this.txtGovernanceSituation.Text.Trim();
            report.ManagementActivity = this.txtManagementActivity.Text.Trim();
            report.WorkExperience = this.txtWorkExperience.Text.Trim();
            report.Countermeasures = this.txtCountermeasures.Text.Trim();
            report.NextYearWorkPlan = this.txtNextYearWorkPlan.Text.Trim();
            report.JobSuggestion = this.txtJobSuggestion.Text.Trim();

            report.CompileMan = this.CurrUser.UserId;
            report.CompileDate = DateTime.Now;
            //if (type == BLL.Const.BtnSubmit)
            //{
            //    safetyQuarterlyReport.States = this.ctlAuditFlow.NextStep;
            //}
            if (!string.IsNullOrEmpty(this.WorkSummaryReportId))
            {
                report.WorkSummaryReportId = this.WorkSummaryReportId;
                BLL.ProjectWorkSummaryReportService.UpdateWorkSummaryReport(report);
                BLL.LogService.AddSys_Log(this.CurrUser, report.YearId.ToString(), report.WorkSummaryReportId, BLL.Const.ProjectWorkSummaryReportMenuId, BLL.Const.BtnModify);
            }
            else
            {
                Model.InformationProject_WorkSummaryReport oldReport = (from x in Funs.DB.InformationProject_WorkSummaryReport
                                                                                       where x.ProjectId == report.ProjectId && x.YearId == report.YearId 
                                                                                           select x).FirstOrDefault();
                if (oldReport == null)
                {
                    this.WorkSummaryReportId = SQLHelper.GetNewID(typeof(Model.InformationProject_WorkSummaryReport));
                    report.WorkSummaryReportId = this.WorkSummaryReportId;
                    BLL.ProjectWorkSummaryReportService.AddWorkSummaryReport(report);
                    BLL.LogService.AddSys_Log(this.CurrUser, report.YearId.ToString(), report.WorkSummaryReportId, BLL.Const.ProjectWorkSummaryReportMenuId, BLL.Const.BtnAdd);
                  
                }
                else
                {
                    Alert.ShowInTop("该年度记录已存在", MessageBoxIcon.Warning);
                    return;
                }
            }
        }
        #endregion
    }
}