using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace FineUIPro.Web.ProjectData
{
    public partial class UnitWork : PageBase
    {
        #region  页面加载
        /// 
        /// 页面加载
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetButtonPower();
                InitTreeMenu();
            }
        }
        #endregion
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            if (Request.Params["value"] == "0")
            {
                return;
            }
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.UnitWorkMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnAdd))
                {
                    this.btnMenuAdd.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnModify))
                {
                    this.btnMenuEdit.Hidden = false;
                }
                if (buttonList.Contains(BLL.Const.BtnDelete))
                {
                    this.btnMenuDelete.Hidden = false;
                }
            }
        }
        #endregion
        #region  加载树
        /// 
        /// 加载树
        /// 
        private void InitTreeMenu()
        {
            this.trProjects.Nodes.Clear();
            this.trProjects.ShowBorder = false;
            this.trProjects.ShowHeader = false;
            this.trProjects.EnableIcons = true;
            this.trProjects.AutoScroll = true;
            this.trProjects.EnableSingleClickExpand = true;
            Model.Base_Project project = BLL.ProjectService.GetProjectByProjectId(this.CurrUser.LoginProjectId);
            if (project != null)
            {
                TreeNode node = new TreeNode();
                node.Text = project.ProjectName;
                node.NodeID = project.ProjectId;
                node.CommandName = "project";
                node.EnableClickEvent = true;
                node.EnableExpandEvent = true;
                this.trProjects.Nodes.Add(node);
                TreeNode emptyNode = new TreeNode();
                emptyNode.Text = "";
                emptyNode.NodeID = "";
                node.Nodes.Add(emptyNode);
            }
        }
        #endregion
        #region  展开树
        /// 
        /// 展开树
        /// 
        /// 
        /// 
        protected void trProjects_NodeExpand(object sender, TreeNodeEventArgs e)
        {
            e.Node.Nodes.Clear();
            if (e.Node.CommandName == "project")  //展开项目节点
            {
                var unitWorks = from x in Funs.DB.WBS_UnitWork
                                    where x.ProjectId == e.Node.NodeID && (x.SuperUnitWork == null || x.SuperUnitWork == "0")
                                orderby x.UnitWorkCode
                                    select x;
                foreach (var unitWork in unitWorks)
                {
                    TreeNode newNode = new TreeNode(); 
                    newNode.Text = unitWork.UnitWorkName;
                    newNode.NodeID = unitWork.UnitWorkId;
                    newNode.CommandName = "unitWork";
                    newNode.EnableExpandEvent = true;
                    newNode.EnableClickEvent = true;
                    e.Node.Nodes.Add(newNode);
                    var installation2s = from x in Funs.DB.WBS_UnitWork where x.SuperUnitWork == unitWork.UnitWorkId orderby x.UnitWorkCode select x;
                    if (installation2s.Count() > 0)
                    {
                        TreeNode emptyNode = new TreeNode();
                        emptyNode.Text = "";
                        emptyNode.NodeID = "";
                        newNode.Nodes.Add(emptyNode);
                    }
                }
            }
            else if (e.Node.CommandName == "unitWork")  //展开装置/单元节点
            {
                var unitWorks = from x in Funs.DB.WBS_UnitWork
                                    where x.SuperUnitWork == e.Node.NodeID
                                    orderby x.UnitWorkCode
                                    select x;
                foreach (var unitWork in unitWorks)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Text = unitWork.UnitWorkName;
                    newNode.NodeID = unitWork.UnitWorkId;
                    newNode.CommandName = "unitWork";
                    newNode.EnableExpandEvent = true;
                    newNode.EnableClickEvent = true;
                    e.Node.Nodes.Add(newNode);
                    var installation3s = from x in Funs.DB.WBS_UnitWork where x.SuperUnitWork == unitWork.UnitWorkId orderby x.UnitWorkCode select x;
                    if (installation3s.Count() > 0)
                    {
                        TreeNode emptyNode = new TreeNode();
                        emptyNode.Text = "";
                        emptyNode.NodeID = "";
                        newNode.Nodes.Add(emptyNode);
                    }
                }
            }
        }
        #endregion
        #region  Tree点击事件
        /// 
        /// Tree点击事件
        /// 
        /// 
        /// 
        protected void trProjects_NodeCommand(object sender, TreeCommandEventArgs e)
        {
            if (this.Grid1.Rows.Count > 0)
            {
                SaveData();
            }
            BindGrid();
        }
        #endregion
        #region  保存方法
        /// 
        /// 保存方法
        /// 
        private void SaveData()
        {
            //string rowId = this.Grid1.Rows[0].RowID;
            //string type = this.Grid1.Rows[0].Values[4].ToString();
            //if (type == "unitProject")
            //{
            //    Model.Wbs_UnitProjectInit unitProject = BLL.UnitProjectInitService.GetUnitProjectInitByUnitProjectCode(rowId);
            //    if (unitProject != null)
            //    {
            //        BLL.UnitProjectInitService.UpdateUnitProjectInit(unitProject);
            //    }
            //}
            //else if (type == "wbsSet")
            //{
            //    Model.WBS_WbsSetInit wbsSet = BLL.WbsSetInitService.GetWbsSetInitByWbsSetCode(rowId);
            //    if (wbsSet != null)
            //    {
            //        var childWbsSets = BLL.WbsSetInitService.GetWbsSetInitsBySuperWbsSetCode(this.trProjects.SelectedNodeID);
            //        //wbsSet.StartDate = Convert.ToDateTime(this.Grid1.Rows[0].Values[2].ToString());
            //        //wbsSet.EndDate = Convert.ToDateTime(this.Grid1.Rows[0].Values[3].ToString());
            //        BLL.WbsSetInitService.UpdateWbsSetInit(wbsSet);
            //    }
            //}
        }
        #endregion
        #region  修改关闭窗口
        /// 
        /// 关闭窗口
        /// 
        /// 
        /// 
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            ShowNotify("修改成功!", MessageBoxIcon.Success);
            getWBSSet();
        }
        #endregion
        #region  拷贝关闭窗口
        /// 
        /// 拷贝关闭窗口
        /// 
        /// 
        /// 
        protected void Window2_Close(object sender, WindowCloseEventArgs e)
        {
            ShowNotify("增加成功!", MessageBoxIcon.Success);
            getWBSSet();
        }
        #endregion
        #region 右键增加、修改、删除方法
        /// 
        /// 右键修改事件
        /// 
        /// 
        /// 
        protected void btnMenuEdit_Click(object sender, EventArgs e)
        {
            if (this.trProjects.SelectedNode != null && this.trProjects.SelectedNode.CommandName != "project")
            {
                if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.UnitWorkMenuId, BLL.Const.BtnModify))
                {
                    this.hdSelectId.Text = this.trProjects.SelectedNode.NodeID;
                    var unitWork = BLL.UnitWorkService.GetUnitWorkByUnitWorkId(this.hdSelectId.Text);
                    if (unitWork!=null)
                    {
                        PageContext.RegisterStartupScript(Window2.GetShowReference(String.Format("UnitWorkEdit.aspx?Id={0}&&SuperId={1}", this.trProjects.SelectedNode.NodeID,unitWork.SuperUnitWork, "编辑 - ")));
                    }
                    else
                    {
                        PageContext.RegisterStartupScript(Window2.GetShowReference(String.Format("UnitWorkEdit.aspx?Id={0}", this.trProjects.SelectedNode.NodeID, "编辑 - ")));
                    }                    
                }
                else
                {
                    ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                }
            }
            else
            {
                ShowNotify("项目节点无法修改!", MessageBoxIcon.Warning);
            }
        }
        /// 
        /// 增加
        /// 
        /// 
        /// 
        protected void btnMenuAdd_Click(object sender, EventArgs e)
        {
            if (this.trProjects.SelectedNode != null)
            {
                if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.UnitWorkMenuId, BLL.Const.BtnAdd))
                {
                    string id = this.trProjects.SelectedNode.NodeID;
                    if (id != null)
                    {
                        var unitWork = BLL.UnitWorkService.GetUnitWorkByUnitWorkId(id);
                        if (unitWork != null)
                        {
                            string openUrl = String.Format("UnitWorkEdit.aspx?SuperId={0}", this.trProjects.SelectedNode.NodeID, "增加 - ");
                            PageContext.RegisterStartupScript(Window2.GetSaveStateReference(hdSelectId.ClientID)
                                    + Window2.GetShowReference(openUrl));
                        }
                        else
                        {
                            string openUrl = String.Format("UnitWorkEdit.aspx", "增加 - ");
                            PageContext.RegisterStartupScript(Window2.GetSaveStateReference(hdSelectId.ClientID)
                                    + Window2.GetShowReference(openUrl));
                        }
                    }
                }
                else
                {
                    ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                }
            }
            else
            {
                ShowNotify("请选择树节点!", MessageBoxIcon.Warning);
            }
        }
        /// 
        /// 右键删除事件
        /// 
        /// 
        /// 
        protected void btnMenuDelete_Click(object sender, EventArgs e)
        {
            if (this.trProjects.SelectedNode != null)
            {
                if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.UnitWorkMenuId, BLL.Const.BtnDelete))
                {
                    if (this.trProjects.SelectedNode.CommandName != "project")   //非专业节点可以删除
                    {
                        string id = this.trProjects.SelectedNode.NodeID;
                        Model.WBS_UnitWork unitWork = BLL.UnitWorkService.getUnitWorkByUnitWorkId(id);
                        if (unitWork.SuperUnitWork == "0")  //删除一级装置
                        {
                            BLL.UnitWorkService.DeleteUnitWorkById(id);
                            var unitWork2s = from x in Funs.DB.WBS_UnitWork where x.SuperUnitWork == id select x;
                            foreach (var unitWork2 in unitWork2s)
                            {
                                BLL.UnitWorkService.DeleteUnitWorkById(unitWork2.UnitWorkId);
                                var unitWork3s = from x in Funs.DB.WBS_UnitWork where x.SuperUnitWork == unitWork2.UnitWorkId select x;
                                foreach (var unitWork3 in unitWork3s)
                                {
                                    BLL.UnitWorkService.DeleteUnitWorkById(unitWork3.UnitWorkId);
                                }
                            }
                        }
                        else
                        {
                            Model.WBS_UnitWork unitWork2 = BLL.UnitWorkService.getUnitWorkByUnitWorkId(id);
                            Model.WBS_UnitWork unitWork1 = BLL.UnitWorkService.getUnitWorkByUnitWorkId(unitWork2.SuperUnitWork);
                            if (unitWork1.SuperUnitWork == "0")  //删除二级装置
                            {
                                BLL.UnitWorkService.DeleteUnitWorkById(id);
                                var unitWork3s = from x in Funs.DB.WBS_UnitWork where x.SuperUnitWork == id select x;
                                foreach (var unitWork3 in unitWork3s)
                                {
                                    BLL.UnitWorkService.DeleteUnitWorkById(unitWork3.UnitWorkId);
                                }
                            }
                            else    //删除二级装置
                            {
                                BLL.UnitWorkService.DeleteUnitWorkById(id);
                            }
                        }
                        ShowNotify("删除成功!", MessageBoxIcon.Success);
                        InitTreeMenu();
                    }
                    else
                    {
                        ShowNotify("项目节点无法删除!", MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
                }
            }
            else
            {
                ShowNotify("请选择树节点!", MessageBoxIcon.Warning);
            }
        }
        #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();
        }
        /// 
        /// 加载Grid
        /// 
        private void BindGrid()
        {
            List items = new List();
            if (this.trProjects.SelectedNode != null)
            {
                if (this.trProjects.SelectedNode.CommandName == "unitWork")
                {
                    Model.WBS_UnitWork unitWork = BLL.UnitWorkService.getUnitWorkByUnitWorkId(this.trProjects.SelectedNode.NodeID);
                    if (unitWork != null)
                    {
                        Model.UnitWork item = new Model.UnitWork();
                        if (unitWork.SuperUnitWork == "0")
                        {
                            item.UnitWorkId = unitWork.UnitWorkId;
                            item.SupUnitWorkCode = unitWork.UnitWorkCode;
                            item.SupUnitWorkName = unitWork.UnitWorkName;
                            items.Add(item);
                        }
                        else
                        {
                            item.UnitWorkId = unitWork.UnitWorkId;
                            var supUnitWork = BLL.UnitWorkService.getUnitWorkByUnitWorkId(unitWork.SuperUnitWork);
                            if (supUnitWork != null)
                            {
                                item.SupUnitWorkCode = supUnitWork.UnitWorkCode;
                                item.SupUnitWorkName = supUnitWork.UnitWorkName;
                            }
                            item.UnitWorkCode = unitWork.UnitWorkCode;
                            item.UnitWorkName = unitWork.UnitWorkName;
                            items.Add(item);
                        }
                    }
                }
                this.Grid1.DataSource = items;
                this.Grid1.DataBind();
            }
        }
        #endregion
        #region 根据所给Id定位到对应装置
        /// 
        /// 根据所给Id定位到对应装置
        /// 
        private void getWBSSet()
        {
            string unitWorkId = string.Empty;
            string pUnitWorkId = string.Empty;
            string ppUnitWorkId = string.Empty;
            string projectId = this.CurrUser.LoginProjectId;
            string id = this.hdSelectId.Text;
            Model.WBS_UnitWork installation = BLL.UnitWorkService.getUnitWorkByUnitWorkId(id);
            if (installation.SuperUnitWork == "0")  //一级装置
            {
                ppUnitWorkId = id;
            }
            else
            {
                if (BLL.UnitWorkService.IsCanAddUnitWork(id))   //二级装置
                {
                    pUnitWorkId = id;
                    ppUnitWorkId = installation.SuperUnitWork;
                }
                else   //三级装置
                {
                    unitWorkId = id;
                    pUnitWorkId = installation.SuperUnitWork;
                    Model.WBS_UnitWork pInstallation = BLL.UnitWorkService.getUnitWorkByUnitWorkId(installation.SuperUnitWork);
                    if (pInstallation != null)
                    {
                        ppUnitWorkId = pInstallation.SuperUnitWork;
                    }
                }
            }
            this.trProjects.Nodes.Clear();
            this.trProjects.ShowBorder = false;
            this.trProjects.ShowHeader = false;
            this.trProjects.EnableIcons = true;
            this.trProjects.AutoScroll = true;
            this.trProjects.EnableSingleClickExpand = true;
            Model.Base_Project project = BLL.ProjectService.GetProjectByProjectId(this.CurrUser.LoginProjectId);
            if (project != null)
            {
                TreeNode node = new TreeNode();
                node.Text = project.ProjectName;
                node.NodeID = project.ProjectId;
                node.EnableClickEvent = true;
                this.trProjects.Nodes.Add(node);
                node.Expanded = true;
                var unitWork1s = from x in Funs.DB.WBS_UnitWork
                                     where x.ProjectId == projectId && x.SuperUnitWork == "0"
                                     orderby x.UnitWorkCode
                                     select x;
                foreach (var unitWork1 in unitWork1s)
                {
                    TreeNode newNode1 = new TreeNode();
                    newNode1.Text = unitWork1.UnitWorkName;
                    newNode1.NodeID = unitWork1.UnitWorkId;
                    newNode1.CommandName = "unitWork";
                    newNode1.EnableExpandEvent = true;
                    newNode1.EnableClickEvent = true;
                    node.Nodes.Add(newNode1);
                    if (unitWork1.UnitWorkId == ppUnitWorkId)
                    {
                        newNode1.Expanded = true;
                        var unitWork2s = from x in Funs.DB.WBS_UnitWork where x.SuperUnitWork == unitWork1.SuperUnitWork orderby x.UnitWorkCode select x;
                        foreach (var unitWork2 in unitWork2s)
                        {
                            TreeNode newNode2 = new TreeNode();
                            newNode2.Text = unitWork2.UnitWorkName;
                            newNode2.NodeID = unitWork2.UnitWorkId;
                            newNode2.CommandName = "unitWork";
                            newNode2.EnableExpandEvent = true;
                            newNode2.EnableClickEvent = true;
                            newNode1.Nodes.Add(newNode2);
                            if (unitWork2.UnitWorkId == pUnitWorkId)
                            {
                                newNode2.Expanded = true;
                                var unitWork3s = from x in Funs.DB.WBS_UnitWork where x.SuperUnitWork == unitWork2.UnitWorkId orderby x.UnitWorkCode select x;
                                foreach (var unitWork3 in unitWork3s)
                                {
                                    TreeNode newNode3 = new TreeNode();
                                    newNode3.Text = unitWork3.UnitWorkName;
                                    newNode3.NodeID = unitWork3.UnitWorkId;
                                    newNode3.CommandName = "unitWork";
                                    newNode3.EnableExpandEvent = true;
                                    newNode3.EnableClickEvent = true;
                                    newNode2.Nodes.Add(newNode3);
                                }
                            }
                            else
                            {
                                if (BLL.UnitWorkService.IsExitsUnitWorkBySuperUnitWork(unitWork2.UnitWorkId))
                                {
                                    TreeNode emptyNode = new TreeNode();
                                    emptyNode.Text = "";
                                    emptyNode.NodeID = "";
                                    newNode2.Nodes.Add(emptyNode);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (BLL.UnitWorkService.IsExitsUnitWorkBySuperUnitWork(unitWork1.UnitWorkId))
                        {
                            TreeNode emptyNode = new TreeNode();
                            emptyNode.Text = "";
                            emptyNode.NodeID = "";
                            newNode1.Nodes.Add(emptyNode);
                        }
                    }
                }
            }
            this.trProjects.SelectedNodeID = this.hdSelectId.Text;
            BindGrid();
        }
        #endregion
    }
}