<%- include('../partials/header') %>
<%- include('../partials/adm-nav') %>

<p class="lead">Attribuez à chaque rôle les menus auxquels il a accès.</p>

<% if (saved) { %>
  <div class="notice">Accès enregistrés.</div>
<% } %>

<% if (roles.length === 0) { %>
  <p class="empty">Créez d'abord un <a href="/adm/roles/nouveau">rôle</a>.</p>
<% } else { %>
  <form method="POST" action="/adm/acces" class="access-form">
    <div class="table-wrap access-wrap">
      <table class="access-table">
        <thead>
          <tr>
            <th class="access-role">Rôle</th>
            <% menus.forEach(function(menu) { %>
              <th class="access-menu" title="<%= menu.path %>">
                <span class="access-menu-label"><%= menu.label %></span>
                <% if (menu.group === 'adm') { %>
                  <span class="access-menu-group">ADM</span>
                <% } %>
              </th>
            <% }) %>
          </tr>
        </thead>
        <tbody>
          <% roles.forEach(function(role) { %>
            <%
              const granted = accesMap.get(Number(role.id)) || new Set();
            %>
            <tr>
              <td class="access-role">
                <strong><%= role.nom %></strong>
                <% if (role.description) { %>
                  <span class="muted tiny"><%= role.description %></span>
                <% } %>
              </td>
              <% menus.forEach(function(menu) { %>
                <td class="access-check">
                  <label class="access-checkbox">
                    <input
                      type="checkbox"
                      name="acces"
                      value="<%= role.id %>::<%= menu.key %>"
                      <%= granted.has(menu.key) ? 'checked' : '' %>
                    >
                    <span class="access-box" aria-hidden="true"></span>
                    <span class="sr-only"><%= menu.label %></span>
                  </label>
                </td>
              <% }) %>
            </tr>
          <% }) %>
        </tbody>
      </table>
    </div>

    <div class="form-actions sticky-actions access-actions">
      <button type="submit" class="btn btn-primary">Enregistrer les accès</button>
      <a href="/adm" class="btn btn-ghost">Annuler</a>
    </div>
  </form>
<% } %>

<%- include('../partials/footer') %>
