using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.HJGL.CheckManage
{
    public partial class ShowTrust :PageBase
    {
        #region 定义变量
        /// 
        /// 委托单主键
        /// 
        public string Trust_ID
        {
            get
            {
                return (string)ViewState["Trust_ID"];
            }
            set
            {
                ViewState["Trust_ID"] = value;
            }
        }
        private List viewCheckSerchs = new List();
        #endregion
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string unitId = Request.Params["unitId"];
                string installationId = Request.Params["installationId"];
                BLL.UnitService.InitUnitByProjectIdUnitTypeDropDownList(this.drpCheckUnitId, this.CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_5, true);//查询检测单位
                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, installationId, unitId, this.CurrUser.UnitId, true);
                }
                else
                {
                    BLL.WorkAreaService.GetWorkAreaListByInstallUnit(this.drpWorkArea, this.CurrUser.LoginProjectId, installationId, unitId, true);
                }
                if (!String.IsNullOrEmpty(Request.Params["ch_TrustID"]))
                {
                    Trust_ID = Request.Params["ch_TrustID"].ToString();
                    var trust = BLL.TrustManageEditService.GetCH_TrustByID(Trust_ID);
                    if (trust != null)
                    {
                        this.txtTrust_ID.Text = trust.CH_TrustCode;
                    }
                    this.drpCheckUnitId.Enabled = false;
                    this.drpWorkArea.Enabled = false;
                    this.txtTrust_ID.Enabled = false;
                }
                else
                {
                    this.drpCheckUnitId.Enabled = true;
                    this.drpWorkArea.Enabled = true;
                    this.txtTrust_ID.Enabled = true;
                }
            }
        }
        #endregion
        #region 查询
        /// 
        /// 查询
        /// 
        /// 
        /// 
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            InitTreeMenu();
        }
        private void GetNodes(TreeNodeCollection nodes, string parentId)
        {
            if (parentId != null)
            {
                if (this.drpCheckUnitId.SelectedValue != BLL.Const._Null&&!string.IsNullOrEmpty(this.drpCheckUnitId.SelectedValue))
                {
                    viewCheckSerchs = viewCheckSerchs.Where(x => x.CH_CheckUnit == this.drpCheckUnitId.SelectedValue).ToList();
                }
                else if (this.CurrUser.UnitId != null && BLL.UnitService.GetUnitByUnitId(this.CurrUser.UnitId).UnitTypeId == BLL.Const.ProjectUnitType_5)
                {   //显示和检测单位相关的单子
                    viewCheckSerchs = viewCheckSerchs.Where(x => x.CH_CheckUnit == this.CurrUser.UnitId).ToList();
                }
                if (!string.IsNullOrEmpty(this.txtTrust_ID.Text.Trim()))
                {
                    viewCheckSerchs = viewCheckSerchs.Where(x => x.CH_TrustCode.Contains(this.txtTrust_ID.Text.Trim())).ToList();
                }
                if (!String.IsNullOrEmpty(Trust_ID)) // 直接查找委托单明细
                {
                    var trust = BLL.TrustManageEditService.GetCH_TrustByID(Trust_ID);
                    TreeNode newNode = new TreeNode();
                    newNode.Text = trust.CH_TrustCode;
                    newNode.NodeID = trust.CH_TrustID;
                    newNode.EnableClickEvent = true;
                    nodes.Add(newNode);
                }
                else
                {
                    var trustIds = (from x in viewCheckSerchs
                                    where x.WorkAreaId == parentId
                                    select new { x.CH_TrustID, x.CH_TrustCode }).Distinct();
                    if (trustIds != null)
                    {
                        foreach (var trustId in trustIds)
                        {
                            // 判断这个委托单是不是在检测单中存在,如果存在则不加载
                            int num = BLL.CheckManageService.GetCheckByTrustId(trustId.CH_TrustID);
                            if (num == 0)
                            {
                                TreeNode newNode = new TreeNode();
                                newNode.Text = trustId.CH_TrustCode;
                                newNode.NodeID = trustId.CH_TrustID;
                                newNode.EnableClickEvent = true;
                                nodes.Add(newNode);
                            }
                        }
                    }
                }
            }
        }
        #endregion
        #region 加载树
        /// 
        /// 加载树节点
        /// 
        /// 
        private void InitTreeMenu()
        {
            string unitId = Request.Params["unitId"];
            string installationId = Request.Params["installationId"];
            viewCheckSerchs.Clear();
            this.tvControlItem.Nodes.Clear();
            if (this.drpCheckUnitId.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpCheckUnitId.SelectedValue))
            {
                viewCheckSerchs = (from x in Funs.DB.View_CH_CheckSerch where x.ProjectId == this.CurrUser.LoginProjectId && x.CH_CheckUnit == this.drpCheckUnitId.SelectedValue select x).ToList();
            }
            else
            {
                viewCheckSerchs = (from x in Funs.DB.View_CH_CheckSerch where x.ProjectId == this.CurrUser.LoginProjectId && x.UnitId == unitId select x).ToList();
            }
            if (this.drpWorkArea.SelectedValue != BLL.Const._Null && !string.IsNullOrEmpty(this.drpWorkArea.SelectedValue))
            {
                TreeNode rootNode = new TreeNode();
                rootNode.Text = this.drpWorkArea.SelectedItem.Text;
                rootNode.NodeID = this.drpWorkArea.SelectedValue;
                rootNode.Expanded = true;
                viewCheckSerchs = viewCheckSerchs.Where(x => x.WorkAreaId == this.drpWorkArea.SelectedValue).ToList();
                this.tvControlItem.Nodes.Add(rootNode);
                this.GetNodes(rootNode.Nodes, rootNode.NodeID);
            }
            else
            {
                var workAreas = (from x in Funs.DB.ProjectData_WorkArea where x.UnitId == unitId && x.InstallationId == installationId select x).ToList();
                var workAreasIds = workAreas.Select(x => x.WorkAreaId);
                viewCheckSerchs = viewCheckSerchs.Where(x => workAreasIds.Contains(x.WorkAreaId)).ToList();
                foreach (var q in workAreas)
                {
                    if (viewCheckSerchs.Count() > 0)
                    {
                        var isExWork = Funs.DB.View_CH_CheckSerch.FirstOrDefault(x => x.WorkAreaId == q.WorkAreaId);
                        if (isExWork != null)
                        {
                            TreeNode tn = new TreeNode();
                            tn.Text = q.WorkAreaCode;
                            tn.NodeID = q.WorkAreaId;
                            this.tvControlItem.Nodes.Add(tn);
                            this.GetNodes(tn.Nodes, tn.NodeID);
                        }
                    }
                }
            }
        }
        #endregion
        #region 树点击事件
        /// 
        /// Tree点击事件
        /// 
        /// 
        /// 
        protected void tvControlItem_NodeCommand(object sender, TreeCommandEventArgs e)
        {
            this.Trust_ID = this.tvControlItem.SelectedNode.NodeID;
            BindGrid(this.Trust_ID);
        }
        #endregion
        #region 数据绑定
        /// 
        /// 数据绑定
        /// 
        private void BindGrid(string trust_ID)
        {
            string strSql = @"select checkSearch.ProjectId,
                                    checkSearch.JOT_ID,
                                    checkSearch.CH_TrustID,
                                    checkSearch.CH_TrustCode,
                                    checkSearch.UnitId,
                                    checkSearch.UnitName,
                                    checkSearch.ISO_IsoNo,
                                    checkSearch.JOT_JointNo,
                                    checkSearch.DetectionTypeId,
                                    checkSearch.DetectionTypeName,
                                    checkSearch.JOT_JointDesc"
                            + @" from View_CH_CheckSerch as checkSearch "
                            + @" WHERE checkSearch.ProjectId= @projectId AND checkSearch.CH_TrustID=@CH_TrustID";
            List listStr = new List
            {
                new SqlParameter("@projectId", this.CurrUser.LoginProjectId),
                new SqlParameter("@CH_TrustID",trust_ID),
            };
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            Grid1.RecordCount = tb.Rows.Count;
           // var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = tb;
            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(","));
            }
            selectedList += "|" + this.Trust_ID;
            PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(selectedList)
                   + ActiveWindow.GetHidePostBackReference());
        }
        #endregion
    }
}