<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Access control &bull; <%= appName %></title>
  <%- headAssets %>
  <link rel="stylesheet" href="<%= asset('assets/css/access.css') %>">
</head>
<body>

  <%- topNav %>

  <main class="admin-container" id="access">

    <% for (const f of flashes) { %>
      <div class="flash-alert <%= f.type %>"><span><%= f.message %></span></div>
    <% } %>

    <div class="page-header">
      <div>
        <h1 class="page-title">Access control</h1>
        <p class="page-subtitle">What each account can do. A person keeps the access their role gives by default; here you can <b>allow</b> extra features or <b>deny</b> ones they would normally have. Deny always wins. Changes apply on the person's next click. Which applications someone sees on the dashboard is set under Users.</p>
      </div>
    </div>

    <nav class="ac-tabs" aria-label="Access control sections">
      <a class="ac-tab<%= tab === 'people' ? ' active' : '' %>" href="<%= url('admin/access?tab=people') %>"<%- tab === 'people' ? ' aria-current="page"' : '' %>>Accounts</a>
      <a class="ac-tab<%= tab === 'roles' ? ' active' : '' %>" href="<%= url('admin/access?tab=roles') %>"<%- tab === 'roles' ? ' aria-current="page"' : '' %>>Roles (defaults)</a>
    </nav>

    <% if (tab === 'people') { %>

      <div class="ac-split">
        <aside class="table-card ac-list" aria-label="Accounts">
          <div class="ac-list-head">
            <input type="search" class="admin-search-input ac-search" id="acSearch" placeholder="Search person or username..." aria-label="Search accounts" autocomplete="off">
            <select class="admin-select" id="acRole" aria-label="Role">
              <option value="">All</option>
              <% for (const r of roles) { %><option value="<%= r %>"><%= roleInfo[r].label %></option><% } %>
            </select>
          </div>
          <div class="ac-list-body" id="acList">
            <% for (const u of users) { %>
              <a class="ac-user<%= picked && Number(picked.id) === Number(u.id) ? ' active' : '' %><%= Number(u.is_active) === 1 ? '' : ' off' %>" href="<%= url('admin/access?tab=people&user=' + Number(u.id)) %>" data-role="<%= u.role %>" data-search="<%= ((u.full_name || '') + ' ' + u.username + ' ' + u.email).toLowerCase() %>"<%- picked && Number(picked.id) === Number(u.id) ? ' aria-current="true"' : '' %>>
                <span class="ac-user-name"><%= u.full_name || u.username %></span>
                <span class="ac-user-meta"><%= roleInfo[u.role] ? roleInfo[u.role].label : u.role %><%= Number(u.is_active) === 1 ? '' : ' · inactive' %><% if (overrideCounts[u.id]) { %> <b class="ac-custom"><%= overrideCounts[u.id] %> custom</b><% } %></span>
              </a>
            <% } %>
            <p class="ac-empty" id="acNone" hidden>No accounts match.</p>
          </div>
        </aside>

        <section class="table-card ac-detail" aria-label="Access of the selected account">
          <% if (!picked) { %>
            <div class="ac-placeholder"><span class="ac-shield" aria-hidden="true">&#10003;</span><p>Select an account to manage its access.</p></div>
          <% } else if (picked.role === 'admin') { %>
            <div class="ac-head">
              <div><b class="ac-name"><%= picked.full_name || picked.username %></b><span class="ac-sub"><%= picked.email %> &middot; Administrator</span></div>
            </div>
            <div class="ac-placeholder"><p>Administrators hold every permission and cannot be restricted, so the portal can never be locked out. Change the role under Users to customise this account.</p></div>
          <% } else { %>
            <form method="POST" action="<%= url('admin/access') %>" autocomplete="off" id="acForm">
              <%- csrfField %>
              <input type="hidden" name="action" value="save_user">
              <input type="hidden" name="id" value="<%= Number(picked.id) %>">
              <div class="ac-head">
                <div>
                  <b class="ac-name"><%= picked.full_name || picked.username %></b>
                  <span class="ac-sub"><%= picked.email %> &middot; <%= roleInfo[picked.role] ? roleInfo[picked.role].label : picked.role %><%= Number(picked.is_active) === 1 ? '' : ' · inactive' %></span>
                </div>
                <button type="submit" class="btn btn-primary" id="acSave" disabled>Save changes</button>
              </div>

              <% for (const g of groups) { %>
                <div class="ac-group"><%= g %></div>
                <% for (const p of permissions.filter((x) => x.group === g)) { %>
                  <% const viaRole = !!(table[picked.role] && table[picked.role].has(p.key)); const cur = overrides[p.key] || ''; const has = cur === 'ALLOW' ? true : cur === 'DENY' ? false : viaRole; %>
                  <div class="ac-row">
                    <div class="ac-row-text">
                      <b><%= p.label %></b><% if (p.sensitive) { %> <span class="ac-sens">SENSITIVE</span><% } %>
                      <span class="ac-desc"><%= p.description %></span>
                    </div>
                    <span class="ac-has<%= has ? ' yes' : '' %>" data-has="<%= p.key %>"><%= has ? 'HAS' : 'NO' %></span>
                    <select class="form-control ac-ov-select" name="ov_<%= p.key %>" data-key="<%= p.key %>" data-default="<%= viaRole ? 1 : 0 %>" data-initial="<%= cur %>" aria-label="<%= p.label %> access">
                      <option value="" <%= cur === '' ? 'selected' : '' %>>Role default (<%= viaRole ? 'allowed' : 'not allowed' %>)</option>
                      <option value="ALLOW" <%= cur === 'ALLOW' ? 'selected' : '' %>>Allow</option>
                      <option value="DENY" <%= cur === 'DENY' ? 'selected' : '' %>>Deny</option>
                    </select>
                  </div>
                <% } %>
              <% } %>
            </form>
          <% } %>
        </section>
      </div>

    <% } else { %>

      <div class="table-card section-gap">
        <div class="table-scroll">
          <table class="admin-table ac-matrix">
            <caption class="ac-caption">What every role holds right now</caption>
            <thead>
              <tr>
                <th scope="col">Permission</th>
                <% for (const r of roles) { %>
                  <th scope="col" class="ac-c"><a href="<%= url('admin/access?tab=roles&role=' + r) %>"><%= roleInfo[r].label %></a></th>
                <% } %>
              </tr>
            </thead>
            <tbody>
              <% for (const g of groups) { %>
                <tr class="ac-grouprow"><th scope="rowgroup" colspan="<%= roles.length + 1 %>"><%= g %></th></tr>
                <% for (const p of permissions.filter((x) => x.group === g)) { %>
                  <tr>
                    <th scope="row" class="ac-perm"><%= p.label %><% if (p.sensitive) { %> <span class="ac-sens">SENSITIVE</span><% } %><span class="ac-desc"><%= p.description %></span></th>
                    <% for (const r of roles) { %>
                      <td class="ac-c"><% if (r === 'admin' || table[r].has(p.key)) { %><span class="ac-yes" aria-label="allowed">&#10003;</span><% } else { %><span class="ac-no" aria-label="not allowed">&ndash;</span><% } %></td>
                    <% } %>
                  </tr>
                <% } %>
              <% } %>
            </tbody>
          </table>
        </div>
      </div>

      <div class="form-card section-gap">
        <h2 class="card-title">Edit a role</h2>
        <div class="fbtns ac-rolepick" role="group" aria-label="Role to edit">
          <% for (const r of roles) { %>
            <a class="fbtn" href="<%= url('admin/access?tab=roles&role=' + r) %>" aria-pressed="<%= roleKey === r ? 'true' : 'false' %>"><%= roleInfo[r].label %></a>
          <% } %>
        </div>
        <p class="card-note"><%= roleInfo[roleKey].about %></p>

        <% if (roleKey === 'admin') { %>
          <p class="card-note">Administrators hold every permission and cannot be restricted, so the portal can never be locked out. Edit one of the other roles.</p>
        <% } else { %>
          <form method="POST" action="<%= url('admin/access') %>" autocomplete="off">
            <%- csrfField %>
            <input type="hidden" name="action" value="save_role">
            <input type="hidden" name="role" value="<%= roleKey %>">
            <% for (const g of groups) { %>
              <h3 class="card-subtitle"><%= g %></h3>
              <div class="check-grid ac-grid">
                <% for (const p of permissions.filter((x) => x.group === g)) { %>
                  <label class="check-item ac-item">
                    <input type="checkbox" name="perm" value="<%= p.key %>" <%= table[roleKey].has(p.key) ? 'checked' : '' %>>
                    <span><b><%= p.label %></b><% if (p.sensitive) { %> <span class="ac-sens">SENSITIVE</span><% } %><span class="ac-desc"><%= p.description %></span></span>
                  </label>
                <% } %>
              </div>
            <% } %>
            <div class="form-actions">
              <button type="submit" class="btn btn-primary">Save <%= roleInfo[roleKey].label %></button>
            </div>
          </form>
          <form method="POST" action="<%= url('admin/access') %>" class="danger-zone">
            <%- csrfField %>
            <input type="hidden" name="action" value="reset_role">
            <input type="hidden" name="role" value="<%= roleKey %>">
            <button type="submit" class="btn btn-secondary btn-sm" data-confirm="Put <%= roleInfo[roleKey].label %> back to the built-in permissions?">Back to the built-in permissions</button>
          </form>
        <% } %>
      </div>

    <% } %>

  </main>

  <%- shellEnd %>
  <script src="<%= asset('assets/js/access.js') %>"></script>
</body>
</html>
