<?php
use App\Services\SetupDefaults;
$perms = $draft['permissions'] ?? [];
?>
<div class="sup-page">
  <?php \App\Core\View::partial('setup_nav', compact('section', 'dirty', 'canUndo', 'publishedAt')); ?>
  <form class="set-section ajax-form" action="<?= e(url('setup/save')) ?>" method="post">
    <?= csrf_field() ?>
    <input type="hidden" name="section" value="permissions">
    <div class="set-section-head">
      <div>
        <div class="set-section-title"><i class="bi bi-shield-check"></i> 7. Permissions</div>
        <p class="set-section-sub">Who can see or edit each section. Users, roles, and this Setup page stay with Admin.</p>
      </div>
      <button class="btn btn-primary" type="submit">Save draft</button>
    </div>
    <div class="sup-table-wrap">
      <table class="table table-soft sup-perm mb-0">
        <thead>
          <tr>
            <th>Section</th>
            <?php foreach (SetupDefaults::ROLES as $role): ?><th><?= e(ucfirst($role)) ?></th><?php endforeach; ?>
          </tr>
        </thead>
        <tbody>
          <?php foreach (SetupDefaults::MODULES as $module => $label): ?>
            <tr>
              <td><?= e($label) ?></td>
              <?php foreach (SetupDefaults::ROLES as $role):
                  $level = $perms[$role][$module] ?? (in_array($role, SetupDefaults::roleModules()[$module] ?? [], true) ? 'edit' : 'hide');
              ?>
                <td>
                  <select class="form-select form-select-sm" name="permissions[<?= e($role) ?>][<?= e($module) ?>]">
                    <option value="hide" <?= $level === 'hide' ? 'selected' : '' ?>>Hide</option>
                    <option value="view" <?= $level === 'view' ? 'selected' : '' ?>>View</option>
                    <option value="edit" <?= $level === 'edit' ? 'selected' : '' ?>>Edit</option>
                  </select>
                </td>
              <?php endforeach; ?>
            </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    </div>
  </form>
</div>
