using BLL;
using System;
using System.Linq;
using System.Web;
namespace FineUIPro.Web.HSSE.Accident
{
    public partial class AccidentReportEdit : PageBase
    {
        #region 定义项
        /// 
        /// 主键
        /// 
        private string AccidentReportId
        {
            get
            {
                return (string)ViewState["AccidentReportId"];
            }
            set
            {
                ViewState["AccidentReportId"] = 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;
                Funs.FineUIPleaseSelect(this.drpAccidentDegree);
                if (!string.IsNullOrEmpty(this.ProjectId))
                {
                    this.lblProjectName.Text = BLL.ProjectService.GetProjectByProjectId(this.ProjectId).ProjectName;
                }
                int l = 18 - this.lblProjectName.Text.Length;
                this.lblProjectName.Margin = "5 0 0 " + (10 + 7 * l).ToString();
                this.txtAccidentReportName.Text = "管理体系文件-施工管理";
                this.InitDropDownList();
                this.AccidentReportId = Request.Params["AccidentReportId"];
                this.txtWorkingHoursLoss.Text = "0";
                this.txtEconomicLoss.Text = "0";
                this.txtEconomicOtherLoss.Text = "0";
                if (!string.IsNullOrEmpty(this.AccidentReportId))
                {
                    var accidentReport = BLL.AccidentReportService.GetAccidentReportById(this.AccidentReportId);
                    if (accidentReport != null)
                    {
                        this.ProjectId = accidentReport.ProjectId;
                        if (this.ProjectId != this.CurrUser.LoginProjectId)
                        {
                            this.InitDropDownList();
                        }
                        this.txtAccidentReportCode.Text = BLL.CodeRecordsService.ReturnCodeByDataId(this.AccidentReportId);
                        //this.drpAccidentReportType.SelectedValue = accidentReport.AccidentReportType;
                        if (!string.IsNullOrEmpty(accidentReport.UnitId))
                        {
                            this.drpUnitId.SelectedValue = accidentReport.UnitId;
                        }
                        if(!string.IsNullOrEmpty(accidentReport.AccidentDegree))
                        {
                            this.drpAccidentDegree.SelectedValue = accidentReport.AccidentDegree;
                        }
                        if (!string.IsNullOrEmpty(accidentReport.CompileMan))
                        {
                            this.drpCompileMan.SelectedValue = accidentReport.CompileMan;
                        }
                        if (accidentReport.CompileDate != null)
                        {
                            this.txtCompileDate.Text = string.Format("{0:yyyy-MM-dd}", accidentReport.CompileDate);
                        }
                        this.txtAccidentReportName.Text = accidentReport.AccidentReportName;
                        if (!string.IsNullOrEmpty(accidentReport.AccidentTypeId))
                        {
                            this.drpAccidentTypeId.SelectedValue = accidentReport.AccidentTypeId;
                        }
                        if (accidentReport.AccidentTypeId == "1" || accidentReport.AccidentTypeId == "2" || accidentReport.AccidentTypeId == "3")
                        {
                            if (accidentReport.IsNotConfirm == true)  //待定事故
                            {
                                this.trConfirm.Hidden = true;
                                this.trNotConfirm.Hidden = false;
                                this.drpUnitId2.SelectedValue = accidentReport.UnitId;
                                this.txtNotConfirmWorkingHoursLoss.Text = accidentReport.NotConfirmWorkingHoursLoss;
                                this.txtNotConfirmEconomicLoss.Text = accidentReport.NotConfirmEconomicLoss;
                                this.txtNotConfirmEconomicOtherLoss.Text = accidentReport.NotConfirmEconomicOtherLoss;
                            }
                            else  //确定事故
                            {
                                this.trConfirm.Hidden = false;
                                this.trNotConfirm.Hidden = true;
                                this.drpUnitId.SelectedValue = accidentReport.UnitId;
                                if (accidentReport.WorkingHoursLoss != null)
                                {
                                    this.txtWorkingHoursLoss.Text = Convert.ToString(accidentReport.WorkingHoursLoss);
                                }
                                if (accidentReport.EconomicLoss != null)
                                {
                                    this.txtEconomicLoss.Text = Convert.ToString(accidentReport.EconomicLoss);
                                }
                                if (accidentReport.EconomicOtherLoss != null)
                                {
                                    this.txtEconomicOtherLoss.Text = Convert.ToString(accidentReport.EconomicOtherLoss);
                                }
                            }
                        }
                        else
                        {
                            if (accidentReport.WorkingHoursLoss != null)
                            {
                                this.txtWorkingHoursLoss.Text = Convert.ToString(accidentReport.WorkingHoursLoss);
                            }
                            if (accidentReport.EconomicLoss != null)
                            {
                                this.txtEconomicLoss.Text = Convert.ToString(accidentReport.EconomicLoss);
                            }
                            if (accidentReport.EconomicOtherLoss != null)
                            {
                                this.txtEconomicOtherLoss.Text = Convert.ToString(accidentReport.EconomicOtherLoss);
                            }
                        }
                        if (accidentReport.IsNotConfirm != null)
                        {
                            this.rbIsNotConfirm.Hidden = false;
                            if (accidentReport.IsNotConfirm == true)   //待定
                            {
                                this.rbIsNotConfirm.SelectedValue = "True";
                            }
                            else
                            {
                                this.rbIsNotConfirm.SelectedValue = "False";
                            }
                        }
                        this.txtAbstract.Text = accidentReport.Abstract;
                        if (accidentReport.AccidentDate != null)
                        {
                            this.txtAccidentDate.Text = string.Format("{0:yyyy-MM-dd}", accidentReport.AccidentDate);
                        }
                        this.txtWorkArea.Text = accidentReport.WorkArea;
                        if (accidentReport.PeopleNum != null)
                        {
                            this.txtPeopleNum.Text = Convert.ToString(accidentReport.PeopleNum);
                        }
                        this.txtReportMan.Text = accidentReport.ReportMan;
                        this.txtReporterUnit.Text = accidentReport.ReporterUnit;
                        if (accidentReport.ReportDate != null)
                        {
                            this.txtReportDate.Text = string.Format("{0:yyyy-MM-dd}", accidentReport.ReportDate);
                        }
                        this.txtProcessDescription.Text = accidentReport.ProcessDescription;
                        this.txtEmergencyMeasures.Text = accidentReport.EmergencyMeasures;
                    }
                }
                else
                {
                    this.txtAccidentReportCode.Text = BLL.CodeRecordsService.ReturnCodeByMenuIdProjectId(BLL.Const.ProjectAccidentReportMenuId, this.ProjectId, this.CurrUser.UnitId);
                    //var codeTemplateRule = BLL.ProjectData_CodeTemplateRuleService.GetProjectData_CodeTemplateRuleByMenuIdProjectId(BLL.Const.ProjectAccidentReportMenuId, this.ProjectId);
                    //if (codeTemplateRule != null)
                    //{
                    //    this.txtFileContents.Text = HttpUtility.HtmlDecode(codeTemplateRule.Template);
                    //}
                    this.drpCompileMan.SelectedValue = this.CurrUser.UserId;
                    this.txtCompileDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                }
            }
        }
        #endregion
        /// 
        /// 事故类型选择事件
        /// 
        /// 
        /// 
        protected void drpAccidentTypeId_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpAccidentTypeId.SelectedValue == "1" || this.drpAccidentTypeId.SelectedValue == "2" || this.drpAccidentTypeId.SelectedValue == "3")
            {
                this.frIsNotConfirm.Hidden = false;
                if (this.drpAccidentTypeId.SelectedValue == "1")
                {
                    this.txtWorkingHoursLoss.Readonly = true;
                }
                else
                {
                    this.txtWorkingHoursLoss.Readonly = false;
                }
            }
            else
            {
                this.txtWorkingHoursLoss.Readonly = false;
                this.frIsNotConfirm.Hidden = true;
            }
        }
        /// 
        /// 是否待定选择事件
        /// 
        /// 
        /// 
        protected void rbIsNotConfirm_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.rbIsNotConfirm.SelectedValue == "True")
            {
                this.trConfirm.Hidden = true;
                this.trNotConfirm.Hidden = false;
            }
            else
            {
                this.trConfirm.Hidden = false;
                this.trNotConfirm.Hidden = true;
                if (this.drpAccidentTypeId.SelectedValue == "1")
                {
                    this.txtWorkingHoursLoss.Readonly = true;
                }
            }
        }
        /// 
        ///  初始化下拉框
        /// 
        private void InitDropDownList()
        {            
            BLL.UnitService.InitUnitDropDownList(this.drpUnitId, this.ProjectId, true);
            BLL.UnitService.InitUnitDropDownList(this.drpUnitId2, this.ProjectId, true);
            BLL.UserService.InitUserDropDownList(this.drpCompileMan, this.ProjectId, true);
            BLL.ConstValue.InitConstValueDropDownList(this.drpAccidentTypeId, BLL.ConstValue.Group_AccidentReportRegistration, true);            
        }
        #region 保存、提交
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool res = SaveData(BLL.Const.BtnSave);
            if (res)
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
            }
        }
        /// 
        /// 数据保存
        /// 
        /// 
        private bool SaveData(string type)
        {
            if (this.drpAccidentTypeId.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择事故类型!", MessageBoxIcon.Warning);
                return false;
            }
            if (this.drpAccidentDegree.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请选择严重程度!", MessageBoxIcon.Warning);
                return false;
            }
            if (this.rbIsNotConfirm.SelectedValue == "True")
            {
                if (this.drpUnitId2.SelectedValue == BLL.Const._Null)
                {
                    Alert.ShowInTop("请选择事故责任单位!", MessageBoxIcon.Warning);
                    return false;
                }
            }
            else
            {
                if (this.drpUnitId.SelectedValue == BLL.Const._Null)
                {
                    Alert.ShowInTop("请选择事故责任单位!", MessageBoxIcon.Warning);
                    return false;
                }
            }
            Model.Accident_AccidentReport accidentReport = new Model.Accident_AccidentReport
            {
                ProjectId = this.ProjectId,
                AccidentReportCode = this.txtAccidentReportCode.Text.Trim(),
                //accidentReport.AccidentReportType = this.drpAccidentReportType.SelectedValue;
            };
            if (this.drpCompileMan.SelectedValue != BLL.Const._Null)
            {
                accidentReport.CompileMan = this.drpCompileMan.SelectedValue;
            }
            if (this.drpAccidentDegree.SelectedValue != BLL.Const._Null)
            {
                accidentReport.AccidentDegree = this.drpAccidentDegree.SelectedValue;
            }
            accidentReport.CompileDate = Funs.GetNewDateTime(this.txtCompileDate.Text.Trim());
            accidentReport.States = BLL.Const.State_2;
            accidentReport.AccidentReportName = this.txtAccidentReportName.Text.Trim();
            if (this.drpAccidentTypeId.SelectedValue != BLL.Const._Null)
            {
                accidentReport.AccidentTypeId = this.drpAccidentTypeId.SelectedValue;
                if (this.rbIsNotConfirm.Hidden == false)
                {
                    accidentReport.IsNotConfirm = Convert.ToBoolean(this.rbIsNotConfirm.SelectedValue);
                }
            }
            accidentReport.Abstract = this.txtAbstract.Text.Trim();
            accidentReport.AccidentDate = Funs.GetNewDateTime(this.txtAccidentDate.Text.Trim());
            accidentReport.WorkArea = this.txtWorkArea.Text;
            accidentReport.PeopleNum = Funs.GetNewIntOrZero(this.txtPeopleNum.Text.Trim());
            if (accidentReport.IsNotConfirm == true)  //待定事故
            {
                accidentReport.UnitId = this.drpUnitId2.SelectedValue;
                accidentReport.NotConfirmWorkingHoursLoss = this.txtNotConfirmWorkingHoursLoss.Text.Trim();
                accidentReport.NotConfirmEconomicLoss = this.txtNotConfirmEconomicLoss.Text.Trim();
                accidentReport.NotConfirmEconomicOtherLoss = this.txtNotConfirmEconomicOtherLoss.Text.Trim();
            }
            else
            {
                accidentReport.UnitId = this.drpUnitId.SelectedValue;
                accidentReport.WorkingHoursLoss = Funs.GetNewDecimalOrZero(this.txtWorkingHoursLoss.Text.Trim());
                accidentReport.EconomicLoss = Funs.GetNewDecimalOrZero(this.txtEconomicLoss.Text.Trim());
                accidentReport.EconomicOtherLoss = Funs.GetNewDecimalOrZero(this.txtEconomicOtherLoss.Text.Trim());
            }
            accidentReport.ReportMan = this.txtReportMan.Text.Trim();
            accidentReport.ReporterUnit = this.txtReporterUnit.Text.Trim();
            accidentReport.ReportDate = Funs.GetNewDateTime(this.txtReportDate.Text.Trim());
            accidentReport.ProcessDescription = this.txtProcessDescription.Text.Trim();
            accidentReport.EmergencyMeasures = this.txtEmergencyMeasures.Text.Trim();
            if (!string.IsNullOrEmpty(this.AccidentReportId))
            {
                accidentReport.AccidentReportId = this.AccidentReportId;
                BLL.AccidentReportService.UpdateAccidentReport(accidentReport);
                BLL.LogService.AddSys_Log(this.CurrUser, this.txtAccidentReportCode.Text, this.AccidentReportId, BLL.Const.ProjectAccidentReportMenuId, Const.BtnModify);
                Model.Accident_AccidentReport a = BLL.AccidentReportService.GetAccidentReportById(this.AccidentReportId);                
            }
            else
            {
                this.AccidentReportId = SQLHelper.GetNewID(typeof(Model.Accident_AccidentReport));
                accidentReport.AccidentReportId = this.AccidentReportId;
                if (accidentReport.IsNotConfirm == true)  //待定事故
                {
                    accidentReport.NotConfirmed = true;
                }
                BLL.AccidentReportService.AddAccidentReport(accidentReport);
                BLL.LogService.AddSys_Log(this.CurrUser, this.txtAccidentReportCode.Text, this.AccidentReportId, BLL.Const.ProjectAccidentReportMenuId, Const.BtnAdd);                
            }
            Project_HSSEData_HSSEService.StatisticalData(this.CurrUser.LoginProjectId, Project_HSSEData_HSSEService.HSSEDateType.WorkingHours);
            Project_HSSEData_HSSEService.StatisticalData(this.CurrUser.LoginProjectId, Project_HSSEData_HSSEService.HSSEDateType.AccidentEvent);
            return true;
        }
        #endregion
        #region 附件上传
        /// 
        /// 上传附件
        /// 
        /// 
        /// 
        protected void btnAttachUrl_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.AccidentReportId))
            {
                if (this.drpAccidentTypeId.SelectedValue == BLL.Const._Null)
                {
                    Alert.ShowInTop("请选择事故类型!", MessageBoxIcon.Warning);
                    return;
                }
                if (this.rbIsNotConfirm.SelectedValue == "True")
                {
                    if (this.drpUnitId2.SelectedValue == BLL.Const._Null)
                    {
                        Alert.ShowInTop("请选择事故责任单位!", MessageBoxIcon.Warning);
                        return;
                    }
                }
                else
                {
                    if (this.drpUnitId.SelectedValue == BLL.Const._Null)
                    {
                        Alert.ShowInTop("请选择事故责任单位!", MessageBoxIcon.Warning);
                        return;
                    }
                }
                this.SaveData(BLL.Const.BtnSave);
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("~/AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/AccidentReportAttachUrl&menuId={1}", this.AccidentReportId, BLL.Const.ProjectAccidentReportMenuId)));
        }
        #endregion
    }
}