using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using AspNet = System.Web.UI.WebControls;
namespace FineUIPro.Web.HSSE.Technique
{
    public partial class HazardListOut : PageBase
    {
        #region 定义变量
        /// 
        /// 主键
        /// 
        public string IsCompany
        {
            get
            {
                return (string)ViewState["IsCompany"];
            }
            set
            {
                ViewState["IsCompany"] = value;
            }
        }       
        #endregion
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Funs.DropDownPageSize(this.ddlPageSize);
                this.IsCompany = Request.Params["IsCompany"];
                this.BindGrid();
            }
        }
        #endregion
        #region 绑定Grid
        /// 
        /// 绑定Grid
        /// 
        private void BindGrid()
        {
            string strSql = @"SELECT H.HazardId, ST.HazardListTypeCode AS STHazardListTypeCode,ST.HazardListTypeName AS STHazardListTypeName,T.HazardListTypeCode,T.HazardListTypeName,
                    H.HazardCode,H.HazardItems,H.DefectsType,H.MayLeadAccidents,H.HelperMethod,H.HazardJudge_L,H.HazardJudge_E,H.HazardJudge_C,H.HazardJudge_D,Const0007.ConstText AS HazardLevel,H.ControlMeasures
                    FROM Technique_HazardList AS H
                    LEFT JOIN Technique_HazardListType AS T ON T.HazardListTypeId=H.HazardListTypeId
                    LEFT JOIN Technique_HazardListType AS ST ON T.SupHazardListTypeId=ST.HazardListTypeId
                    LEFT JOIN Sys_Const AS Const0007 ON Const0007.ConstValue = H.HazardLevel and Const0007.GroupId='0007'
                    WHERE 1=1 ";
            List listStr = new List();
            //if (!string.IsNullOrEmpty(this.HazardCode.Text.Trim()))
            //{
            //    strSql += " AND HazardCode LIKE @HazardCode";
            //    listStr.Add(new SqlParameter("@HazardCode", "%" + this.HazardCode.Text.Trim() + "%"));
            //}
            //if (!string.IsNullOrEmpty(this.HazardListTypeCode.Text.Trim()))
            //{
            //    strSql += " AND HazardListTypeCode LIKE @HazardListTypeCode";
            //    listStr.Add(new SqlParameter("@HazardListTypeCode", "%" + this.HazardListTypeCode.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();
        }
        #endregion
        
        /// 
        /// 导出
        /// 
        /// 
        /// 
        protected void btnOut_Click(object sender, EventArgs e)
        {
            Response.ClearContent();
            string filename = Funs.GetNewFileName();
            Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("危险源清单" + filename, System.Text.Encoding.UTF8) + ".xls");
            Response.ContentType = "application/excel";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            this.Grid1.PageSize = Grid1.RecordCount;
            BindGrid();
            Response.Write(GetGridTableHtmlPage(Grid1));
            Response.End();
        }
        #region 导出方法
        /// 
        /// 导出方法
        /// 
        /// 
        /// 
        public static string GetGridTableHtmlPage(Grid grid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("");
            sb.Append("");
            sb.Append("");
            foreach (GridColumn column in grid.Columns)
            {
                sb.AppendFormat("| {0}", column.HeaderText);
            }
            sb.Append(" | 
");
            foreach (GridRow row in grid.Rows)
            {
                sb.Append("");
                foreach (GridColumn column in grid.Columns)
                {
                    string html = row.Values[column.ColumnIndex].ToString();
                    if (column.ColumnID == "tfNumber" && (row.FindControl("lbNumber") as AspNet.Label) != null)
                    {
                        html = (row.FindControl("lbNumber") as AspNet.Label).Text;
                    }
                    if (column.ColumnID == "tfSTHazardListTypeCode" && (row.FindControl("lbSTHazardListTypeCode") as AspNet.Label) != null)
                    {
                        html = (row.FindControl("lbSTHazardListTypeCode") as AspNet.Label).Text;
                    }
                    if (column.ColumnID == "tfSTHazardListTypeName" && (row.FindControl("lbSTHazardListTypeName") as AspNet.Label) != null)
                    {
                        html = (row.FindControl("lbSTHazardListTypeName") as AspNet.Label).Text;
                    }
                    if (column.ColumnID == "tfHazardListTypeCode" && (row.FindControl("lbHazardListTypeCode") as AspNet.Label) != null)
                    {
                        html = (row.FindControl("lbHazardListTypeCode") as AspNet.Label).Text;
                    }
                    if (column.ColumnID == "tfHazardListTypeName" && (row.FindControl("lbHazardListTypeName") as AspNet.Label) != null)
                    {
                        html = (row.FindControl("lbHazardListTypeName") as AspNet.Label).Text;
                    }
                    if (column.ColumnID == "tfHazardCode" && (row.FindControl("lbHazardCode") as AspNet.Label) != null)
                    {
                        html = (row.FindControl("lbHazardCode") as AspNet.Label).Text;
                    }
                    if (column.ColumnID == "tfHazardItems" && (row.FindControl("lbHazardItems") as AspNet.Label) != null)
                    {
                        html = (row.FindControl("lbHazardItems") as AspNet.Label).Text;
                    }
                    if (column.ColumnID == "tfDefectsType" && (row.FindControl("lbDefectsType") as AspNet.Label) != null)
                    {
                        html = (row.FindControl("lbDefectsType") as AspNet.Label).Text;
                    }
                    if (column.ColumnID == "tfMayLeadAccidents" && (row.FindControl("lbMayLeadAccidents") as AspNet.Label) != null)
                    {
                        html = (row.FindControl("lbMayLeadAccidents") as AspNet.Label).Text;
                    }
                    if (column.ColumnID == "tfHelperMethod" && (row.FindControl("lbHelperMethod") as AspNet.Label) != null)
                    {
                        html = (row.FindControl("lbHelperMethod") as AspNet.Label).Text;
                    }
                    if (column.ColumnID == "tfControlMeasures" && (row.FindControl("lbControlMeasures") as AspNet.Label) != null)
                    {
                        html = (row.FindControl("lbControlMeasures") as AspNet.Label).Text;
                    }
                    sb.AppendFormat("| {0}", html);
                }
                sb.Append(" | 
");
            }
            sb.Append("
");
            return sb.ToString();
        }
        #endregion
        /// 
        /// 分页下拉选择事件
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
    }
}