using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
namespace FineUIPro.Web.ZHGL.Information
{
    public partial class ReportSubmit : PageBase
    {
        #region 定义变量
        /// 
        /// 报表类别
        /// 
        public string Type
        {
            get
            {
                return (string)ViewState["Type"];
            }
            set
            {
                ViewState["Type"] = value;
            }
        }
        /// 
        /// 报表主键Id
        /// 
        public string Id
        {
            get
            {
                return (string)ViewState["Id"];
            }
            set
            {
                ViewState["Id"] = value;
            }
        }
        #endregion
        #region 加载页面
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Type = Request.Params["Type"];
                this.Id = Request.Params["Id"];
                this.BindGrid();     
            }
        }
        #endregion
        #region 人员下拉框绑定数据
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            string unitId = Const.UnitId_CWCEC;
            string strSql = @"SELECT UserId,UserName,UserCode,role.RoleName"
                    + @" FROM Sys_User AS users"
                    + @" LEFT JOIN Sys_Role AS role ON users.RoleId= role.RoleId"
                    + @" WHERE users.IsPost=1 AND role.IsAuditFlow=1 AND users.IsOffice =1 AND UnitId ='" + unitId + "'";
            List listStr = new List();
            if (!string.IsNullOrEmpty(this.txtUserName.Text.Trim()))
            {
                strSql += " AND (UserName LIKE @Name OR UserCode LIKE @Name OR role.RoleName LIKE @Name)";
                listStr.Add(new SqlParameter("@Name", "%" + this.txtUserName.Text.Trim() + "%"));
            }
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            Grid1.RecordCount = tb.Rows.Count;
            var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = table;
            Grid1.DataBind();
        }
        #region 查询
        /// 
        /// 下拉框查询
        /// 
        /// 
        /// 
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            this.drpHandleMan.Values = null;
            this.BindGrid();
        }
        #endregion
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string handleMan = this.drpHandleMan.Value;
            string handleStates = (this.cbNext.SelectedValue == "0") ? BLL.Const.HandleState_4 : BLL.Const.HandleState_3;
            if (!string.IsNullOrEmpty(handleMan) && handleMan != Const._Null)
            {
               
                #region 企业安全数据统计月报
                if (Type == "MillionsMonthlyReport")//企业安全数据统计月报
                {
                    var report = BLL.MillionsMonthlyReportService.GetMillionsMonthlyReportByMillionsMonthlyReportId(Id);
                    if (report != null)
                    {
                        SetFlowData(Const.MillionsMonthlyReportMenuId, this.Id, handleMan);
                        report.HandleState = handleStates;
                        report.HandleMan = handleMan;
                        BLL.MillionsMonthlyReportService.UpdateMillionsMonthlyReport(report);
                    }
                }
                #endregion
                #region 职工伤亡事故原因分析
                if (Type == "AccidentCauseReport")//职工伤亡事故原因分析
                {
                    var report = BLL.AccidentCauseReportService.GetAccidentCauseReportByAccidentCauseReportId(Id);
                    if (report != null)
                    {
                        SetFlowData(Const.AccidentCauseReportMenuId, this.Id, handleMan);
                        report.HandleState = handleStates;
                        report.HandleMan = handleMan;
                        BLL.AccidentCauseReportService.UpdateAccidentCauseReport(report);
                    }
                }
                #endregion
                #region 安全生产数据季报
                if (Type == "SafetyQuarterlyReport")//安全生产数据季报
                {
                    var report = BLL.SafetyQuarterlyReportService.GetSafetyQuarterlyReportById(Id);
                    if (report != null)
                    {
                        SetFlowData(Const.SafetyQuarterlyReportMenuId, this.Id, handleMan);
                        report.HandleState = handleStates;
                        report.HandleMan = handleMan;
                        BLL.SafetyQuarterlyReportService.UpdateSafetyQuarterlyReport(report);
                    }
                }
                #endregion
                #region 应急演练开展情况季报表
                if (Type == "DrillConductedQuarterlyReport")//应急演练开展情况季报表
                {
                    var report = BLL.DrillConductedQuarterlyReportService.GetDrillConductedQuarterlyReportById(Id);
                    if (report != null)
                    {
                        SetFlowData(Const.DrillConductedQuarterlyReportMenuId, this.Id, handleMan);
                        report.HandleState = handleStates;
                        report.HandleMan = handleMan;
                        BLL.DrillConductedQuarterlyReportService.UpdateDrillConductedQuarterlyReport(report);
                    }
                }
                #endregion
                #region 应急演练工作计划半年报
                if (Type == "DrillPlanHalfYearReport")//应急演练工作计划半年报
                {
                    var report = BLL.DrillPlanHalfYearReportService.GetDrillPlanHalfYearReportById(this.Id);
                    if (report != null)
                    {
                        SetFlowData(Const.DrillPlanHalfYearReportMenuId, this.Id, handleMan);
                        report.HandleState = handleStates;
                        report.HandleMan = handleMan;
                        BLL.DrillPlanHalfYearReportService.UpdateDrillPlanHalfYearReport(report);
                    }
                }
                #endregion
                #region 安全管理工作总结报告
                //if (Type== "WorkSummaryReport")//安全管理工作总结报告
                //{
                //    var report = BLL.WorkSummaryReportService.GetWorkSummaryReportById(this.Id);
                //    if (report != null)
                //    {
                //        SetFlowData(Const.WorkSummaryReportMenuId, this.Id, handleMan);
                //        report.HandleState = handleStates;
                //        report.HandleMan = handleMan;
                //        BLL.WorkSummaryReportService.UpdateWorkSummaryReport(report);
                //    }
                //}
                #endregion
                #region 质量管理工作总结报告
                //if (Type == "QualityWorkSummaryReport")//质量管理工作总结报告
                //{
                //    var report = BLL.QualityWorkSummaryReportService.GetQualityWorkSummaryReportById(this.Id);
                //    if (report != null)
                //    {
                //        SetFlowData(Const.QualityWorkSummaryReportMenuId, this.Id, handleMan);
                //        report.HandleState = handleStates;
                //        report.HandleMan = handleMan;
                //        BLL.QualityWorkSummaryReportService.UpdateQualityWorkSummaryReport(report);
                //    }
                //}
                #endregion
                #region 中央企业安全生产治本攻坚三年行动工作台账
                if (Type == "ActionWorkLedger")//中央企业安全生产治本攻坚三年行动工作台账
                {
                    var report = BLL.ActionWorkLedgerService.GetActionWorkLedgerById(Id);
                    if (report != null)
                    {
                        SetFlowData(Const.ActionWorkLedgerMenuId, this.Id, handleMan);
                        report.HandleState = handleStates;
                        report.HandleMan = handleMan;
                        BLL.ActionWorkLedgerService.UpdateActionWorkLedger(report);
                    }
                }
                #endregion
                #region 中央企业安全生产管理评价工作调度台账
                if (Type == "SafetyProductionEvaluation")//中央企业安全生产管理评价工作调度台账
                {
                    var report = BLL.SafetyProductionEvaluationService.GetSafetyProductionEvaluationById(Id);
                    if (report != null)
                    {
                        SetFlowData(Const.SafetyProductionEvaluationMenuId, this.Id, handleMan);
                        report.HandleState = handleStates;
                        report.HandleMan = handleMan;
                        BLL.SafetyProductionEvaluationService.UpdateSafetyProductionEvaluation(report);
                    }
                }
                #endregion
                ShowNotify("提交成功!", MessageBoxIcon.Success);
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
            else
            {
                Alert.ShowInParent("请选择办理人!", MessageBoxIcon.Warning);
            }
        }
        #endregion
        /// 
        /// 
        /// 
        /// 
        /// 
        private void SetFlowData(string menuId, string dataId,string handleMan)
        {
            var getIsExitOperate = Funs.DB.ProjectData_FlowOperate.FirstOrDefault(x => x.DataId == this.Id);
            if (getIsExitOperate == null)  //首次生成审批记录
            {
                ////编制人添加记录信息
                Model.ProjectData_FlowOperate newFlow = new Model.ProjectData_FlowOperate
                {
                    MenuId = menuId,
                    DataId = this.Id,
                    OperaterId = this.CurrUser.UserId,
                    OperaterTime = DateTime.Now,
                    IsClosed = true,
                    State = BLL.Const.State_1,
                    Opinion = this.txtOpinion.Text
                };
                BLL.ProjectDataFlowSetService.AddProjectData_FlowOperate(newFlow);
            }
            ////更新 当前人要处理的意见
            var updateUnFlowOperate = Funs.DB.ProjectData_FlowOperate.FirstOrDefault(x => x.MenuId == menuId && x.DataId == dataId && (x.IsClosed == false || !x.IsClosed.HasValue));
            if (updateUnFlowOperate != null)
            {
                updateUnFlowOperate.OperaterTime = DateTime.Now;
                updateUnFlowOperate.Opinion = this.txtOpinion.Text;
                updateUnFlowOperate.IsClosed = true;
                BLL.ProjectDataFlowSetService.UpdateFlowOperateOpinion(updateUnFlowOperate);
            }
            string states =  (this.cbNext.SelectedValue == "0") ? BLL.Const.State_2: BLL.Const.State_1;
            ////增加 下一步办理信息
            Model.ProjectData_FlowOperate newdateUnFlowOperate = new Model.ProjectData_FlowOperate
            {
                MenuId = menuId,
                DataId = this.Id,
                OperaterId = handleMan,
                IsClosed = false,
                State = states,
            };
            BLL.ProjectDataFlowSetService.AddProjectData_FlowOperate(newdateUnFlowOperate);
        }
        
        /// 
        /// 
        /// 
        /// 
        /// 
        protected void cbNext_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbNext.SelectedValue == "0")
            {
                this.drpHandleMan.Label = "上报人";
                var getFData = Funs.DB.ProjectData_FlowOperate.FirstOrDefault(x => x.DataId == Id && x.SortIndex == 1);
                if (getFData != null)
                {
                    this.drpHandleMan.Value = getFData.OperaterId;
                }
            }
            else
            {
                this.drpHandleMan.Label = "办理人";
                this.drpHandleMan.Value = string.Empty;
            }
        }
    }
}