using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.TestRun.DriverReport
{
    public partial class MonthReportPush : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string id = Request.Params["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    this.hdId.Text = id;
                }
                InitMenuTree();
            }
        }
        #region 初始化树
        /// 
        /// 初始化树
        /// 
        /// 单位集合
        private void InitMenuTree()
        {
            this.tvPerson.Nodes.Clear();
            var units = BLL.UnitService.GetUnitByProjectIdList(this.CurrUser.LoginProjectId);
            foreach (var item in units)
            {
                TreeNode rootNode = new TreeNode
                {
                    Text = item.UnitName,
                    NodeID = item.UnitId,
                    EnableCheckBox = true,
                    EnableCheckEvent = true,
                    Expanded = true
                };
                this.tvPerson.Nodes.Add(rootNode);
                this.BoundTree(rootNode.Nodes, rootNode.NodeID);
            }
        }
        /// 
        /// 遍历增加子节点
        /// 
        /// 
        /// 
        private void BoundTree(TreeNodeCollection nodes, string superMenuId)
        {
            var menus = BLL.UserService.GetUserByUnitId(this.CurrUser.LoginProjectId, superMenuId);
            foreach (var item in menus)
            {
                TreeNode chidNode = new TreeNode
                {
                    Text = item.Text,
                    NodeID = item.Value,
                    EnableCheckBox = true,
                    EnableCheckEvent = true
                };
                var monthReport = BLL.TestRun_MonthReportService.GetMonthReportById(this.hdId.Text.Trim());
                if (monthReport != null)
                {
                    if (!string.IsNullOrEmpty(monthReport.PushPerson))
                    {
                        if (monthReport.PushPerson.Contains(item.Value))
                        {
                            chidNode.Checked = true;
                            chidNode.Expanded = true;
                            chidNode.Selectable = true;
                        }
                    }
                }
                nodes.Add(chidNode);
            }
        }
        #endregion
        #region 全选、全不选
        /// 
        /// 全选、全不选
        /// 
        /// 
        /// 
        protected void tvPerson_NodeCheck(object sender, FineUIPro.TreeCheckEventArgs e)
        {
            if (e.Checked)
            {
                this.tvPerson.CheckAllNodes(e.Node.Nodes);
                SetCheckParentNode(e.Node);
            }
            else
            {
                this.tvPerson.UncheckAllNodes(e.Node.Nodes);
            }
        }
        /// 
        /// 选中父节点
        /// 
        /// 
        private void SetCheckParentNode(TreeNode node)
        {
            if (node.ParentNode != null && node.ParentNode.NodeID != "0")
            {
                node.ParentNode.Checked = true;
                if (node.ParentNode.ParentNode.NodeID != "0")
                {
                    SetCheckParentNode(node.ParentNode);
                }
            }
        }
        #endregion
        #region 提交推送人员
        /// 
        /// 提交推送人员
        /// 
        /// 
        /// 
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            #region 保存推送人员
            string users = string.Empty;
            TreeNode[] nodes = this.tvPerson.GetCheckedNodes();
            if (nodes.Length > 0)
            {
                foreach (TreeNode tn in nodes)
                {
                    if (tn.NodeID != "0")
                    {
                        users += tn.NodeID + ",";
                    }
                }
                if (!string.IsNullOrEmpty(users))
                {
                    users = users.Substring(0, users.LastIndexOf(','));
                }
                var monthReport = BLL.TestRun_MonthReportService.GetMonthReportById(this.hdId.Text.Trim());
                if (monthReport != null)
                {
                    monthReport.PushPerson = users;
                    Funs.DB.SubmitChanges();
                    ShowNotify("提交成功!", MessageBoxIcon.Success);
                    PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
                }
            }
            #endregion
        }
        #endregion
    }
}