<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<h1 class="h3 mb-1">Role Management</h1>
<p class="text-muted mb-3">Control which nav sections each staff role sees.</p>

<div class="alert alert-light border small mb-4">
    <i class="bi bi-info-circle me-1" style="color: var(--brand);"></i>
    This controls what shows up in the sidebar for each role — it does not change what a role is allowed to reach at
    all. That's enforced separately, at the route level, by which of Front Desk / F&amp;B / Manager a staff member is
    assigned (see their profile under Staff). A module only appears here if that role could ever reach it in the
    first place.
</div>

<ul class="nav nav-tabs mb-3" id="roleTabs">
    <?php foreach ($lookups->staffRoles as $key => $meta): ?>
        <li class="nav-item">
            <a class="nav-link <?= $key === array_key_first($lookups->staffRoles) ? 'active' : '' ?>" data-bs-toggle="tab" href="#tab-<?= $key ?>">
                <?= esc($meta['label']) ?> <span class="badge bg-light text-dark border ms-1"><?= $staffCounts[$key] ?></span>
            </a>
        </li>
    <?php endforeach; ?>
</ul>

<div class="tab-content">
    <?php foreach ($lookups->staffRoles as $roleKey => $roleMeta):
        $modulesForRole = array_filter($lookups->permissionModules, static fn ($m) => in_array($roleKey, $m['roles'], true));
        $overrides = $permissionsByRole[$roleKey] ?? [];
    ?>
    <div class="tab-pane fade <?= $roleKey === array_key_first($lookups->staffRoles) ? 'show active' : '' ?>" id="tab-<?= $roleKey ?>">
        <form action="<?= site_url('role-management/' . $roleKey) ?>" method="post">
            <?= csrf_field() ?>
            <div class="card shadow-sm">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <span><?= esc($roleMeta['label']) ?> — Visible Sections</span>
                    <span class="text-muted small"><?= $staffCounts[$roleKey] ?> active staff</span>
                </div>
                <?php if (empty($modulesForRole)): ?>
                    <div class="card-body text-muted small">This role has no assignable nav modules.</div>
                <?php else: ?>
                <ul class="list-group list-group-flush">
                    <?php foreach ($modulesForRole as $moduleKey => $moduleMeta): ?>
                        <li class="list-group-item d-flex justify-content-between align-items-center">
                            <span><?= esc($moduleMeta['label']) ?></span>
                            <div class="form-check form-switch mb-0">
                                <input class="form-check-input" type="checkbox" role="switch" name="module_<?= esc($moduleKey) ?>" <?= ($overrides[$moduleKey] ?? true) ? 'checked' : '' ?>>
                            </div>
                        </li>
                    <?php endforeach; ?>
                </ul>
                <div class="card-body">
                    <button type="submit" class="btn btn-primary btn-sm">Save for <?= esc($roleMeta['label']) ?></button>
                </div>
                <?php endif; ?>
            </div>
        </form>
    </div>
    <?php endforeach; ?>
</div>

<?= $this->endSection() ?>
