using Aspose.Words;
using BLL;
using Org.BouncyCastle.Asn1.Ocsp;
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 QualityProblemItem : PageBase
    {
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string projectId = Request.Params["projectId"];
                this.txtProject.Text=ProjectService.GetProjectNameByProjectId(projectId);
                Funs.DropDownPageSize(this.ddlPageSize);
                btnClose.OnClientClick = ActiveWindow.GetHideReference();
                ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
                // 绑定表格t
                BindGrid();
                this.Panel1.Title = "质量问题治理数据(" + BLL.UnitService.GetUnitNameByUnitId(BLL.Const.UnitId_CWCEC) + ")";
            }
        }
        /// 
        /// 绑定数据
        /// 
        private void BindGrid()
        {
            string strSql = @"SELECT chec.CheckControlCode,chec.CheckSite,chec.ProjectId,chec.unitId,cNProfessional.ProfessionalName,
                QualityQuestionType.QualityQuestionType as QuestionType,
                chec.checkman,chec.CheckDate,chec.DocCode,chec.submitman,chec.state,chec.CNProfessionalCode,
                unit.UnitName,unitWork.UnitWorkName+(case unitWork.ProjectType when '1' then '(建筑)' else '(安装)' end) as UnitWorkName 
                FROM Check_CheckControl chec
                left join Base_Unit unit on unit.unitId=chec.unitId
                left join Base_CNProfessional cNProfessional on cNProfessional.CNProfessionalId=chec.CNProfessionalCode
                left join WBS_UnitWork unitWork on unitWork.UnitWorkId = chec.UnitWorkId
                left join Base_QualityQuestionType QualityQuestionType on QualityQuestionType.QualityQuestionTypeId = chec.QuestionType
                WHERE 1=1  ";
            List listStr = new List();
            strSql += " AND chec.ProjectId = @ProjectId";
            listStr.Add(new SqlParameter("@ProjectId", Request.Params["projectId"]));
            strSql += " AND (chec.CheckDate>=@startTime or @startTime='') and (chec.CheckDate<=@endTime or @endTime='') ";
            listStr.Add(new SqlParameter("@startTime", !string.IsNullOrEmpty(txtStartTime.Text.Trim()) ? txtStartTime.Text.Trim() + " 00:00:00" : ""));
            listStr.Add(new SqlParameter("@endTime", !string.IsNullOrEmpty(txtEndTime.Text.Trim()) ? txtEndTime.Text.Trim() + "  23:59:59" : ""));
            if (dpHandelStatus.SelectedValue != "0")
            {
                if (dpHandelStatus.SelectedValue.Equals("1"))
                {
                    strSql += " AND (chec.state='5' or chec.state='6')";
                }
                else if (dpHandelStatus.SelectedValue.Equals("2"))
                {
                    strSql += " AND chec.state='7'";
                }
                else if (dpHandelStatus.SelectedValue.Equals("3"))
                {
                    strSql += " AND DATEADD(day,1,chec.LimitDate)< GETDATE() and chec.state<>5 and chec.state<>6 and chec.state<>7";
                }
                else if (dpHandelStatus.SelectedValue.Equals("4"))
                {
                    strSql += " AND DATEADD(day,1,chec.LimitDate)> GETDATE() and chec.state<>5 and chec.state<>6 and chec.state<>7";
                }
            }
            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 查询
        /// 
        /// 查询
        /// 
        /// 
        /// 
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            this.BindGrid();
        }
        #endregion
        #region 表排序、分页、关闭窗口
        /// 
        /// 分页
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            BindGrid();
        }
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            BindGrid();
        }
        /// 
        /// 分页显示条数下拉框
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        #endregion
        protected string ConvertImageUrlByImage(object registrationId)
        {
            string url = string.Empty;
            string httpUrl = string.Empty;
            var sysSet6 = (from x in Funs.DB.Sys_Set where x.SetName == "程序访问地址" select x).ToList().FirstOrDefault();
            if (sysSet6 != null)
            {
                httpUrl = sysSet6.SetValue;
            }
            if (registrationId != null)
            {
                IList sourlist = AttachFileService.GetBeforeFileList(registrationId.ToString(), BLL.Const.CheckListMenuId);
                if (sourlist != null && sourlist.Count > 0)
                {
                    string AttachUrl = "";
                    foreach (var item in sourlist)
                    {
                        if (!string.IsNullOrEmpty(item.AttachUrl) && item.AttachUrl.ToLower().EndsWith(".jpg") || item.AttachUrl.ToLower().EndsWith(".jpeg") || item.AttachUrl.ToLower().EndsWith(".png"))
                            AttachUrl += item.AttachUrl.TrimEnd(',') + ",";
                    }
                    url = BLL.UploadAttachmentService.ShowImage("../", AttachUrl.TrimEnd(','));
                }
            }
            return url;
        }
        protected string ConvertImgUrlByImage(object registrationId)
        {
            string url = string.Empty;
            string httpUrl = string.Empty;
            var sysSet6 = (from x in Funs.DB.Sys_Set where x.SetName == "程序访问地址" select x).ToList().FirstOrDefault();
            if (sysSet6 != null)
            {
                httpUrl = sysSet6.SetValue;
            }
            if (registrationId != null)
            {
                IList sourlist = AttachFileService.Getfilelist(registrationId.ToString() + "r", BLL.Const.CheckListMenuId);
                if (sourlist != null && sourlist.Count > 0)
                {
                    string AttachUrl = "";
                    foreach (var item in sourlist)
                    {
                        if (!string.IsNullOrEmpty(item.AttachUrl) && item.AttachUrl.ToLower().EndsWith(".jpg") || item.AttachUrl.ToLower().EndsWith(".jpeg") || item.AttachUrl.ToLower().EndsWith(".png"))
                            AttachUrl += item.AttachUrl.TrimEnd(',') + ",";
                    }
                    url = BLL.UploadAttachmentService.ShowImage("../", AttachUrl.TrimEnd(','));
                }
            }
            return url;
        }
        public string Convertstatus(Object code)
        {
            Model.Check_CheckControl checkControl = BLL.CheckControlService.GetCheckControl(code.ToString());
            if (checkControl.State.Equals("5") || checkControl.State.Equals("6"))
            {
                return "未确认";
            }
            else if (checkControl.State == Const.CheckControl_Complete)
            { //闭环
                return "已闭环";
            }
            //else if( checkControl.LimitDate> )
            else if (Convert.ToDateTime(checkControl.LimitDate).AddDays(1) < DateTime.Now)  //延期未整改
            {
                return "超期未整改";
            }
            else  //期内未整改
            {
                return "未整改";
            }
        }
    }
}