using BLL;
using System;
using System.Linq;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.ZHGL.Information
{
    public partial class WorkSummaryReportEdit : PageBase
    {
        #region 定义变量
        /// 
        /// 主键
        /// 
        public string WorkSummaryReportId
        {
            get
            {
                return (string)ViewState["WorkSummaryReportId"];
            }
            set
            {
                ViewState["WorkSummaryReportId"] = 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.ddlUnitId.Readonly = true;
                string unitId = Request.Params["UnitId"];
                string year = Request.QueryString["Year"];
                this.WorkSummaryReportId = Request.Params["WorkSummaryReportId"];
                if (!string.IsNullOrEmpty(this.WorkSummaryReportId))
                {
                    var report = BLL.WorkSummaryReportService.GetWorkSummaryReportById(this.WorkSummaryReportId);
                    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();
                        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.btnCopy.Hidden = false;
                    this.ddlUnitId.SelectedValue = unitId;
                    this.ddlYearId.SelectedValue = year;
                    this.txtReportDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                    #region 获取项目上报数据
                    decimal? safeLaborTime = 0;
                    string safetyObjectives = string.Empty;
                    string accidentSituation = string.Empty;
                    string awards = string.Empty;
                    string workDevelopment = string.Empty;
                    string personnelTraining = string.Empty;
                    string governanceSituation = string.Empty;
                    string managementActivity = string.Empty;
                    string workExperience = string.Empty;
                    string countermeasures = string.Empty;
                    string nextYearWorkPlan = string.Empty;
                    string jobSuggestion = 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_WorkSummaryReport.FirstOrDefault(x => x.ProjectId == item.ProjectId && x.YearId == date);
                        if (report != null)
                        {
                            safeLaborTime += report.SafeLaborTime;
                            safetyObjectives += item.ProjectName + ":\r\n" + report.SafetyObjectives + "\r\n";
                            accidentSituation += item.ProjectName + ":\r\n" + report.AccidentSituation + "\r\n";
                            awards += item.ProjectName + ":\r\n" + report.Awards + "\r\n";
                            workDevelopment += item.ProjectName + ":\r\n" + report.WorkDevelopment + "\r\n";
                            personnelTraining += item.ProjectName + ":\r\n" + report.PersonnelTraining + "\r\n";
                            governanceSituation += item.ProjectName + ":\r\n" + report.GovernanceSituation + "\r\n";
                            managementActivity += item.ProjectName + ":\r\n" + report.ManagementActivity + "\r\n";
                            workExperience += item.ProjectName + ":\r\n" + report.WorkExperience + "\r\n";
                            countermeasures += item.ProjectName + ":\r\n" + report.Countermeasures + "\r\n";
                            nextYearWorkPlan += item.ProjectName + ":\r\n" + report.NextYearWorkPlan + "\r\n";
                            jobSuggestion += item.ProjectName + ":\r\n" + report.JobSuggestion + "\r\n";
                        }
                    }
                    this.txtSafeLaborTime.Text = safeLaborTime.ToString();
                    this.txtSafetyObjectives.Text = safetyObjectives;
                    this.txtAccidentSituation.Text = accidentSituation;
                    this.txtAwards.Text = awards;
                    this.txtWorkDevelopment.Text = workDevelopment;
                    this.txtPersonnelTraining.Text = personnelTraining;
                    this.txtGovernanceSituation.Text = governanceSituation;
                    this.txtManagementActivity.Text = managementActivity;
                    this.txtWorkExperience.Text = workExperience;
                    this.txtCountermeasures.Text = countermeasures;
                    this.txtNextYearWorkPlan.Text = nextYearWorkPlan;
                    this.txtJobSuggestion.Text = jobSuggestion;
                    #endregion
                }
            }
        }
        #endregion
        #region 保存数据
        private void Save(string type)
        {
            Model.Information_WorkSummaryReport workSummaryReport = new Model.Information_WorkSummaryReport();
            if (this.ddlUnitId.SelectedValue != BLL.Const._Null)
            {
                workSummaryReport.UnitId = this.ddlUnitId.SelectedValue;
            }
            else
            {
                ShowNotify("请选择单位!", MessageBoxIcon.Warning);
                return;
            }
            if (this.ddlYearId.SelectedValue != BLL.Const._Null)
            {
                workSummaryReport.YearId = Funs.GetNewIntOrZero(this.ddlYearId.SelectedValue);
            }
            else
            {
                ShowNotify("请选择年度!", MessageBoxIcon.Warning);
                return;
            }
            workSummaryReport.ResponsiblePerson = this.txtResponsiblePerson.Text.Trim();
            workSummaryReport.ResponsiblePersonTel = this.txtResponsiblePersonTel.Text.Trim();
            workSummaryReport.ContactPerson = this.txtContactPerson.Text.Trim();
            workSummaryReport.ContactPersonTel = this.txtContactPersonTel.Text.Trim();
            workSummaryReport.ReportDate = Funs.GetNewDateTime(this.txtReportDate.Text.Trim());
            workSummaryReport.SafeLaborTime = Funs.GetNewDecimal(this.txtSafeLaborTime.Text);
            workSummaryReport.SafetyObjectives = this.txtSafetyObjectives.Text.Trim();
            workSummaryReport.AccidentSituation = this.txtAccidentSituation.Text.Trim();
            workSummaryReport.Awards = this.txtAwards.Text.Trim();
            workSummaryReport.WorkDevelopment = this.txtWorkDevelopment.Text.Trim();
            workSummaryReport.PersonnelTraining = this.txtPersonnelTraining.Text.Trim();
            workSummaryReport.GovernanceSituation = this.txtGovernanceSituation.Text.Trim();
            workSummaryReport.ManagementActivity = this.txtManagementActivity.Text.Trim();
            workSummaryReport.WorkExperience = this.txtWorkExperience.Text.Trim();
            workSummaryReport.Countermeasures = this.txtCountermeasures.Text.Trim();
            workSummaryReport.NextYearWorkPlan = this.txtNextYearWorkPlan.Text.Trim();
            workSummaryReport.JobSuggestion = this.txtJobSuggestion.Text.Trim();
            if (string.IsNullOrEmpty(this.WorkSummaryReportId))
            {
                var s = BLL.WorkSummaryReportService.GetWorkSummaryReportByUnitIdAndYear(this.ddlUnitId.SelectedValue, Funs.GetNewIntOrZero(this.ddlYearId.SelectedValue));
                if (s != null)
                {
                    ShowNotify("该单位的该年度的该安全管理工作总结报告已经存在,不能重复编制!", MessageBoxIcon.Warning);
                    return;
                }
                else
                {
                    this.WorkSummaryReportId = SQLHelper.GetNewID(typeof(Model.Information_WorkSummaryReport));
                    workSummaryReport.WorkSummaryReportId = this.WorkSummaryReportId;
                    workSummaryReport.UpState = BLL.Const.UpState_2;
                    BLL.WorkSummaryReportService.AddWorkSummaryReport(workSummaryReport);
                    BLL.LogService.AddSys_Log(this.CurrUser, this.ddlYearId.SelectedText, workSummaryReport.WorkSummaryReportId, BLL.Const.WorkSummaryReportMenuId, BLL.Const.BtnAdd);
                }
            }
            else
            {
                workSummaryReport.WorkSummaryReportId = this.WorkSummaryReportId;
                workSummaryReport.UpState = BLL.Const.UpState_2;
                BLL.WorkSummaryReportService.UpdateWorkSummaryReport(workSummaryReport);
                BLL.LogService.AddSys_Log(this.CurrUser, this.ddlYearId.SelectedText, workSummaryReport.WorkSummaryReportId, BLL.Const.WorkSummaryReportMenuId, BLL.Const.BtnModify);
            }
            if (type == "updata")     //保存并上报
            {
                if (workSummaryReport.UpState == BLL.Const.UpState_2)
                {
                    string code = CNCECHSSEWebService.UpWorkSummaryReport(workSummaryReport.WorkSummaryReportId, 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");
        }
        /// 
        /// 提交按钮
        /// 
        /// 
        /// 
        //protected void btnSubmit_Click(object sender, EventArgs e)
        //{
        //    Save("submit");
        //}
        #endregion        
        #region 关闭办理流程窗口
        /// 
        /// 关闭办理流程窗口
        /// 
        /// 
        /// 
        //protected void Window1_Close(object sender, WindowCloseEventArgs e)
        //{
        //    Model.Information_WorkSummaryReport report = BLL.WorkSummaryReportService.GetWorkSummaryReportById(this.WorkSummaryReportId);
        //    if (report.HandleMan == this.CurrUser.UserId)
        //    {
        //        this.btnSave.Hidden = false;
        //        this.btnSubmit.Hidden = false;
        //    }
        //    else
        //    {
        //        this.btnSave.Hidden = true;
        //        this.btnSubmit.Hidden = true;
        //    }
        //}
        #endregion
        #region 复制上个年度数据
        /// 
        /// 复制上个年度数据
        /// 
        /// 
        /// 
        protected void btnCopy_Click(object sender, EventArgs e)
        {
            int lastYear = 0;
            int year = Convert.ToInt32(this.ddlYearId.SelectedValue);
            lastYear = year - 1;
            Model.Information_WorkSummaryReport workSummaryReport = BLL.WorkSummaryReportService.GetWorkSummaryReportByUnitIdAndYear(this.ddlUnitId.SelectedValue, lastYear);
            if (workSummaryReport != null)
            {
                Model.Information_WorkSummaryReport newReport = new Model.Information_WorkSummaryReport();
                this.WorkSummaryReportId = SQLHelper.GetNewID(typeof(Model.Information_WorkSummaryReport));
                newReport.WorkSummaryReportId = this.WorkSummaryReportId;
                newReport.UnitId = this.ddlUnitId.SelectedValue;
                newReport.YearId = Funs.GetNewIntOrZero(this.ddlYearId.SelectedValue);
                newReport.SafeLaborTime = workSummaryReport.SafeLaborTime;
                newReport.SafetyObjectives = workSummaryReport.SafetyObjectives;
                newReport.AccidentSituation = workSummaryReport.AccidentSituation;
                newReport.Awards = workSummaryReport.Awards;
                newReport.WorkDevelopment = workSummaryReport.WorkDevelopment;
                newReport.PersonnelTraining = workSummaryReport.PersonnelTraining;
                newReport.GovernanceSituation = workSummaryReport.GovernanceSituation;
                newReport.ManagementActivity = workSummaryReport.ManagementActivity;
                newReport.WorkExperience = workSummaryReport.WorkExperience;
                newReport.Countermeasures = workSummaryReport.Countermeasures;
                newReport.NextYearWorkPlan = workSummaryReport.NextYearWorkPlan;
                newReport.JobSuggestion = workSummaryReport.JobSuggestion;
                newReport.UpState = BLL.Const.UpState_2;
                BLL.WorkSummaryReportService.AddWorkSummaryReport(newReport);
                GetValues(newReport.WorkSummaryReportId);
            }
        }
        /// 
        /// 赋值
        ///  
        private void GetValues(string workSummaryReportId)
        {
            var report = BLL.WorkSummaryReportService.GetWorkSummaryReportById(workSummaryReportId);
            if (report != null)
            {
                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
    }
}