using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.HJGL.RepairManage
{
    public partial class ShowRepairSearch : 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;
            }
        }
        /// 
        /// 委托id
        /// 
        public string CH_TrustID
        {
            get
            {
                return (string)ViewState["CH_TrustID"];
            }
            set
            {
                ViewState["CH_TrustID"] = value;
            }
        }
        /// 
        /// 选择列表
        /// 
        public List jotList
        {
            get
            {
                return (List)ViewState["jotList"];
            }
            set
            {
                ViewState["jotList"] = value;
            }
        }       
        /// 
        /// 选择列表
        /// 
        public List CheckList
        {
            get
            {
                return (List)ViewState["CheckList"];
            }
            set
            {
                ViewState["CheckList"] = value;
            }
        }
        private static List repairSearchViewLists = new List();
        #endregion
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.UnitId = Request.Params["unitId"];
                this.InstallationId = Request.Params["installationId"];
                this.CH_TrustID = Request.Params["ch_TrustID"];
                BLL.WorkAreaService.InitWorkAreaProjectInstallUnitDropDownList(this.drpWorkArea, this.CurrUser.LoginProjectId, this.InstallationId, this.UnitId, true);
            }
        }
        #endregion
        #region 查询
        /// 
        /// 查询
        /// 
        /// 
        /// 
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string workAreaId = string.Empty;
            if (this.drpWorkArea.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpWorkArea.SelectedValue))
            {
                workAreaId = this.drpWorkArea.SelectedValue;
            }
            this.InitTreeMenu(workAreaId);//加载树  
        }
        #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);
                GetNodes(workAreaId, rootNode);
            }
            else
            {
                var workAreas = (from x in Funs.DB.ProjectData_WorkArea
                                 where x.ProjectId == this.CurrUser.LoginProjectId && x.UnitId == UnitId && x.InstallationId == InstallationId
                                 select x).ToList();
                foreach (var q in workAreas)
                {
                    var trustSearch = from x in Funs.DB.View_CH_RepairSearch
                                      where x.ProjectId == this.CurrUser.LoginProjectId && x.UnitId == UnitId && x.WorkAreaId == q.WorkAreaId
                                      select x;
                    if (trustSearch.Count() > 0)
                    {
                        TreeNode rootNode = new TreeNode();
                        rootNode.Text = q.WorkAreaCode;
                        rootNode.NodeID = q.WorkAreaId;
                        rootNode.Expanded = true;
                        this.tvControlItem.Nodes.Add(rootNode);
                        GetNodes(q.WorkAreaId, rootNode);
                    }
                }
            }
        }
        /// 
        /// 遍历节点
        /// 
        /// 
        /// 
        private void GetNodes(string workAreaId, TreeNode rootNode)
        {
            var trustSearch = from x in Funs.DB.View_CH_RepairSearch 
                              where x.ProjectId == this.CurrUser.LoginProjectId && x.UnitId == UnitId
                              && x.WorkAreaId == workAreaId && (x.RepairTrustId == null || x.RepairTrustId == this.CH_TrustID)
                              select x;
            if (!string.IsNullOrEmpty(this.txtISO_ID.Text.Trim()))
            {
                trustSearch = trustSearch.Where(x => x.ISO_IsoNo.Contains(this.txtISO_ID.Text.Trim()));
            }
            var checkID = (from x in trustSearch select x.CHT_CheckID).Distinct();
            if (checkID.Count() > 0)
            {
                foreach (var id in checkID)
                {
                    var point = from x in Funs.DB.CH_Check
                                where x.CHT_CheckID == id.ToString()
                                select x;
                    foreach (var item in point)
                    {
                        TreeNode newNode = new TreeNode();
                        newNode.Text = item.CHT_CheckCode;
                        newNode.NodeID = item.CHT_CheckID;
                        newNode.Expanded = true;
                        //newNode.EnableClickEvent = true;
                        //newNode.EnableCheckBox = true;
                        newNode.EnableCheckEvent = 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);
           BindGrid2();
        }
        #endregion
        #region 绑定数据
        private void BindGrid(List checkList)
        {
            repairSearchViewLists = (from x in Funs.DB.View_CH_RepairSearch where x.ProjectId == this.CurrUser.LoginProjectId select x).ToList();
            if (!string.IsNullOrEmpty(this.CH_TrustID))
            {
                repairSearchViewLists = repairSearchViewLists.Where(e => e.RepairTrustId == this.CH_TrustID || e.RepairTrustId is null).ToList();
            }
            else
            {
                repairSearchViewLists = repairSearchViewLists.Where(e => e.RepairTrustId is null).ToList();
            }
            if (checkList != null)
            {
                repairSearchViewLists = repairSearchViewLists.Where(e => checkList.Contains(e.CHT_CheckID)).ToList();
            }
            this.Grid1.DataSource = repairSearchViewLists;
            this.Grid1.DataBind();
            this.GetCheckgvList();
        }
        /// 
        /// 选择列表某项事件
        /// 
        private void GetCheckgvList()
        {
            jotList = new List();
            foreach (var item in repairSearchViewLists)
            {
                jotList.Add(item.JOT_ID);
            }
        }
        /// 
        /// 数据绑定2
        /// 
        private void BindGrid2()
        {
            List SpRepairSearchItems = new List();
            if (jotList != null)
            {
                var IsoInfo = from x in Funs.DB.PW_IsoInfo where x.ProjectId == this.CurrUser.LoginProjectId && x.WorkAreaId == this.drpWorkArea.SelectedValue && x.UnitId == this.UnitId select x;
                if (this.ckISO.Checked)
                {
                    var isoList = (from x in Funs.DB.PW_JointInfo where jotList.Contains(x.JOT_ID) select x.ISO_ID).Distinct().ToList();
                    IsoInfo = IsoInfo.Where(x => isoList.Contains(x.ISO_ID));
                }
                if (IsoInfo.Count() > 0)
                {
                    foreach (var isoItem in IsoInfo)
                    {
                        var jots = from x in Funs.DB.PW_JointInfo where x.ISO_ID == isoItem.ISO_ID && x.JOT_JointStatus == "100" && x.DReportID != null select x;
                        if (this.ckWeld.Checked)
                        {
                            var weldList = (from x in Funs.DB.PW_JointInfo where jotList.Contains(x.JOT_ID) select x.JOT_CellWelder).Distinct().ToList();
                            jots = jots.Where(x => weldList.Contains(x.JOT_CellWelder)).Distinct();
                        }
                        if (jots.Count() > 0)
                        {
                            foreach (var jotItem in jots)
                            {
                                if (!jotList.Contains(jotItem.JOT_ID))
                                {
                                    Model.PW_JointInfo SpRepairSearchItem = new Model.PW_JointInfo();
                                    SpRepairSearchItem.JOT_ID = jotItem.JOT_ID;
                                    SpRepairSearchItem.JOT_JointNo = jotItem.JOT_JointNo;
                                    SpRepairSearchItem.ISO_ID = jotItem.ISO_ID;
                                    SpRepairSearchItem.JOT_CellWelder = jotItem.JOT_CellWelder;
                                    SpRepairSearchItems.Add(SpRepairSearchItem);
                                }
                            }
                        }
                    }
                }
            }
            this.Grid2.DataSource = SpRepairSearchItems;
            this.Grid2.DataBind();
        }
        #endregion
        #region 是否默认同管线、同焊工
        /// 
        /// 是否默认同管线
        /// 
        /// 
        /// 
        protected void ckISO_CheckedChanged(object sender, CheckedEventArgs e)
        {
            GetCheckgvList();
        }
        /// 
        /// 是否同焊工
        /// 
        /// 
        /// 
        protected void ckWeld_CheckedChanged(object sender, CheckedEventArgs e)
        {
            GetCheckgvList();
        }
        #endregion
        #region 确定
        /// 
        /// 确定按钮
        /// 
        /// 
        /// 
        protected void btnSure_Click(object sender, EventArgs e)
        {
            string selectedList = string.Empty;
            foreach (var item in repairSearchViewLists)
            {
                selectedList += item.JOT_ID +";"+item.CHT_CheckID+ ",";
            }
            if (repairSearchViewLists.Count > 0)
            {
                foreach (var jotId in Grid2.SelectedRowIDArray)
                {
                    selectedList += jotId + ",";
                }
                if (!string.IsNullOrEmpty(selectedList))
                {
                    selectedList = selectedList.Substring(0, selectedList.LastIndexOf(","));
                }
                else
                {
                    Alert.ShowInTop("请选择焊口再保存!", MessageBoxIcon.Warning);
                    return;
                }
            }
            PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(selectedList)
                   + ActiveWindow.GetHidePostBackReference());
        }
        #endregion
        #region 格式化字符串
        /// 
        /// 根据管线Id获取管线号
        /// 
        /// 
        /// 
        protected string ConvertIsoNo(object ISO_ID)
        {
            if (ISO_ID != null)
            {
                return BLL.PW_IsoInfoService.GetIsoInfoByIsoInfoId(ISO_ID.ToString()).ISO_IsoNo;
            }
            else
            {
                return "";
            }
        }
        #endregion
    }
}