@using System.Collections.Generic
@using System.Linq
@using MrCMS.Entities.People
@using MrCMS.Models
@using MrCMS.Web.Areas.Admin.Models
@model System.Collections.Generic.List<ACLRuleModel>
@{
    var roles = ViewData["roles"] as List<ACLRoleModel>;
}

@foreach (var app in Model.GroupBy(model => model.AppName).OrderByDescending(models => models.Key == "System").ThenBy(model => model.Key))
{
    <h5>@app.Key</h5>
    <table class="table table-condensed table-striped">
        <thead>
            <tr>
                <th class="col-lg-4">Type
                </th>
                <th class="col-lg-4">Operation
                </th>
                @foreach (var role in roles)
                {
                    <th class="col-lg-4">
                        @role.Name
                    </th>
                }
            </tr>
        </thead>
        <tbody>
            @foreach (var rule in app.OrderBy(model => model.Name))
            {
                foreach (var operation in rule.Operations.OrderBy(model => model.DisplayName))
                {
                <tr>
                    <td>
                        @rule.Name
                    </td>
                    <td>
                        @operation.DisplayName
                    </td>
                    @foreach (var role in operation.Roles)
                    {
                        if (role.RoleName != UserRole.Administrator)
                        {
                        <td>
                            @Html.CheckBox(string.Format("acl-{0}-{1}", role.Role.Name, operation.Key), role.IsAllowed)
                        </td>
                        }
                        else
                        {
                        <td>
                            <input type="checkbox" checked="checked" disabled="disabled" />
                        </td>
                        }
                    }
                </tr>
                }
            }
        </tbody>
    </table>
}