using BLL;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.TestRun.Produce
{
    public partial class InspectWanderAboutList : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }
        #region 数据绑定
        /// 
        /// 绑定数据
        /// 
        public void BindGrid()
        {
            //承包商
            string contractorStr = $"select NEWID() as Id,a.SubInspectId,a.ProjectId,b.ProjectName,b.ProjectCode,a.Contractor,a.Owner,a.ProcessPatent,1 as ConfirmType,'承包商' as ConfirmName,(select count (1) from ProduceRun_SubInspectTermItem as t where t.SubInspectId=a.SubInspectId and t.ContractorIsPass is null) as WaitConfirmNum,(select count (1) from ProduceRun_SubInspectTermItem as t where t.SubInspectId=a.SubInspectId and t.ContractorIsPass=1) as PassConfirmNum,(select count (1) from ProduceRun_SubInspectTermItem as t where t.SubInspectId=a.SubInspectId and t.ContractorIsPass=0) as FailConfirmNum,a.AddUser,a.AddTime,a.IsUnifyWanderAbout,(case IsUnifyWanderAbout when 1 then '是' else '否' end) as IsUnifyWanderAboutName from ProduceRun_SubInspectTerm as a inner join Base_Project as b on a.ProjectId=b.ProjectId where 1=1 and a.InspectionIsAllPass=1 and a.ProjectId='{this.CurrUser.LoginProjectId}' and a.Contractor='{this.CurrUser.UserId}' ";
            //业主
            string ownerStr = $"select NEWID() as Id,a.SubInspectId,a.ProjectId,b.ProjectName,b.ProjectCode,a.Contractor,a.Owner,a.ProcessPatent,2 as ConfirmType,'业主' as ConfirmName,(select count(1) from ProduceRun_SubInspectTermItem as t where t.SubInspectId = a.SubInspectId and t.OwnerIsPass is null) as WaitConfirmNum,(select count(1) from ProduceRun_SubInspectTermItem as t where t.SubInspectId = a.SubInspectId and t.OwnerIsPass = 1) as PassConfirmNum,(select count(1) from ProduceRun_SubInspectTermItem as t where t.SubInspectId = a.SubInspectId and t.OwnerIsPass = 0) as FailConfirmNum,a.AddUser,a.AddTime,a.IsUnifyWanderAbout,(case IsUnifyWanderAbout when 1 then '是' else '否' end) as IsUnifyWanderAboutName from ProduceRun_SubInspectTerm as a inner join Base_Project as b on a.ProjectId = b.ProjectId where 1=1 and a.InspectionIsAllPass = 1 and a.ProjectId = '{this.CurrUser.LoginProjectId}' and a.Owner = '{this.CurrUser.UserId}' ";
            //工艺专利商
            string processPatentStr = $"select NEWID() as Id,a.SubInspectId,a.ProjectId,b.ProjectName,b.ProjectCode,a.Contractor,a.Owner,a.ProcessPatent,3 as ConfirmType,'工艺专利商' as ConfirmName,(select count(1) from ProduceRun_SubInspectTermItem as t where t.SubInspectId = a.SubInspectId and t.ProcessPatentIsPass is null) as WaitConfirmNum,(select count(1) from ProduceRun_SubInspectTermItem as t where t.SubInspectId = a.SubInspectId and t.ProcessPatentIsPass = 1) as PassConfirmNum,(select count(1) from ProduceRun_SubInspectTermItem as t where t.SubInspectId = a.SubInspectId and t.ProcessPatentIsPass = 0) as FailConfirmNum,a.AddUser,a.AddTime,a.IsUnifyWanderAbout,(case IsUnifyWanderAbout when 1 then '是' else '否' end) as IsUnifyWanderAboutName from ProduceRun_SubInspectTerm as a inner join Base_Project as b on a.ProjectId = b.ProjectId where 1=1 and a.InspectionIsAllPass = 1 and a.InspectionIsAllPass = 1 and a.InspectionIsAllPass = 1 and a.ProjectId = '{this.CurrUser.LoginProjectId}' and a.ProcessPatent = '{this.CurrUser.UserId}' ";
            var allSql = $"select * from ({contractorStr} union all {ownerStr} union all {processPatentStr}) as t order by t.ConfirmType asc";
            List listStr = new List();
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(allSql);
            Grid1.RecordCount = tb.Rows.Count;
            var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = table;
            Grid1.DataBind();
        }
        /// 
        /// 分页
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        /// 
        /// 双击事件
        /// 
        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            btnMenuConfirmation_Click(null, null);
        }
        /// 
        /// 分页下拉框
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        /// 
        /// 排序
        /// 
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 按钮和事件
        /// 
        /// 查询
        /// 
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindGrid();
        }
        /// 
        /// 检查确认
        /// 
        protected void btnConfirmation_Click(object sender, EventArgs e)
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("请选择记录!", MessageBoxIcon.Warning);
                return;
            }
            if (Grid1.SelectedRowIndexArray.Length > 1)
            {
                Alert.ShowInTop("只可选择一条记录!", MessageBoxIcon.Warning);
                return;
            }
            PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("InspectWanderAboutConfirm.aspx?SubInspectId={0}&ConfirmType={1}", Grid1.DataKeys[Grid1.SelectedRowIndex][1].ToString(), Grid1.DataKeys[Grid1.SelectedRowIndex][2].ToString(), "编辑 - ")));
        }
        /// 
        /// 右击检查确认
        /// 
        protected void btnMenuConfirmation_Click(object sender, EventArgs e)
        {
            btnConfirmation_Click(null, null);
        }
        /// 
        /// 关闭
        /// 
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            BindGrid();
        }
        #endregion
    }
}