using BLL;
using System;
using System.Linq;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.HSSE.Hazard
{
    public partial class HazardInfoEdit : PageBase
    {
        #region 定义变量
        /// 
        /// 主键
        /// 
        public string HazardSelectedItemId
        {
            get
            {
                return (string)ViewState["HazardSelectedItemId"];
            }
            set
            {
                ViewState["HazardSelectedItemId"] = value;
            }
        }
        #endregion
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnClose.OnClientClick = ActiveWindow.GetHideReference();
                ////权限按钮方法
                this.GetButtonPower();
                BLL.ConstValue.InitConstValueDropDownList(this.ddlHelperMethod, ConstValue.Group_0006, true);
                BLL.RiskLevelService.InitRiskLevelDropDownList(this.ddlHazardLevel, true);
                BLL.UserService.InitFlowOperateControlUserDropDownList(this.drpDutyPerson, this.CurrUser.LoginProjectId, string.Empty, true);
                this.txtPlanExistDate.Enabled = false;
                this.txtCheckStartDate.Enabled = false;
                this.drpDutyPerson.Enabled = false;
                this.HazardSelectedItemId = Request.Params["HazardSelectedItemId"];
                if (!string.IsNullOrEmpty(this.HazardSelectedItemId))
                {
                    Model.Hazard_HazardSelectedItem item = BLL.Hazard_HazardSelectedItemService.GetHazardSelectedItemByHazardSelectedItemId(this.HazardSelectedItemId);
                    if (item != null)
                    {
                        var q = BLL.HazardListService.GetHazardListById(item.HazardId);
                        if (q != null)
                        {
                            this.txtHazardCode.Text = q.HazardCode;
                            this.txtHazardItems.Text = item.HazardItems;
                            this.txtDefectsType.Text = item.DefectsType;
                            this.txtMayLeadAccidents.Text = item.MayLeadAccidents;
                            if (item.HelperMethod != "null")
                            {
                                this.ddlHelperMethod.SelectedValue = item.HelperMethod;
                            }
                            if (item.HazardJudge_L != null)
                            {
                                this.txtHazardJudge_L.Text = Convert.ToString(item.HazardJudge_L);
                            }
                            if (item.HazardJudge_E != null)
                            {
                                this.txtHazardJudge_E.Text = Convert.ToString(item.HazardJudge_E);
                            }
                            if (item.HazardJudge_C != null)
                            {
                                this.txtHazardJudge_C.Text = Convert.ToString(item.HazardJudge_C);
                            }
                            if (item.HazardJudge_D != null)
                            {
                                this.txtHazardJudge_D.Text = Convert.ToString(item.HazardJudge_D);
                            }
                            if (item.HazardLevel != "0")
                            {
                                this.ddlHazardLevel.SelectedValue = item.HazardLevel;
                            }
                            this.txtControlMeasures.Text = item.ControlMeasures;
                            if (item.IsStart == true)
                            {
                                this.ckbIsStart.Checked = true;
                                this.txtPlanExistDate.Enabled = true;
                                this.txtCheckStartDate.Enabled = true;
                                this.drpDutyPerson.Enabled = true;
                            }
                            if (item.CheckStartDate != null)
                            {
                                this.txtCheckStartDate.Text = string.Format("{0:yyyy-MM-dd}", item.CheckStartDate);
                            }
                            if (!string.IsNullOrEmpty(item.DutyPerson))
                            {
                                this.drpDutyPerson.SelectedValue = item.DutyPerson;
                            }
                            if (item.PlanExistDate != null)
                            {
                                this.txtPlanExistDate.Text = string.Format("{0:yyyy-MM-dd}", item.PlanExistDate);
                            }
                        }
                    }
                }
            }
        }
        #endregion
        #region 保存
        /// 
        /// 保存数据
        /// 
        private void SaveData()
        {
            if (this.ckbIsStart.Checked)
            {
                if (string.IsNullOrEmpty(this.txtCheckStartDate.Text.Trim()))
                {
                    Alert.ShowInTop("巡检启动时间不能为空!", MessageBoxIcon.Warning);
                    return;
                }
                if (this.drpDutyPerson.SelectedValue == BLL.Const._Null)
                {
                    Alert.ShowInTop("请选择风险责任人!", MessageBoxIcon.Warning);
                    return;
                }
                if (this.ddlHazardLevel.SelectedValue == BLL.Const._Null)
                {
                    Alert.ShowInTop("请选择危险级别!", MessageBoxIcon.Warning);
                    return;
                }
            }
            Model.Hazard_HazardSelectedItem item = BLL.Hazard_HazardSelectedItemService.GetHazardSelectedItemByHazardSelectedItemId(this.HazardSelectedItemId);
            if (item != null)
            {
                item.IsStart = this.ckbIsStart.Checked;
                item.CheckStartDate = Funs.GetNewDateTime(this.txtCheckStartDate.Text.Trim());
                if (this.drpDutyPerson.SelectedValue != BLL.Const._Null)
                {
                    item.DutyPerson = this.drpDutyPerson.SelectedValue;
                }
                item.PlanExistDate = Funs.GetNewDateTime(this.txtPlanExistDate.Text.Trim());
                item.State = "1";  //存在
            }
            BLL.Hazard_HazardSelectedItemService.UpdateHazardSelectedItem(item);
            if (item.IsStart == true)
            {
                //巡检计划
                Model.Hazard_PatrolPlan plan = new Model.Hazard_PatrolPlan();
                plan.PatrolPlanId = SQLHelper.GetNewID();
                plan.HazardSelectedItemId = this.HazardSelectedItemId;
                plan.HazardLevel = item.HazardLevel;
                plan.DutyPerson = this.drpDutyPerson.SelectedValue;
                Model.Base_RiskLevel level = BLL.RiskLevelService.GetRiskLevel(item.HazardLevel);
                if (level != null)
                {
                    plan.Days = level.Days;
                }
                plan.CheckStartDate = item.CheckStartDate;
                plan.LimitCheckDate = item.CheckStartDate.Value.AddDays(level.Days.Value);
                plan.State = "0";
                BLL.Hazard_PatrolPlanService.AddPatrolPlan(plan);
            }
            Project_HSSEData_HSSEService.StatisticalData(this.CurrUser.LoginProjectId, Project_HSSEData_HSSEService.HSSEDateType.SecurityRisk);
            BLL.LogService.AddSys_Log(this.CurrUser, item.HazardItems, item.HazardSelectedItemId, BLL.Const.ProjectHazardInfoMenuId, Const.BtnModify);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        #endregion
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            if (Request.Params["value"] == "0")
            {
                return;
            }
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.ProjectHazardInfoMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion
        protected void txtHazardJudge_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtHazardJudge_L.Text) && !string.IsNullOrEmpty(this.txtHazardJudge_E.Text) && !string.IsNullOrEmpty(this.txtHazardJudge_C.Text))
            {
                decimal D = decimal.Parse(this.txtHazardJudge_L.Text) * decimal.Parse(this.txtHazardJudge_E.Text) * decimal.Parse(this.txtHazardJudge_C.Text);
                this.txtHazardJudge_D.Text = D.ToString("0.0");
                var riskLevel = Funs.DB.Base_RiskLevel.FirstOrDefault(x => (!x.MinValue.HasValue || x.MinValue <= D) && (D <= x.MaxValue || !x.MaxValue.HasValue));
                if (riskLevel != null)
                {
                    this.ddlHazardLevel.SelectedValue = riskLevel.RiskLevelId;
                }
            }
        }
        protected void ckbIsStart_CheckedChanged(object sender, CheckedEventArgs e)
        {
            if (this.ckbIsStart.Checked)
            {
                this.txtPlanExistDate.Enabled = true;
                this.txtCheckStartDate.Enabled = true;
                this.drpDutyPerson.Enabled = true;
            }
            else
            {
                this.txtPlanExistDate.Enabled = false;
                this.txtPlanExistDate.Text = string.Empty;
                this.txtCheckStartDate.Enabled = false;
                this.txtCheckStartDate.Text = string.Empty;
                this.drpDutyPerson.Enabled = false;
                this.drpDutyPerson.SelectedValue = BLL.Const._Null;
            }
        }
    }
}