using BLL;
using System;
using System.Linq;
namespace FineUIPro.Web.HSSE.Hazard
{
    public partial class RoutingInspectionEdit : PageBase
    {
        #region 定义项
        /// 
        /// 主键
        /// 
        private string PatrolPlanId
        {
            get
            {
                return (string)ViewState["PatrolPlanId"];
            }
            set
            {
                ViewState["PatrolPlanId"] = value;
            }
        }
        private string HazardSelectedItemId
        {
            get
            {
                return (string)ViewState["HazardSelectedItemId"];
            }
            set
            {
                ViewState["HazardSelectedItemId"] = value;
            }
        }
        private string RoutingInspectionId
        {
            get
            {
                return (string)ViewState["RoutingInspectionId"];
            }
            set
            {
                ViewState["RoutingInspectionId"] = value;
            }
        }
        /// 
        /// 图片路径
        /// 
        public string ImageUrl
        {
            get
            {
                return (string)ViewState["ImageUrl"];
            }
            set
            {
                ViewState["ImageUrl"] = value;
            }
        }
        /// 
        /// 整改后附件路径
        /// 
        public string RectificationImageUrl
        {
            get
            {
                return (string)ViewState["RectificationImageUrl"];
            }
            set
            {
                ViewState["RectificationImageUrl"] = value;
            }
        }
        #endregion
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                if (!IsPostBack)
                {
                    this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
                    UserService.InitUsersDropDownList(this.drpPatrolMan, this.CurrUser.LoginProjectId, true, string.Empty);
                    UserService.InitUsersDropDownList(this.drpRiskMan, this.CurrUser.LoginProjectId, true, string.Empty);
                    this.PatrolPlanId = Request.Params["PatrolPlanId"];
                    this.hdCheckManId.Text = this.CurrUser.UserId;
                    if (!string.IsNullOrEmpty(this.PatrolPlanId))
                    {
                        Model.Hazard_RoutingInspection routingInspection = BLL.Hazard_RoutingInspectionService.GetRoutingInspectionByPatrolPlanId(this.PatrolPlanId);
                        Model.Hazard_PatrolPlan plan = BLL.Hazard_PatrolPlanService.GetPatrolPlanByPatrolPlanId(this.PatrolPlanId);
                        HazardSelectedItemId = plan.HazardSelectedItemId;
                        Model.Hazard_HazardSelectedItem item = BLL.Hazard_HazardSelectedItemService.GetHazardSelectedItemByHazardSelectedItemId(plan.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 (routingInspection != null)
                        {
                            RoutingInspectionId = routingInspection.RoutingInspectionId;
                            if (!string.IsNullOrEmpty(routingInspection.PatrolManId))
                            {
                                this.drpPatrolMan.SelectedValue = routingInspection.PatrolManId;
                            }
                            if (routingInspection.PatrolTime != null)
                            {
                                this.txtPatrolTime.Text = string.Format("{0:yyyy-MM-dd}", routingInspection.PatrolTime);
                            }
                            if (routingInspection.PatrolResult != null)
                            {
                                this.rblPatrolResult.SelectedValue = routingInspection.PatrolResult.ToString();
                            }
                            this.txtControlMeasures.Text = routingInspection.ControlMeasures;
                            this.txtDealReason.Text = routingInspection.DealReason;
                            if (!string.IsNullOrEmpty(routingInspection.RiskManId))
                            {
                                this.drpRiskMan.SelectedValue = routingInspection.RiskManId;
                            }
                        }
                        else
                        {
                            this.drpPatrolMan.SelectedValue = this.CurrUser.UserId;
                            this.txtPatrolTime.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
                            this.drpRiskMan.SelectedValue = this.CurrUser.UserId;
                        }
                    }
                }
            }
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.ProjectRiskControlMenuId, BLL.Const.BtnSave))
            {
                if (this.drpPatrolMan.SelectedValue == BLL.Const._Null)
                {
                    ShowNotify("请选择巡检人!", MessageBoxIcon.Warning);
                    return;
                }
                if (this.rblPatrolResult.SelectedValue != "-1" && this.drpRiskMan.SelectedValue == BLL.Const._Null)
                {
                    ShowNotify("请选择风险责任人!", MessageBoxIcon.Warning);
                    return;
                }
                SaveData(true);
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
            else
            {
                Alert.ShowInTop("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                return;
            }
        }
        /// 
        /// 保存数据
        /// 
        /// 
        private void SaveData(bool isClosed)
        {
            Model.Hazard_PatrolPlan p = BLL.Hazard_PatrolPlanService.GetPatrolPlanByPatrolPlanId(this.PatrolPlanId);
            Model.Hazard_HazardSelectedItem selectedItem = BLL.Hazard_HazardSelectedItemService.GetHazardSelectedItemByHazardSelectedItemId(p.HazardSelectedItemId);
            Model.Hazard_RoutingInspection routingInspection = new Model.Hazard_RoutingInspection();
            routingInspection.HazardSelectedItemId = this.HazardSelectedItemId;
            routingInspection.PatrolManId = this.drpPatrolMan.SelectedValue;
            routingInspection.PatrolTime = Funs.GetNewDateTimeOrNow(this.txtPatrolTime.Text.Trim());
            routingInspection.PatrolResult = Convert.ToInt32(this.rblPatrolResult.SelectedValue);
            routingInspection.OldRiskLevel = p.HazardLevel;
            routingInspection.ControlMeasures = this.txtControlMeasures.Text.Trim();
            routingInspection.DealReason = this.txtDealReason.Text.Trim();
            routingInspection.RiskManId = this.drpRiskMan.SelectedValue;
            routingInspection.PatrolPlanId = this.PatrolPlanId;
            if (!string.IsNullOrEmpty(RoutingInspectionId))
            {
                routingInspection.RoutingInspectionId = RoutingInspectionId;
                BLL.Hazard_RoutingInspectionService.UpdateRoutingInspection(routingInspection);
                BLL.LogService.AddSys_Log(this.CurrUser, routingInspection.PatrolResult.ToString(), routingInspection.RoutingInspectionId, BLL.Const.ProjectRiskControlMenuId, BLL.Const.BtnModify);
            }
            else
            {
                routingInspection.RoutingInspectionId = SQLHelper.GetNewID();
                this.RoutingInspectionId = routingInspection.RoutingInspectionId;
                BLL.Hazard_RoutingInspectionService.AddRoutingInspection(routingInspection);
                BLL.LogService.AddSys_Log(this.CurrUser, routingInspection.PatrolResult.ToString(), routingInspection.RoutingInspectionId, BLL.Const.ProjectRiskControlMenuId, BLL.Const.BtnAdd);
            }
            p.CheckDate = routingInspection.PatrolTime;
            p.State = "1";   //已巡检
            if (routingInspection.PatrolTime > p.LimitCheckDate)
            {
                p.State = "2";   //超期巡检
            }
            BLL.Hazard_PatrolPlanService.UpdatePatrolPlan(p);
            var ps = BLL.Hazard_PatrolPlanService.GetBeforePatrolPlansByCheckDate(this.HazardSelectedItemId, p.CheckDate.Value);
            foreach (var item in ps)
            {
                item.CheckDate = p.CheckDate;
                item.State = "3";   //超期未巡检
                BLL.Hazard_PatrolPlanService.UpdatePatrolPlan(item);
            }
            if (isClosed)
            {
                string str = string.Empty;
                if (this.rblPatrolResult.SelectedValue == "0")   //原状态
                {
                    //巡检计划
                    Model.Hazard_PatrolPlan plan = new Model.Hazard_PatrolPlan();
                    plan.PatrolPlanId = SQLHelper.GetNewID();
                    plan.HazardSelectedItemId = this.HazardSelectedItemId;
                    plan.HazardLevel = p.HazardLevel;
                    plan.DutyPerson = this.drpRiskMan.SelectedValue;
                    plan.Days = p.Days;
                    plan.CheckStartDate = p.CheckStartDate;
                    plan.LimitCheckDate = p.LimitCheckDate.Value.AddDays(p.Days.Value);
                    plan.State = "0";
                    Model.Hazard_PatrolPlan oldPlan = BLL.Hazard_PatrolPlanService.GetPatrolPlanByHazardSelectedItemIdAndLimitCheckDate(this.HazardSelectedItemId, plan.LimitCheckDate);
                    if (oldPlan == null)
                    {
                        BLL.Hazard_PatrolPlanService.AddPatrolPlan(plan);
                    }
                }
                else if (this.rblPatrolResult.SelectedValue == "1")  //二次评估
                {
                    //二次评价记录
                    Model.Hazard_RiskEvaluationRecord record = new Model.Hazard_RiskEvaluationRecord();
                    record.LECItemRecordId = SQLHelper.GetNewID();
                    record.DataId = this.HazardSelectedItemId;
                    record.DataType = "Project";
                    record.Evaluatorld = this.drpRiskMan.SelectedValue;
                    //record.EvaluatorDate = DateTime.Now;     生成待办记录
                    //record.L = selectedItem.HazardJudge_L;
                    //record.E = selectedItem.HazardJudge_E;
                    //record.C = selectedItem.HazardJudge_C;
                    //record.D = selectedItem.HazardJudge_D;
                    //record.RiskLevel = p.HazardLevel;
                    //record.ControlMeasures = selectedItem.ControlMeasures;
                    //record.Remark = selectedItem.Remark;
                    BLL.RiskEvaluationRecordService.AddRiskEvaluationRecord(record);
                    if (this.drpRiskMan.SelectedValue == this.CurrUser.UserId)
                    {
                        str = "1|" + this.HazardSelectedItemId + "|" + record.LECItemRecordId;
                    }
                }
                else if (this.rblPatrolResult.SelectedValue == "2")  //下整改单
                {
                    //巡检计划
                    Model.Hazard_PatrolPlan plan = new Model.Hazard_PatrolPlan();
                    plan.PatrolPlanId = SQLHelper.GetNewID();
                    plan.HazardSelectedItemId = this.HazardSelectedItemId;
                    plan.HazardLevel = p.HazardLevel;
                    plan.DutyPerson = this.drpRiskMan.SelectedValue;
                    plan.Days = p.Days;
                    plan.CheckStartDate = p.CheckStartDate;
                    plan.LimitCheckDate = p.LimitCheckDate.Value.AddDays(p.Days.Value);
                    plan.State = "0";
                    Model.Hazard_PatrolPlan oldPlan = BLL.Hazard_PatrolPlanService.GetPatrolPlanByHazardSelectedItemIdAndLimitCheckDate(this.HazardSelectedItemId, plan.LimitCheckDate);
                    if (oldPlan == null)
                    {
                        BLL.Hazard_PatrolPlanService.AddPatrolPlan(plan);
                    }
                    str = "2|" + this.HazardSelectedItemId;
                }
                else if (this.rblPatrolResult.SelectedValue == "-1")  //消除
                {
                    selectedItem.IsStart = false;
                    selectedItem.State = "0";
                    BLL.Hazard_HazardSelectedItemService.UpdateHazardSelectedItem(selectedItem);
                }
                //if (string.IsNullOrEmpty(Request.Params["Main"]))
                //{
                //    PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(str)
                // + ActiveWindow.GetHidePostBackReference());
                //}
                //else
                //{
                //    PageContext.RegisterStartupScript(String.Format("window.close();"));
                //}
            }
            Project_HSSEData_HSSEService.StatisticalData(this.CurrUser.LoginProjectId, Project_HSSEData_HSSEService.HSSEDateType.SecurityRisk);
        }
        #endregion
        #region 附件上传
        /// 
        /// 上传附件资源
        /// 
        /// 
        /// 
        protected void btnAttachUrl_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.RoutingInspectionId))
            {
                SaveData(false);
            }
            string edit = "0";
            if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.ProjectRiskControlMenuId, BLL.Const.BtnSave))
            {
                edit = "1";
                DateTime date = Funs.GetNewDateTimeOrNow(this.txtPatrolTime.Text.Trim());
                string dateStr = date.Year.ToString() + date.Month.ToString();
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("~/AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/RoutingInspection/" + dateStr + "&menuId={1}&edit={2}", this.RoutingInspectionId, Const.ProjectRiskControlMenuId, edit)));
            }
        }
        #endregion
    }
}