using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;
using System.IO;
using System.Data.SqlClient;
using Newtonsoft.Json.Linq;
using AspNet = System.Web.UI.WebControls;

namespace FineUIPro.Web.CQMS.WBS
{
    public partial class ControlPoint : PageBase
    {
        #region  页面加载
        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetButtonPower();
                InitTreeMenu();
                this.Grid1.Columns[2].Hidden = false;
                this.Grid1.Columns[3].Hidden = true;
                this.Grid1.Columns[4].Hidden = true;
                this.Grid1.Columns[5].Hidden = false;
                this.Grid1.Columns[7].Hidden = false;
                this.Grid1.Columns[8].Hidden = false;
                this.Grid1.Columns[9].Hidden = false;
                this.Grid1.Columns[10].Hidden = false;
                this.Grid1.Columns[11].Hidden = false;
                this.Grid1.Columns[12].Hidden = false;
            }
        }
        #endregion

        #region  加载树
        /// <summary>
        /// 加载树
        /// </summary>
        private void InitTreeMenu()
        {
            this.trWBS.Nodes.Clear();
            this.trWBS.ShowBorder = false;
            this.trWBS.ShowHeader = false;
            this.trWBS.EnableIcons = true;
            this.trWBS.AutoScroll = true;
            this.trWBS.EnableSingleClickExpand = true;
            TreeNode newNode1 = new TreeNode();
            newNode1.Text = "建筑工程";
            newNode1.NodeID = "1";
            newNode1.CommandName = "ProjectType";
            newNode1.EnableExpandEvent = true;
            newNode1.EnableClickEvent = true;
            trWBS.Nodes.Add(newNode1);
            TreeNode tempNode1 = new TreeNode();
            tempNode1.NodeID = "";
            tempNode1.Text = "";
            tempNode1.EnableExpandEvent = true;
            tempNode1.EnableClickEvent = true;
            newNode1.Nodes.Add(tempNode1);
            TreeNode newNode2 = new TreeNode();
            newNode2.Text = "安装工程";
            newNode2.NodeID = "2";
            newNode2.CommandName = "ProjectType";
            newNode2.EnableExpandEvent = true;
            newNode2.EnableClickEvent = true;
            trWBS.Nodes.Add(newNode2);
            TreeNode tempNode2 = new TreeNode();
            tempNode2.NodeID = "";
            tempNode2.Text = "";
            tempNode2.EnableExpandEvent = true;
            tempNode2.EnableClickEvent = true;
            newNode2.Nodes.Add(tempNode2);
        }
        #endregion

        #region  展开树
        /// <summary>
        /// 展开树
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void trWBS_NodeExpand(object sender, TreeNodeEventArgs e)
        {
            e.Node.Nodes.Clear();
            if (e.Node.CommandName == "ProjectType")   //展开工程类型
            {
                if (e.NodeID == "1")
                {
                    var cNProfessional = (from x in BLL.Funs.DB.Base_CNProfessional where x.CNProfessionalId == Const.CNProfessionalCVId orderby x.SortIndex select x).ToList();
                    foreach (var c in cNProfessional)
                    {
                        TreeNode newCNProfessionalNode = new TreeNode();
                        newCNProfessionalNode.Text = c.ProfessionalName;
                        newCNProfessionalNode.NodeID = c.CNProfessionalId;
                        newCNProfessionalNode.CommandName = "CNProfessional";
                        newCNProfessionalNode.EnableExpandEvent = true;
                        newCNProfessionalNode.EnableClickEvent = true;
                        e.Node.Nodes.Add(newCNProfessionalNode);
                        TreeNode tempNode = new TreeNode();
                        tempNode.NodeID = "";
                        tempNode.Text = "";
                        tempNode.EnableExpandEvent = true;
                        tempNode.EnableClickEvent = true;
                        newCNProfessionalNode.Nodes.Add(tempNode);
                    }
                }
                else
                {
                    var cNProfessional = (from x in BLL.Funs.DB.Base_CNProfessional where x.CNProfessionalId != Const.CNProfessionalConstructId && x.CNProfessionalId != Const.CNProfessionalCVId orderby x.SortIndex select x).ToList();
                    foreach (var c in cNProfessional)
                    {
                        TreeNode newCNProfessionalNode = new TreeNode();
                        newCNProfessionalNode.Text = c.ProfessionalName;
                        newCNProfessionalNode.NodeID = c.CNProfessionalId;
                        newCNProfessionalNode.CommandName = "CNProfessional";
                        newCNProfessionalNode.EnableExpandEvent = true;
                        newCNProfessionalNode.EnableClickEvent = true;
                        e.Node.Nodes.Add(newCNProfessionalNode);
                        TreeNode tempNode = new TreeNode();
                        tempNode.NodeID = "";
                        tempNode.Text = "";
                        tempNode.EnableExpandEvent = true;
                        tempNode.EnableClickEvent = true;
                        newCNProfessionalNode.Nodes.Add(tempNode);
                    }
                }
            }
            else if (e.Node.CommandName == "CNProfessional")   //展开专业
            {
                var divisions = (from x in BLL.Funs.DB.WBS_Division
                                 where x.CNProfessionalId == e.Node.NodeID && x.SuperDivisionId == null
                                 orderby x.SortIndex
                                 select x).ToList();
                foreach (var q in divisions)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Text = q.DivisionName;
                    newNode.NodeID = q.DivisionId;
                    newNode.CommandName = "Division";
                    newNode.EnableExpandEvent = true;
                    newNode.EnableClickEvent = true;
                    e.Node.Nodes.Add(newNode);
                    var list = (from x in Funs.DB.WBS_Division
                                where x.SuperDivisionId == q.DivisionId
                                orderby x.SortIndex
                                select x).ToList();
                    if (list.Count > 0)
                    {
                        TreeNode tempNode = new TreeNode();
                        tempNode.NodeID = "";
                        tempNode.Text = "";
                        newNode.Nodes.Add(tempNode);
                    }
                }
            }
            else if (e.Node.CommandName == "Division")   //展开分部节点
            {
                var childDivisions = (from x in BLL.Funs.DB.WBS_Division
                                      where x.SuperDivisionId == e.Node.NodeID
                                      orderby x.SortIndex
                                      select x).ToList();
                foreach (var q in childDivisions)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Text = q.DivisionName;
                    newNode.NodeID = q.DivisionId;
                    newNode.CommandName = "Division";
                    newNode.EnableExpandEvent = true;
                    newNode.EnableClickEvent = true;
                    e.Node.Nodes.Add(newNode);
                    var list = (from x in Funs.DB.WBS_Division
                                where x.SuperDivisionId == q.DivisionId
                                orderby x.SortIndex
                                select x).ToList();
                    if (list.Count > 0)
                    {
                        TreeNode tempNode = new TreeNode();
                        tempNode.NodeID = "";
                        tempNode.Text = "";
                        newNode.Nodes.Add(tempNode);
                    }
                }
            }
        }
        #endregion

        #region  Tree点击事件
        /// <summary>
        /// Tree点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void trWBS_NodeCommand(object sender, TreeCommandEventArgs e)
        {
            string divisionId = this.trWBS.SelectedNode.NodeID;
            var temp = BLL.DivisionService.GetDivisionById(divisionId);
            if (temp == null)
            {
                return;
            }
            if (temp.CNProfessionalId != null && temp.CNProfessionalId == Const.CNProfessionalConstructId)
            {
                this.Grid1.Columns[2].Hidden = false;
                this.Grid1.Columns[3].Hidden = true;
                this.Grid1.Columns[4].Hidden = true;
                this.Grid1.Columns[5].Hidden = false;
                this.Grid1.Columns[7].Hidden = false;
                this.Grid1.Columns[8].Hidden = false;
                this.Grid1.Columns[9].Hidden = false;
                this.Grid1.Columns[10].Hidden = false;
                this.Grid1.Columns[11].Hidden = false;
                this.Grid1.Columns[12].Hidden = false;
            }
            else
            {
                this.Grid1.Columns[2].Hidden = false;
                this.Grid1.Columns[3].Hidden = false;
                this.Grid1.Columns[4].Hidden = false;
                this.Grid1.Columns[5].Hidden = true;
                this.Grid1.Columns[7].Hidden = true;
                this.Grid1.Columns[8].Hidden = true;
                this.Grid1.Columns[9].Hidden = true;
                this.Grid1.Columns[10].Hidden = true;
                this.Grid1.Columns[11].Hidden = true;
                this.Grid1.Columns[12].Hidden = true;
            }
            BindGrid();
        }
        #endregion

        #region  修改关闭窗口
        /// <summary>
        /// 关闭窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            ShowNotify("修改成功!", MessageBoxIcon.Success);

            GetSelectTreeNode();
        }
        #endregion

        #region  增加关闭窗口
        /// <summary>
        /// 增加关闭窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Window2_Close(object sender, WindowCloseEventArgs e)
        {
            ShowNotify("增加成功!", MessageBoxIcon.Success);

            GetSelectTreeNode();
        }
        #endregion

        #region  关闭窗口
        /// <summary>
        /// 关闭窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Window3_Close(object sender, WindowCloseEventArgs e)
        {
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            BindGrid();
        }
        #endregion

        #region 右键增加、修改、删除方法
        /// <summary>
        /// 右键修改事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMenuEdit_Click(object sender, EventArgs e)
        {
            if (this.trWBS.SelectedNode != null)
            {
                if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.ControlPointMenuId, BLL.Const.BtnModify))
                {
                    if (this.trWBS.SelectedNode.CommandName != "ProjectType" && this.trWBS.SelectedNode.CommandName != "CNProfessional")   //非工程类型和专业节点可以修改
                    {
                        this.hdSelectId.Text = this.trWBS.SelectedNode.NodeID;
                        string openUrl = String.Format("EditDivision.aspx?type=modify&selectedCode={0}", this.trWBS.SelectedNode.NodeID, "编辑 - ");
                        PageContext.RegisterStartupScript(Window1.GetSaveStateReference(hdSelectId.ClientID)
                                    + Window1.GetShowReference(openUrl));
                    }
                    else
                    {
                        ShowNotify("工程类型和专业节点无法修改!", MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                }
            }
            else
            {
                ShowNotify("请选择树节点!", MessageBoxIcon.Warning);
            }
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMenuAdd_Click(object sender, EventArgs e)
        {
            if (this.trWBS.SelectedNode != null)
            {
                if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.ControlPointMenuId, BLL.Const.BtnAdd))
                {
                    if (this.trWBS.SelectedNode.CommandName != "ProjectType")   //非工程类型节点可以增加
                    {
                        string openUrl = String.Format("EditDivision.aspx?type=add&selectedCode={0}", this.trWBS.SelectedNode.NodeID, "增加 - ");
                        PageContext.RegisterStartupScript(Window2.GetSaveStateReference(hdSelectId.ClientID)
                                + Window2.GetShowReference(openUrl));
                    }
                    else
                    {
                        ShowNotify("工程类型节点无法增加!", MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                }
            }
            else
            {
                ShowNotify("请选择树节点!", MessageBoxIcon.Warning);
            }
        }

        /// <summary>
        /// 右键删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMenuDelete_Click(object sender, EventArgs e)
        {
            if (this.trWBS.SelectedNode != null)
            {
                if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.ControlPointMenuId, BLL.Const.BtnDelete))
                {
                    if (this.trWBS.SelectedNode.CommandName != "ProjectType" && this.trWBS.SelectedNode.CommandName != "CNProfessional")   //非工程类型和专业节点可以删除
                    {
                        string id = this.trWBS.SelectedNodeID;
                        //var workPackage = Funs.DB.WBS_WorkPackage.FirstOrDefault(x => x.ProjectId == this.CurrUser.LoginProjectId && x.InitWorkPackageCode == this.trWBS.SelectedNodeID);
                        //if (workPackage != null)
                        //{
                        //    ShowNotify("WBS定制中已使用该数据,无法删除!", MessageBoxIcon.Warning);
                        //}
                        //else
                        //{
                        DeleteData();
                        //}
                    }
                    else
                    {
                        ShowNotify("工程类型和专业节点无法删除!", MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                }
            }
            else
            {
                ShowNotify("请选择树节点!", MessageBoxIcon.Warning);
            }
        }

        /// <summary>
        /// 删除方法
        /// </summary>
        private void DeleteData()
        {
            string id = this.trWBS.SelectedNodeID;
            this.hdSelectId.Text = this.trWBS.SelectedNode.ParentNode.NodeID;
            if (this.trWBS.SelectedNode.CommandName == "Division")
            {
                string divisionId = this.trWBS.SelectedNode.NodeID;
                Model.WBS_Division division = BLL.DivisionService.GetDivisionById(divisionId);
                if (division != null)
                {
                    if (!string.IsNullOrEmpty(division.CNProfessionalId))    //删除分部
                    {
                        var q = from x in Funs.DB.WBS_Division where x.SuperDivisionId == divisionId select x;
                        if (q.Count() > 0)     //分部含有子分部
                        {
                            foreach (var d in q)
                            {
                                var q2 = from x in Funs.DB.WBS_Division where x.SuperDivisionId == d.DivisionId select x;
                                if (q2.Count() > 0)     //子分部还有子级
                                {
                                    foreach (var d2 in q2)
                                    {
                                        BLL.BreakdownService.DeleteAllBreakdown(d2.DivisionId);
                                        BLL.DivisionService.DeleteDivision(d2.DivisionId);
                                    }
                                }
                                BLL.BreakdownService.DeleteAllBreakdown(d.DivisionId);
                                BLL.DivisionService.DeleteDivision(d.DivisionId);
                            }
                        }
                        else          //分部不包含子分部
                        {
                            BLL.BreakdownService.DeleteAllBreakdown(divisionId);
                        }
                        BLL.DivisionService.DeleteDivision(divisionId);
                    }
                    else   //删除子分部
                    {
                        var q = from x in Funs.DB.WBS_Division where x.SuperDivisionId == divisionId select x;
                        if (q.Count() > 0)     //子分部含有子级
                        {
                            foreach (var d in q)
                            {
                                BLL.BreakdownService.DeleteAllBreakdown(d.DivisionId);
                                BLL.DivisionService.DeleteDivision(d.DivisionId);
                            }
                        }
                        else          //子分部不包含子级
                        {
                            BLL.BreakdownService.DeleteAllBreakdown(divisionId);
                        }
                        BLL.DivisionService.DeleteDivision(divisionId);
                    }
                }
                BLL.LogService.AddSys_Log(this.CurrUser, id, id, BLL.Const.ControlPointMenuId, "删除分部或子分部工程及其下所有内容!");
            }
            ShowNotify("删除成功!", MessageBoxIcon.Success);
            GetSelectTreeNode();
        }
        #endregion

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnNew_Click(object sender, EventArgs e)
        {
            if (this.trWBS.SelectedNode != null)
            {
                if (this.trWBS.SelectedNode.Nodes.Count == 0)   //末级节点
                {
                    PageContext.RegisterStartupScript(Window3.GetShowReference(String.Format("EditBreakdown.aspx?type=add&divisionId={0}", this.trWBS.SelectedNode.NodeID, "新增 - ")));
                }
                else
                {
                    ShowNotify("不是末级,无法添加分项!", MessageBoxIcon.Warning);
                }
            }
            else
            {
                ShowNotify("请选择树节点!", MessageBoxIcon.Warning);
            }
        }

        #region Grid双击事件
        /// <summary>
        /// Grid行双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            btnMenuModify_Click(null, null);
        }
        #endregion
        #region 编辑
        /// <summary>
        /// 编辑按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMenuModify_Click(object sender, EventArgs e)
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning);
                return;
            }
            PageContext.RegisterStartupScript(Window3.GetShowReference(String.Format("EditBreakdown.aspx?type=modify&breakdownId={0}&divisionId={1}", this.Grid1.SelectedRowID, this.trWBS.SelectedNodeID, "新增 - ")));
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMenuDel_Click(object sender, EventArgs e)
        {
            if (Grid1.SelectedRowIndexArray.Length == 0)
            {
                Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning);
                return;
            }
            //var controlItemAndCycle = Funs.DB.WBS_ControlItemAndCycle.FirstOrDefault(x => x.ProjectId == this.CurrUser.LoginProjectId && x.InitControlItemCode == Grid1.SelectedRowID);
            //if (controlItemAndCycle != null)
            //{
            //    ShowNotify("WBS定制中已使用该数据,无法删除!", MessageBoxIcon.Warning);
            //}
            //else
            //{
            BLL.BreakdownService.DeleteBreakdown(Grid1.SelectedRowID);
            BLL.LogService.AddSys_Log(this.CurrUser, Grid1.SelectedRowID, Grid1.SelectedRowID, BLL.Const.ControlPointMenuId, "删除分项");
            Grid1.DataBind();
            BindGrid();
            Alert.ShowInTop("删除数据成功!", MessageBoxIcon.Success);
            //}
        }
        #endregion

        #region  绑定数据
        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_FilterChange(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        /// <summary>
        /// Grid1排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }

        /// <summary>
        /// 分页下拉选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }

        /// <summary>
        /// 加载Grid
        /// </summary>
        private void BindGrid()
        {
            string strSql = @"SELECT BreakdownId,BreakdownCode,BreakdownName,Basis,CheckPoints,RecordAndCode,Class,FenBao,WuHuan,JianLi,YeZhu,Remark,ModelURL"
                     + @" FROM WBS_Breakdown ";
            List<SqlParameter> listStr = new List<SqlParameter>();
            strSql += " where DivisionId = @DivisionId ";
            listStr.Add(new SqlParameter("@DivisionId", this.trWBS.SelectedNodeID));
            SqlParameter[] parameter = listStr.ToArray();
            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();
        }
        #endregion

        #region 根据所给Id定位到对应分部分项
        /// <summary>
        /// 根据所给Id定位到对应具体的工程类型、子单位工程、分部、子分部、分项、子分项
        /// </summary>
        private void GetSelectTreeNode()
        {
            string projectType = string.Empty;
            string cNProfessionalId = string.Empty;
            string projectTypeId = string.Empty;
            List<string> list = new List<string>();
            Model.WBS_Division division = BLL.DivisionService.GetDivisionById(this.hdSelectId.Text);
            if (division != null)
            {
                list = GetDivisionIds(division);
                list.Add(this.hdSelectId.Text);
                cNProfessionalId = GetCNProfessionalId(division);
                if (cNProfessionalId == Const.CNProfessionalConstructId || cNProfessionalId == Const.CNProfessionalCVId)
                {
                    projectTypeId = Const.CNProfessionalConstructId;
                }
                else
                {
                    projectTypeId = "2";
                }
                list.Add(cNProfessionalId);
            }
            else
            {
                cNProfessionalId = this.hdSelectId.Text;
                if (cNProfessionalId == Const.CNProfessionalConstructId || cNProfessionalId == Const.CNProfessionalCVId)
                {
                    projectTypeId = Const.CNProfessionalConstructId;
                }
                else
                {
                    projectTypeId = "2";
                }
            }

            InitTreeMenu();
            for (int i = 0; i < trWBS.Nodes.Count; i++)
            {
                if (trWBS.Nodes[i].NodeID == projectTypeId)
                {
                    trWBS.Nodes[i].Nodes.Clear();
                    trWBS.Nodes[i].Expanded = true;
                    if (trWBS.Nodes[i].NodeID == BLL.Const.CNProfessionalConstructId)  //建筑工程
                    {
                        var divisions = (from x in BLL.Funs.DB.WBS_Division
                                         where x.CNProfessionalId == BLL.Const.CNProfessionalConstructId && x.SuperDivisionId == null
                                         orderby x.SortIndex
                                         select x).ToList();
                        foreach (var q in divisions)
                        {
                            TreeNode newNode4 = new TreeNode();
                            newNode4.Text = q.DivisionName;
                            newNode4.NodeID = q.DivisionId;
                            newNode4.CommandName = "Division";
                            newNode4.EnableExpandEvent = true;
                            newNode4.EnableClickEvent = true;
                            trWBS.Nodes[i].Nodes.Add(newNode4);
                            var division1s = (from x in BLL.Funs.DB.WBS_Division
                                              where x.SuperDivisionId == q.DivisionId
                                              orderby x.SortIndex
                                              select x).ToList();
                            if (list.Contains(q.DivisionId))
                            {
                                newNode4.Expanded = true;
                                foreach (var division1 in division1s)
                                {
                                    TreeNode newNode5 = new TreeNode();
                                    newNode5.Text = division1.DivisionName;
                                    newNode5.NodeID = division1.DivisionId;
                                    newNode5.CommandName = "Division";
                                    newNode5.EnableExpandEvent = true;
                                    newNode5.EnableClickEvent = true;
                                    newNode4.Nodes.Add(newNode5);
                                    var division2s = (from x in BLL.Funs.DB.WBS_Division
                                                      where x.SuperDivisionId == division1.DivisionId
                                                      orderby x.SortIndex
                                                      select x).ToList();
                                    if (list.Contains(division1.DivisionId))
                                    {
                                        newNode5.Expanded = true;
                                        foreach (var division2 in division2s)
                                        {
                                            TreeNode newNode6 = new TreeNode();
                                            newNode6.Text = division2.DivisionName;
                                            newNode6.NodeID = division2.DivisionId;
                                            newNode6.CommandName = "Division";
                                            newNode6.EnableExpandEvent = true;
                                            newNode6.EnableClickEvent = true;
                                            newNode5.Nodes.Add(newNode6);
                                            var division3s = (from x in BLL.Funs.DB.WBS_Division
                                                              where x.SuperDivisionId == division2.DivisionId
                                                              orderby x.SortIndex
                                                              select x).ToList();
                                            if (list.Contains(division2.DivisionId))
                                            {
                                                newNode6.Expanded = true;
                                            }
                                            else
                                            {
                                                if (division3s.Count > 0)
                                                {
                                                    TreeNode tempNode2 = new TreeNode();
                                                    tempNode2.NodeID = "";
                                                    tempNode2.Text = "";
                                                    newNode6.Nodes.Add(tempNode2);
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (division2s.Count > 0)
                                        {
                                            TreeNode tempNode2 = new TreeNode();
                                            tempNode2.NodeID = "";
                                            tempNode2.Text = "";
                                            newNode5.Nodes.Add(tempNode2);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (division1s.Count > 0)
                                {
                                    TreeNode tempNode2 = new TreeNode();
                                    tempNode2.NodeID = "";
                                    tempNode2.Text = "";
                                    newNode4.Nodes.Add(tempNode2);
                                }
                            }
                        }
                    }
                    else   //安装工程
                    {
                        var cNProfessional = (from x in BLL.Funs.DB.Base_CNProfessional where x.CNProfessionalId != Const.CNProfessionalConstructId && x.CNProfessionalId != Const.CNProfessionalCVId orderby x.SortIndex select x).ToList();
                        foreach (var c in cNProfessional)
                        {
                            TreeNode newCNProfessionalNode = new TreeNode();
                            newCNProfessionalNode.Text = c.ProfessionalName;
                            newCNProfessionalNode.NodeID = c.CNProfessionalId;
                            newCNProfessionalNode.CommandName = "CNProfessional";
                            newCNProfessionalNode.EnableExpandEvent = true;
                            newCNProfessionalNode.EnableClickEvent = true;
                            trWBS.Nodes[i].Nodes.Add(newCNProfessionalNode);
                            if (c.CNProfessionalId == cNProfessionalId)
                            {
                                newCNProfessionalNode.Expanded = true;
                                var divisions = (from x in BLL.Funs.DB.WBS_Division
                                                 where x.CNProfessionalId == c.CNProfessionalId && x.SuperDivisionId == null
                                                 orderby x.SortIndex
                                                 select x).ToList();
                                foreach (var q in divisions)
                                {
                                    TreeNode newNode4 = new TreeNode();
                                    newNode4.Text = q.DivisionName;
                                    newNode4.NodeID = q.DivisionId;
                                    newNode4.CommandName = "Division";
                                    newNode4.EnableExpandEvent = true;
                                    newNode4.EnableClickEvent = true;
                                    newCNProfessionalNode.Nodes.Add(newNode4);
                                    var division1s = (from x in BLL.Funs.DB.WBS_Division
                                                      where x.SuperDivisionId == q.DivisionId
                                                      orderby x.SortIndex
                                                      select x).ToList();
                                    if (list.Contains(q.DivisionId))
                                    {
                                        newNode4.Expanded = true;
                                        foreach (var division1 in division1s)
                                        {
                                            TreeNode newNode5 = new TreeNode();
                                            newNode5.Text = division1.DivisionName;
                                            newNode5.NodeID = division1.DivisionId;
                                            newNode5.CommandName = "Division";
                                            newNode5.EnableExpandEvent = true;
                                            newNode5.EnableClickEvent = true;
                                            newNode4.Nodes.Add(newNode5);
                                            var division2s = (from x in BLL.Funs.DB.WBS_Division
                                                              where x.SuperDivisionId == division1.DivisionId
                                                              orderby x.SortIndex
                                                              select x).ToList();
                                            if (list.Contains(division1.DivisionId))
                                            {
                                                newNode5.Expanded = true;
                                                foreach (var division2 in division2s)
                                                {
                                                    TreeNode newNode6 = new TreeNode();
                                                    newNode6.Text = division2.DivisionName;
                                                    newNode6.NodeID = division2.DivisionId;
                                                    newNode6.CommandName = "Division";
                                                    newNode6.EnableExpandEvent = true;
                                                    newNode6.EnableClickEvent = true;
                                                    newNode5.Nodes.Add(newNode6);
                                                    var division3s = (from x in BLL.Funs.DB.WBS_Division
                                                                      where x.SuperDivisionId == division2.DivisionId
                                                                      orderby x.SortIndex
                                                                      select x).ToList();
                                                    if (list.Contains(division2.DivisionId))
                                                    {
                                                        newNode6.Expanded = true;
                                                    }
                                                    else
                                                    {
                                                        if (division3s.Count > 0)
                                                        {
                                                            TreeNode tempNode2 = new TreeNode();
                                                            tempNode2.NodeID = "";
                                                            tempNode2.Text = "";
                                                            newNode6.Nodes.Add(tempNode2);
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                if (division2s.Count > 0)
                                                {
                                                    TreeNode tempNode2 = new TreeNode();
                                                    tempNode2.NodeID = "";
                                                    tempNode2.Text = "";
                                                    newNode5.Nodes.Add(tempNode2);
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (division1s.Count > 0)
                                        {
                                            TreeNode tempNode2 = new TreeNode();
                                            tempNode2.NodeID = "";
                                            tempNode2.Text = "";
                                            newNode4.Nodes.Add(tempNode2);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                TreeNode tempNode2 = new TreeNode();
                                tempNode2.NodeID = "";
                                tempNode2.Text = "";
                                newCNProfessionalNode.Nodes.Add(tempNode2);
                            }
                        }
                    }
                }
                else
                {
                    TreeNode tempNode2 = new TreeNode();
                    tempNode2.NodeID = "";
                    tempNode2.Text = "";
                    trWBS.Nodes[i].Nodes.Add(tempNode2);
                }
            }
            this.trWBS.SelectedNodeID = this.hdSelectId.Text;
            BindGrid();
        }

        private string GetCNProfessionalId(Model.WBS_Division division)
        {
            string id = string.Empty;
            if (division != null && !string.IsNullOrEmpty(division.CNProfessionalId))
            {
                id = division.CNProfessionalId;
            }
            else
            {
                Model.WBS_Division superDivision = BLL.DivisionService.GetDivisionById(division.SuperDivisionId);
                id = GetCNProfessionalId(superDivision);
            }
            return id;
        }

        private List<string> GetDivisionIds(Model.WBS_Division division)
        {
            List<string> list = new List<string>();
            if (division != null && !string.IsNullOrEmpty(division.SuperDivisionId))
            {
                list.Add(division.SuperDivisionId);
                Model.WBS_Division superDivision = BLL.DivisionService.GetDivisionById(division.SuperDivisionId);
                list.AddRange(GetDivisionIds(superDivision));
            }
            return list;
        }
        #endregion

        #region  行点击事件
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string id = e.RowID;
            if (e.CommandName.Equals("download"))
            {
                string menuId = Const.ControlPointMenuId;
                PageContext.RegisterStartupScript(Windowtt.GetShowReference(
                 String.Format("../../AttachFile/webuploader.aspx?type=-1&source=1&toKeyId={0}&path=FileUpload/Breakdown&menuId={1}", id + "m", menuId)));
            }
        }
        #endregion

        #region 获取按钮权限
        /// <summary>
        /// 获取按钮权限
        /// </summary>
        /// <param name="button"></param>
        /// <returns></returns>
        private void GetButtonPower()
        {
            if (Request.Params["value"] == "0")
            {
                return;
            }
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.ControlPointMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnAdd))
                {
                    this.btnNew.Hidden = false;
                    this.btnMenuAdd.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnModify))
                {
                    this.btnMenuEdit.Hidden = false;
                    this.btnMenuModify.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnDelete))
                {
                    this.btnMenuDelete.Hidden = false;
                    this.btnMenuDel.Hidden = false;
                }
            }
        }
        #endregion
    }
}