@using MrCMS.ACL.Rules
@using MrCMS.Helpers
@using MrCMS.Models
@model IEnumerable<IAdminMenuItem>
@{
    var currentUrl = Request.Url.AbsolutePath;
}
<ul class="nav nav-pills nav-stacked">
    @if (Html.CanAccess<CoreACL>(CoreACL.ManagePages))
    {
        <li class="@(currentUrl == Url.Action("Index", "Webpage") ? "active" : "")"><a href="@Url.Action("Index", "Webpage")"><i class="fa fa-file-code-o"></i> @Resource("Admin Nav Pages", "Pages")</a></li>
    }

    @if (Html.CanAccess<CoreACL>(CoreACL.ManageMedia))
    {
        <li class="@(currentUrl == Url.Action("Index", "MediaCategory") ? "active" : "")"><a href="@Url.Action("Index", "MediaCategory")"><i class="fa fa-file-image-o"></i> @Resource("Admin Nav Media", "Media")</a></li>
    }

    @if (Html.CanAccess<CoreACL>(CoreACL.ManageLayouts))
    {
        <li class="@(currentUrl == Url.Action("Index", "Layout") ? "active" : "")"><a href="@Url.Action("Index", "Layout")"><i class="glyphicon glyphicon-th"></i> @Resource("Admin Nav Layout", "Layouts")</a></li>
    }
    @{
        List<IAdminMenuItem> adminMenuItems = Model.Where(item => item.CanShow).ToList();
    }
    @foreach (IAdminMenuItem item in adminMenuItems)
    {
        if (item.Children.Any())
        {
            int indexOf = adminMenuItems.IndexOf(item);
            <li class="dropdown-submenu @(currentUrl.Equals(item.Url, StringComparison.OrdinalIgnoreCase) ? "active" : "")" data-menu="@indexOf">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown-mrcms">
                    <i class="@item.IconClass"> </i>
                    @Resource("Nav Links - " + item.Text, item.Text)
                    <b class="caret"></b>

                </a>
                @RenderMenu(item.Children, new List<int> { indexOf }, currentUrl)

            </li>
        }
        else
        {
            <li class="@(currentUrl == item.Url ? "active" : "")"><a href="@item.Url"><i class="@item.IconClass"> </i> @item.Text</a></li>
        }
    }
</ul>
@helper RenderMenu(SubMenu subMenu, IEnumerable<int> indexOf, string currentUrl)
{
    <ul class="nav nav-pills nav-stacked dropdown-menu" data-menu="true">
        @{ List<ChildMenuItem> items = subMenu.Where(menuItem => menuItem.CanShow).ToList(); }
        @foreach (ChildMenuItem item in items)
        {
            if (item.Children != null && item.Children.Any())
            {
                int index = items.IndexOf(item);
                indexOf = indexOf.Concat(new List<int> { index });
                <li class="dropdown-submenu @(currentUrl.Equals(item.Url, StringComparison.OrdinalIgnoreCase) ? "active" : "")" data-menu="@string.Join("-", indexOf)">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown-mrcms">
                        @Resource("Nav Links - " + item.Text, item.Text)
                        <b class="caret"></b>
                    </a>
                    @RenderMenu(item.Children, indexOf, currentUrl)
                </li>
            }
            else
            {
                <li class="@(currentUrl.Equals(item.Url, StringComparison.OrdinalIgnoreCase) ? "active" : "")"> <a href="@item.Url">@item.Text</a></li>
            }
        }
    </ul>
    <div class="clearfix"></div>
}