
using PageAdmin.BLL;
using PageAdmin.Common;using PageAdmin.Utils.ORM;using PageAdmin.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace PageAdmin.Web.Areas.Admin.Controllers
{
    /// <summary>
    /// 后台首页
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    public class DefaultAttribute : ActionFilterAttribute
    {

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            CurrentUser currentUser = filterContext.Controller.ViewBag.CurrentUser;
            int siteId = filterContext.Controller.ViewBag.CurrentSiteId;
            int roleId = currentUser.RoleId;
            AdminMenuBLL adminMenuBLL = new AdminMenuBLL();
            IEnumerable<dynamic> menuList = adminMenuBLL.GetList().Where(c => c.State == 1);
            if (!currentUser.IsSuperAdmin)
            {
                RolePermissionsBLL rolePermissionsBLL = new RolePermissionsBLL();
                string menuIds = rolePermissionsBLL.GetMenuIds(roleId);

                if(string.IsNullOrEmpty(menuIds))
                {
                    menuList = menuList.Where(c => c.Id == 0);
                }
                else
                {
                    string[] arrMenuIds = menuIds.Split(',');
                    menuList = menuList.Where(c => arrMenuIds.Contains<string>((string)(c.Id.ToString())));
                }
            }
            filterContext.Controller.ViewBag.Menu = menuList.OrderBy(c=>c.Xuhao);
            filterContext.Controller.ViewBag.AdminPath = SystemConfig.Get().AdminPath;
            AdminInfoPermissionsBLL adminInfoPermissionsBLL = new AdminInfoPermissionsBLL();
            IEnumerable<dynamic> canAdminTableList= adminInfoPermissionsBLL.GetCanAdminTableList(currentUser.Uid);
            filterContext.Controller.ViewBag.CanAdminTableList = canAdminTableList.ToList().Where(c=>c.SiteId==siteId).OrderBy(c=>c.Xuhao);
        }

    }
}
