using System;
using System.Linq;
using BLL;
using System.Collections.Generic;

namespace FineUIPro.Web.SysManage
{
    public partial class SystemMenuSetMove : PageBase
    {
        #region 单位主键
        /// <summary>
        /// 单位主键
        /// </summary>
        public string MenuId
        {
            get
            {
                return (string)ViewState["MenuId"];
            }
            set
            {
                ViewState["MenuId"] = value;
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                 this.MenuId=Request.Params["MenuId"];
                this.ckMenuType.DataTextField = "Text";
                this.ckMenuType.DataValueField = "Value";
                this.ckMenuType.DataSource = BLL.DropListService.Sys_Menu_Type();
                this.ckMenuType.DataBind();
                this.InitMenuTree(String.Join(", ", this.ckMenuType.SelectedValueArray));

                if (!string.IsNullOrEmpty(MenuId))
                {
                    var menu=BLL.SysMenuService.GetSys_MenuById(MenuId);
                    if (menu != null)
                    {
                        //this.txtMenuName.Text = menu.MenuName; 
                        //this.txtUrl.Text = menu.Url;
                        //this.txtSortIndex.Text = menu.SortIndex.ToString();
                        //this.rbIsUsed.SelectedValue=menu.IsUsed.ToString()=="True"?"1":"0";

                    }
                    
                }
            }
        }
        #region 初始化树
        /// <summary>
        /// 初始化树
        /// </summary>
        /// <param name="menuList">菜单集合</param>
        private void InitMenuTree(string menuType)
        {
            this.tvMenu.Nodes.Clear();
            var menus = BLL.SysMenuService.GetMenuListByMenuType(menuType);
            if (menus.Count() > 0)
            {
                TreeNode rootNode = new TreeNode
                {
                    Text = "菜单",
                    NodeID = "0",
                    EnableCheckBox = true,
                    EnableCheckEvent = true,
                    Expanded = true
                };
                this.tvMenu.Nodes.Add(rootNode);
                this.BoundTree(rootNode.Nodes, menus, rootNode.NodeID);
            }
        }

        /// <summary>
        /// 遍历增加子节点
        /// </summary>
        /// <param name="nodes"></param>
        /// <param name="menuId"></param>
        private void BoundTree(TreeNodeCollection nodes, List<Model.Sys_Menu> sysMenus, string superMenuId)
        {
            var menus = sysMenus.Where(x => x.SuperMenu == superMenuId &&x.IsEnd==false).OrderBy(x => x.SortIndex);
            if (menus.Count() > 0)
            {
                foreach (var item in menus)
                {
                    TreeNode chidNode = new TreeNode
                    {
                        Text = item.MenuName,
                        NodeID = item.MenuId,
                        EnableCheckBox = true,
                        EnableCheckEvent = true
                    };
                    if (item.IsUsed == true)
                    {
                        chidNode.Checked = true;
                        chidNode.Expanded = true;
                        chidNode.Selectable = true;
                    }

                    nodes.Add(chidNode);
                    if (!item.IsEnd.HasValue || item.IsEnd == false)
                    {
                        this.BoundTree(chidNode.Nodes, sysMenus, item.MenuId);
                    }
                }
            }
        }
        #endregion
        protected void ckMenuType_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            this.InitMenuTree(String.Join(", ", this.ckMenuType.SelectedValueArray));
        }
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string MenuType = String.Join(", ", this.ckMenuType.SelectedValueArray);
            string newSuperMenuId = this.tvMenu.SelectedNodeID;
            if (MenuType=="")
            {
                Alert.ShowInParent("请选择菜单类型!", MessageBoxIcon.Warning);
                return;
            }
            if (newSuperMenuId == "")
            {
                Alert.ShowInParent("请选择要迁移到的菜单!", MessageBoxIcon.Warning);
                return;
            }

            var menu = BLL.SysMenuService.GetSys_MenuById(MenuId);
             if (menu != null)
            {
                menu.SuperMenu = newSuperMenuId;
                menu.MenuType = MenuType;
                SysMenuService.UpdateSys_Menu(menu);
            }
            var Menu_Child = SysMenuService.GetSupMenuListBySuperMenu(MenuId);
            foreach (var item in Menu_Child)
            {
                item.MenuType = MenuType;
                SysMenuService.UpdateSys_Menu(item);

            }

            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }


        #region 获取按钮权限
        /// <summary>
        /// 获取按钮权限
        /// </summary>
        /// <param name="button"></param>
        /// <returns></returns>
        private void GetButtonPower()
        {
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.UnitMenuId);
            if (buttonList.Count() > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion        

    }
}