using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
namespace FineUIPro.Web.ZHGL.Supervise
{
    public partial class ShowRectifyItem : PageBase
    {
        #region 定义集合
        /// 
        /// 定义集合
        /// 
        private static List list = new List();
        /// 
        /// 类型:安全、质量
        /// 
        public string Type
        {
            get
            {
                return (string)ViewState["type"];
            }
            set
            {
                ViewState["type"] = value;
            }
        }
        #endregion
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                list = new List();
                this.Type = Request.Params["type"];
                string lists = Request.Params["lists"];
                list = Funs.GetStrListByStr(lists, ',');
                InitTreeMenu();
            }
        }
        #endregion
        #region 加载树
        /// 
        /// 加载树
        /// 
        private void InitTreeMenu()
        {
            this.trRectify.Nodes.Clear();
            this.trRectify.ShowBorder = false;
            this.trRectify.ShowHeader = false;
            this.trRectify.EnableIcons = true;
            this.trRectify.AutoScroll = true;
            this.trRectify.EnableSingleClickExpand = true;
            TreeNode rootNode = new TreeNode
            {
                Text = "作业类别",
                NodeID = "0",
                Expanded = true
            };
            this.trRectify.Nodes.Add(rootNode);
            BoundTree(rootNode.Nodes, "0");
        }
        /// 
        /// 加载树
        /// 
        /// 
        /// 
        private void BoundTree(TreeNodeCollection nodes, string menuId)
        {
            var dt = BLL.RectifyService.GetRectifyBySupRectifyId(menuId, this.Type);
            if (dt.Count() > 0)
            {
                TreeNode tn = null;
                foreach (var dr in dt)
                {
                    tn = new TreeNode
                    {
                        Text = dr.RectifyName,
                        NodeID = dr.RectifyId,
                        EnableClickEvent = true
                    };
                    nodes.Add(tn);
                    BoundTree(tn.Nodes, dr.RectifyId);
                }
            }
        }
        #endregion
        #region 树点击事件
        /// 
        /// tree点击事件
        /// 
        /// 
        /// 
        protected void trRectify_NodeCommand(object sender, TreeCommandEventArgs e)
        {
            this.txtName.Text = "";
            BindGrid();
        }
        #endregion
        #region 加载Grid
        /// 
        /// 加载Grid
        /// 
        private void BindGrid()
        {
            string strSql = "";
            SqlParameter[] parameter = new SqlParameter[] { };
            if (string.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                strSql = "select * from View_Technique_RectifyItem where RectifyId=@RectifyId and IsPass=@IsPass";
                parameter = new SqlParameter[]
                       {
                        new SqlParameter("@RectifyId",this.trRectify.SelectedNode.NodeID),
                        new SqlParameter("@IsPass",true)
                       };
            }
            else
            {
                strSql = "select * from View_Technique_RectifyItem where IsPass=@IsPass and HazardSourcePoint like @HazardSourcePoint ";
                parameter = new SqlParameter[]
                   {
                        new SqlParameter("@IsPass",true),
                        new SqlParameter("@HazardSourcePoint", "%" + this.txtName.Text.Trim() + "%"),
                   };
            }
            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();
            if (list.Count() > 0)
            {
                for (int i = 0; i < Grid1.Rows.Count; i++)
                {
                    CheckBoxField checkField = (CheckBoxField)Grid1.FindColumn("ckbIsSelected");
                    string id = Grid1.DataKeys[i][0].ToString();
                    if (list.Contains(id))
                    {
                        checkField.SetCheckedState(i, true);
                    }
                }
            }
        }
        #endregion
        #region 过滤表头、分页、排序、关闭窗口
        /// 
        /// 过滤表头
        /// 
        /// 
        /// 
        protected void Grid1_FilterChange(object sender, EventArgs e)
        {
            BindGrid();
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        /// 
        /// Grid1排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }
        /// 
        /// 分页下拉选择事件
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        #endregion
        #region 确认按钮
        /// 
        /// 确认按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (list.Count == 0)
            {
                ShowNotify("请至少选择一项!", MessageBoxIcon.Warning);
                return;
            }
            string str = string.Empty;
            foreach (var item in list)
            {
                str += item + ",";
            }
            if (!string.IsNullOrEmpty(str))
            {
                str = str.Substring(0, str.LastIndexOf(","));
            }
            PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(str)
                   + ActiveWindow.GetHidePostBackReference());
        }
        #endregion
        #region Grid行点击事件
        /// 
        /// Grid1行点击事件
        /// 
        /// 
        /// 
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string rowID = Grid1.DataKeys[e.RowIndex][0].ToString();
            if (e.CommandName == "IsSelected")
            {
                CheckBoxField checkField = (CheckBoxField)Grid1.FindColumn("ckbIsSelected");
                if (checkField.GetCheckedState(e.RowIndex))
                {
                    if (!list.Contains(rowID))
                    {
                        list.Add(rowID);
                    }
                }
                else
                {
                    if (list.Contains(rowID))
                    {
                        list.Remove(rowID);
                    }
                }
            }
        }
        #endregion
        /// 
        /// 查询
        /// 
        /// 
        /// 
        protected void txtName_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                this.BindGrid();
            }
        }
    }
}