using Aspose.Words;
using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;

namespace FineUIPro.Web.DataShow
{
    public partial class Check : PageBase
    {

        #region 加载页面
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Funs.DropDownPageSize(this.ddlPageSize);
                ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
                BLL.ProjectService.InitProjectDropDownList(this.drpProject, true);
                // 绑定表格t
                BindGrid();
                this.Panel1.Title = "安全监督检查数据(" + BLL.UnitService.GetUnitNameByUnitId(BLL.Const.UnitId_TCC) + ")";
            }
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindGrid()
        {
            if (this.rbCom.SelectedValue == "1" || this.rbCom.SelectedValue == "2" || this.rbCom.SelectedValue == "3")
            {
                string strSql = @"SELECT S.SuperviseCheckReportId AS ID,S.SuperviseCheckReportCode,S.CheckDate,S.ProjectId,P.ProjectName  +  ( select top 1 case when ProjectState2 is null then '' when ProjectState2 !=3 then '<span style=""color:red"" >'+ ConstText+'<span/>'  else ''end from Sys_Const where GroupId ='ProjectState' and (ProjectState2 is null or ProjectState2=ConstValue )) ProjectName,S.UnitId,u.UnitName,S.CheckTeam,S.EvaluationResult,S.AttachUrl,S.IsIssued,case S.CheckType when '1' then '企业负责人带班检查' when '2' then '企业综合检查' when '3' then '企业专项检查' else '' end as CheckTypeName
						   FROM dbo.Supervise_SuperviseCheckReport AS S
                           LEFT JOIN dbo.Base_Project AS P ON P.ProjectId=S.ProjectId
                           LEFT JOIN dbo.Base_Unit AS U ON U.UnitId=S.UnitId
                            WHERE  (ProjectAttribute is null or ProjectAttribute = 'GONGCHENG') and  (isDelete is null or isDelete =0)  ";
                List<SqlParameter> listStr = new List<SqlParameter>();
                strSql += " AND  S.CheckType= @CheckType";
                listStr.Add(new SqlParameter("@CheckType", this.rbCom.SelectedValue));
                if (!string.IsNullOrEmpty(this.txtStartTime.Text))
                {
                    strSql += " AND  S.CheckDate >=@StartTime";
                    listStr.Add(new SqlParameter("@StartTime", this.txtStartTime.Text));
                }
                if (!string.IsNullOrEmpty(this.txtEndTime.Text))
                {
                    strSql += " AND  S.CheckDate <=@EndTime";
                    listStr.Add(new SqlParameter("@EndTime", this.txtEndTime.Text));
                }
                SqlParameter[] parameter = listStr.ToArray();
                DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);

                Grid1.RecordCount = tb.Rows.Count;
                //tb = GetFilteredTable(Grid1.FilteredData, tb);
                var table = this.GetPagedDataTable(Grid1, tb);

                Grid1.DataSource = table;
                Grid1.DataBind();
            }
            ///项目负责人带班检查
            else if (this.rbCom.SelectedValue == "4")
            {
                string strSql = @"select C.ProjectLeaderCheckId AS ID,C.UnitIds,C.ProjectId,P.ProjectName  +  ( select top 1 case when ProjectState2 is null then '' when ProjectState2 !=3 then '<span style=""color:red"" >'+ ConstText+'<span/>'  else ''end from Sys_Const where GroupId ='ProjectState' and (ProjectState2 is null or ProjectState2=ConstValue )) ProjectName,C.LeaderIds,C.LeaderNames AS CheckTeam,C.CheckDate
                        ,UnitName= STUFF((SELECT ',' + UnitName FROM dbo.Base_Unit where PATINDEX('%,' + RTRIM(UnitId) + ',%',',' +C.UnitIds + ',')>0 FOR XML PATH('')), 1, 1,'')
                        ,CheckTeam= STUFF((SELECT ',' + UserName FROM dbo.Sys_User where PATINDEX('%,' + RTRIM(UserId) + ',%',',' +C.LeaderIds + ',')>0 FOR XML PATH('')), 1, 1,'')
                        from Check_ProjectLeaderCheck AS C
                        LEFT JOIN Base_Project AS P ON C.ProjectId =P.ProjectId
                            where   (P.ProjectAttribute is null or P.ProjectAttribute = 'GONGCHENG') and  (P.isDelete is null or P.isDelete =0) ";
                List<SqlParameter> listStr = new List<SqlParameter>();
                if (this.drpProject.SelectedValue != Const._Null)
                {
                    strSql += " AND  C.ProjectId =@ProjectId";
                    listStr.Add(new SqlParameter("@ProjectId", this.drpProject.SelectedValue));
                }
                if (!string.IsNullOrEmpty(this.txtStartTime.Text))
                {
                    strSql += " AND  C.CheckDate >=@StartTime";
                    listStr.Add(new SqlParameter("@StartTime", this.txtStartTime.Text));
                }
                if (!string.IsNullOrEmpty(this.txtEndTime.Text))
                {
                    strSql += " AND C.CheckDate <=@EndTime";
                    listStr.Add(new SqlParameter("@EndTime", this.txtEndTime.Text));
                }

                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();
                this.Grid1.Columns[1].Hidden = false;
            }
            ///项目专业检查
            else if (this.rbCom.SelectedValue == "5")
            {
                string strSql = @"select C.HazardRegisterId AS ID,C.ResponsibleUnit,C.ProjectId,P.ProjectName  +  ( select top 1 case when ProjectState2 is null then '' when ProjectState2 !=3 then '<span style=""color:red"" >'+ ConstText+'<span/>'  else ''end from Sys_Const where GroupId ='ProjectState' and (ProjectState2 is null or ProjectState2=ConstValue )) ProjectName,C.CheckManId,U.UserName AS CheckTeam,C.CheckTime AS CheckDate,Ut.UnitName
                            from HSSE_Hazard_HazardRegister AS C
                            LEFT JOIN Base_Project AS P ON C.ProjectId =P.ProjectId
                            LEFT JOIN Sys_User AS U ON C.CheckManId=U.UserId
                            LEFT JOIN Base_Unit AS Ut ON C.ResponsibleUnit=Ut.UnitId
                            where  (P.ProjectAttribute is null or P.ProjectAttribute = 'GONGCHENG') and  (P.isDelete is null or P.isDelete =0) ";
                List<SqlParameter> listStr = new List<SqlParameter>();
                if (this.drpProject.SelectedValue != Const._Null)
                {
                    strSql += " AND  C.ProjectId =@ProjectId";
                    listStr.Add(new SqlParameter("@ProjectId", this.drpProject.SelectedValue));
                }
                if (!string.IsNullOrEmpty(this.txtStartTime.Text))
                {
                    strSql += " AND  C.CheckTime >=@StartTime";
                    listStr.Add(new SqlParameter("@StartTime", this.txtStartTime.Text));
                }
                if (!string.IsNullOrEmpty(this.txtEndTime.Text))
                {
                    strSql += " AND C.CheckTime <=@EndTime";
                    listStr.Add(new SqlParameter("@EndTime", this.txtEndTime.Text));
                }

                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();
                this.Grid1.Columns[1].Hidden = false;
            }
            ///项目专项目检查
            else if (this.rbCom.SelectedValue == "6")
            {
                string strSql = @"select C.CheckSpecialId AS ID,C.ProjectId,P.ProjectName  +  ( select top 1 case when ProjectState2 is null then '' when ProjectState2 !=3 then '<span style=""color:red"" >'+ ConstText+'<span/>'  else ''end from Sys_Const where GroupId ='ProjectState' and (ProjectState2 is null or ProjectState2=ConstValue )) ProjectName, ISNULL(C.PartInPersonNames,'') AS CheckTeam,C.CheckTime AS CheckDate
                        ,UnitName= STUFF((SELECT ',' + UnitName FROM dbo.Base_Unit where PATINDEX('%,' + RTRIM(UnitId) + ',%',',' +(C.PartInUnits) + ',')>0 FOR XML PATH('')), 1, 1,'')
                        from Check_CheckSpecial AS C
                        LEFT JOIN Base_Project AS P ON C.ProjectId =P.ProjectId
                        where  (P.ProjectAttribute is null or P.ProjectAttribute = 'GONGCHENG') and  (P.isDelete is null or P.isDelete =0) ";
                List<SqlParameter> listStr = new List<SqlParameter>();
                if (this.drpProject.SelectedValue != Const._Null)
                {
                    strSql += " AND  C.ProjectId =@ProjectId";
                    listStr.Add(new SqlParameter("@ProjectId", this.drpProject.SelectedValue));
                }
                if (!string.IsNullOrEmpty(this.txtStartTime.Text))
                {
                    strSql += " AND  C.CheckTime >=@StartTime";
                    listStr.Add(new SqlParameter("@StartTime", this.txtStartTime.Text));
                }
                if (!string.IsNullOrEmpty(this.txtEndTime.Text))
                {
                    strSql += " AND C.CheckTime <=@EndTime";
                    listStr.Add(new SqlParameter("@EndTime", this.txtEndTime.Text));
                }

                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();
                this.Grid1.Columns[1].Hidden = false;
            }
        }
        #endregion

        #region 查询
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            if (this.rbCom.SelectedValue == "4" || this.rbCom.SelectedValue == "5" || this.rbCom.SelectedValue == "6")
            {
                this.drpProject.Hidden = false;
            }
            else
            {
                this.drpProject.Hidden = true;
            }
            this.BindGrid();
        }
        #endregion

        #region 表排序、分页、关闭窗口
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            BindGrid();
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            BindGrid();
        }

        /// <summary>
        /// 分页显示条数下拉框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }

        /// <summary>
        /// 关闭弹出窗
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            BindGrid();
        }
        #endregion

        #region Grid双击事件 编辑
        /// <summary>
        /// Grid行双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            EditData();
        }


        /// <summary>
        /// 
        /// </summary>
        private void EditData()
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning);
                return;
            }
            if (this.rbCom.SelectedValue == "1"|| this.rbCom.SelectedValue == "2" || this.rbCom.SelectedValue == "3")
            {
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("../ZHGL/Supervise/SuperviseCheckReportEdit.aspx?SuperviseCheckReportId={0}&type=-1", Grid1.SelectedRowID, "查看 - ")));
            }          
            else if (this.rbCom.SelectedValue == "4")
            {
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("../HSSE/Check/ProjectLeaderCheckView.aspx?ProjectLeaderCheckId={0}", Grid1.SelectedRowID, "查看 - ")));
            }
            else if (this.rbCom.SelectedValue == "5")
            {
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("../HSSE/HiddenInspection/HiddenRectificationView.aspx?HazardRegisterId={0}", Grid1.SelectedRowID, "查看 - ")));
            }
            else if (this.rbCom.SelectedValue == "6")
            {
                PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("../HSSE/Check/CheckSpecialView.aspx?CheckSpecialId={0}", Grid1.SelectedRowID, "查看 - ")));
            }
        }
        #endregion
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnView_Click(object sender, EventArgs e)
        {
            EditData();
        }
    }
}