using BLL;
using FineUIPro.Web.BaseInfo;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace FineUIPro.Web.DataShow
{
    public partial class QualityControlPoint : 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.CommonService.GetThisUnitId()) + ")";
            }
        }
        #endregion

        #region 加载页面
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindGrid()
        {
            string strSql = @"select ProjectId,ProjectCode, 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 from Base_Project where  (ProjectAttribute='GONGCHENG' OR  ProjectAttribute IS NULL ) and  (isDelete is null or isDelete =0) and  ProjectState =1 ";
            List<SqlParameter> listStr = new List<SqlParameter>();
            string cpara = string.Empty;
            if (this.drpProject.SelectedValue != Const._Null)
            {
                strSql += " AND projectId = @projectId";  ///状态为已完成
                listStr.Add(new SqlParameter("@projectId", this.drpProject.SelectedValue));
            }

            //if (!string.IsNullOrEmpty(this.txtStartTime.Text))
            //{
            //    strSql += " AND h.RegisterDate >=@StartTime";
            //    listStr.Add(new SqlParameter("@StartTime", this.txtStartTime.Text));

            //    cpara += " AND c.RegisterDate >=" + this.txtStartTime.Text;
            //}
            //if (!string.IsNullOrEmpty(this.txtEndTime.Text))
            //{
            //    strSql += " AND h.RegisterDate <=@EndTime";
            //    listStr.Add(new SqlParameter("@EndTime", this.txtEndTime.Text));

            //    cpara += " AND c.RegisterDate <=" + 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();
        }
        #endregion

        #region 查询
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            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

        /// <summary>
        /// 总工时数
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        protected int Count1(object projectId)
        {
            int cout1 = 0;
            if (projectId != null)
            {
                cout1 = getcount(projectId.ToString(), "S");
            }
            return cout1;
        }

        /// <summary>
        /// 损失工时数
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        protected int Count2(object projectId)
        {
            int cout1 = 0;
            if (projectId != null)
            {
                cout1 = getcount(projectId.ToString(), "A");
            }
            return cout1;
        }

        /// <summary>
        /// 安全工时数
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        protected int Count3(object projectId)
        {
            int cout1 = 0;
            if (projectId != null)
            {
                cout1= getcount(projectId.ToString(), "B");
            }
            return cout1;
        }

        protected int Count4(object projectId)
        {
            int cout1 = 0;
            if (projectId != null)
            {
                cout1 = getcount(projectId.ToString(), "C");
            }
            return cout1;
        }
        /// <summary>
        /// 定义变量
        /// </summary>
        private static IQueryable<Model.WBS_BreakdownProject> getDataLists = from x in Funs.DB.WBS_BreakdownProject
                                                                             select x;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        private int getcount(string projectId, string level)
        {
             int count = 0;
            count = getDataLists.Where(x => x.ProjectId == projectId && x.Class.Contains(level)).Count();
            return count;

        }
    }
}