using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.HJGL.HotHardManage
{
    public partial class ShowHotHardSearch : PageBase
    {
        #region 定义变量
        /// 
        /// 单位ID
        /// 
        public string UnitId
        {
            get
            {
                return (string)ViewState["UnitId"];
            }
            set
            {
                ViewState["UnitId"] = value;
            }
        }
        /// 
        /// 装置id
        /// 
        public string InstallationId
        {
            get
            {
                return (string)ViewState["InstallationId"];
            }
            set
            {
                ViewState["InstallationId"] = value;
            }
        }
        /// 
        /// 选择列表
        /// 
        public List CheckList
        {
            get
            {
                return (List)ViewState["CheckList"];
            }
            set
            {
                ViewState["CheckList"] = value;
            }
        }
        /// 
        /// 定义集合
        /// 
        private static List hotHardSearchLists = new List();
        #endregion
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.UnitId = Request.Params["unitId"];
                this.InstallationId = Request.Params["installationId"];
                var unit = BLL.UnitService.GetUnitByUnitId(this.CurrUser.UnitId);
                if (BLL.WorkAreaService.IsSupervisor(this.CurrUser.UnitId, this.CurrUser.LoginProjectId))
                {
                    BLL.WorkAreaService.GetWorkAreaListByInstallSupervisorUnit(this.drpWorkArea, this.CurrUser.LoginProjectId, this.InstallationId, this.UnitId, this.CurrUser.UnitId, true);
                }
                else
                {
                    BLL.WorkAreaService.GetWorkAreaListByInstallUnit(this.drpWorkArea, this.CurrUser.LoginProjectId, this.InstallationId, this.UnitId, true);
                }
            }
        }
        #endregion
        #region 查询
        /// 
        ///查询按钮
        /// 
        /// 
        /// 
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (this.drpWorkArea.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpWorkArea.SelectedValue))
            {
                this.InitTreeMenu(this.drpWorkArea.SelectedValue);//加载树  
            }
            else
            {
                Alert.ShowInTop("请选择区域!", MessageBoxIcon.Warning);
                return;
            }
        }
        #endregion
        #region 加载树
        /// 
        /// 加载树节点
        /// 
        /// 
        private void InitTreeMenu(string workAreaId)
        {
            if (!string.IsNullOrEmpty(workAreaId))
            {
                this.tvControlItem.Nodes.Clear();
                TreeNode rootNode = new TreeNode();
                rootNode.Text = BLL.WorkAreaService.GetWorkAreaCodeByWorkAreaId(workAreaId);
                rootNode.NodeID = workAreaId;
                rootNode.Expanded = true;
                this.tvControlItem.Nodes.Add(rootNode);
                var trustSearch = from x in Funs.DB.View_HotHardSearch
                                  where x.ProjectId == this.CurrUser.LoginProjectId && x.UnitId == this.UnitId && x.WorkAreaId == workAreaId
                                  select x;
                if (!string.IsNullOrEmpty(this.txtISO_ID.Text.Trim()))
                {
                    trustSearch = trustSearch.Where(x => x.ISO_IsoNo.Contains(this.txtISO_ID.Text.Trim()));
                }
                var hotProessId = (from x in trustSearch select x.HotProessId).Distinct();
                if (hotProessId != null)
                {
                    foreach (var id in hotProessId)
                    {
                        var hotProess = from x in Funs.DB.HotProess
                                        where x.HotProessId == id.ToString() && x.InstallationId == this.InstallationId
                                        select x;
                        foreach (var itm in hotProess)
                        {
                            TreeNode newNode = new TreeNode();
                            newNode.Text = itm.HotProessNo;
                            newNode.NodeID = itm.HotProessId;
                            newNode.Expanded = true;
                            newNode.EnableCheckEvent = true;
                            //newNode.EnableClickEvent = true;
                            rootNode.Nodes.Add(newNode);
                        }
                    }
                }
            }
        }
        #endregion
        #region Tree选中事件
        /// 
        /// Tree选中事件
        /// 
        /// 
        /// 
        protected void tvControlItem_NodeCheck(object sender, TreeCheckEventArgs e)
        {
            CheckList = new List();
            TreeNode[] nodes = tvControlItem.GetCheckedNodes();
            if (nodes.Length > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (TreeNode node in nodes)
                {
                    CheckList.Add(node.NodeID);
                }
            }
            BindGrid(CheckList);
        }
        #endregion
        #region 数据绑定
        /// 
        /// 数据绑定
        /// 
        /// 
        private void BindGrid(List checkList)
        {
            hotHardSearchLists = (from x in Funs.DB.View_HotHardSearch where x.ProjectId == this.CurrUser.LoginProjectId select x).ToList();
            if (checkList != null)
            {
                hotHardSearchLists = hotHardSearchLists.Where(e => checkList.Contains(e.HotProessId)).ToList();
            }
            this.Grid1.DataSource = hotHardSearchLists;
            this.Grid1.DataBind();
        }
        #endregion
        #region 确定按钮
        /// 
        /// 确定按钮
        /// 
        /// 
        /// 
        protected void btnSure_Click(object sender, EventArgs e)
        {
            string selectedList = string.Empty;
            foreach (var item in Grid1.SelectedRowIDArray)
            {
                selectedList += item + ",";
            }
            if (!string.IsNullOrEmpty(selectedList))
            {
                selectedList = selectedList.Substring(0, selectedList.LastIndexOf(","));
            }
            PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(selectedList)
                   + ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}