<?= $this->extend('layouts/app') ?>

<?= $this->section('content') ?>
<div class="core-card p-4 mb-4">
    <div class="d-flex flex-column flex-lg-row justify-content-between gap-3">
        <div>
            <h2 class="h5 mb-1">Rôles et permissions</h2>
            <p class="text-muted mb-0">Un utilisateur reçoit un rôle. Les permissions se règlent ici, au niveau du rôle.</p>
        </div>
        <div class="text-muted small">
            Le rôle <strong>Super Admin</strong> garde toujours toutes les permissions.
        </div>
    </div>
</div>

<div class="row g-4">
    <?php foreach ($roles as $role): ?>
        <?php
            $roleId = (int) $role['id'];
            $selected = array_map('intval', $rolePermissions[$roleId] ?? []);
            $isLocked = $role['name'] === 'super_admin';
        ?>
        <div class="col-xl-4 col-lg-6">
            <div class="core-card p-4 h-100">
                <div class="d-flex justify-content-between gap-3 mb-3">
                    <div>
                        <h3 class="h5 mb-1"><?= esc($role['label']) ?></h3>
                        <div class="text-muted small"><code><?= esc($role['name']) ?></code></div>
                    </div>
                    <span class="badge <?= $isLocked ? 'text-bg-primary' : 'text-bg-light text-dark' ?>">
                        <?= $isLocked ? 'Complet' : count($selected) . ' permission(s)' ?>
                    </span>
                </div>

                <?php if ($isLocked): ?>
                    <div class="alert alert-info py-2 small">Ce rôle donne accès à tout le noyau et à tous les modules autorisés.</div>
                <?php endif; ?>

                <form method="post" action="<?= site_url('/roles/' . $roleId . '/permissions') ?>">
                    <?= csrf_field() ?>
                    <?php foreach ($permissionsByGroup as $group => $permissions): ?>
                        <div class="border rounded p-3 mb-3">
                            <div class="fw-semibold mb-2"><?= esc($group) ?></div>
                            <?php foreach ($permissions as $permission): ?>
                                <?php
                                    $permissionId = (int) $permission['id'];
                                    $checked = $isLocked || in_array($permissionId, $selected, true);
                                ?>
                                <div class="form-check mb-2">
                                    <input
                                        class="form-check-input"
                                        type="checkbox"
                                        name="permissions[]"
                                        value="<?= $permissionId ?>"
                                        id="role<?= $roleId ?>perm<?= $permissionId ?>"
                                        <?= $checked ? 'checked' : '' ?>
                                        <?= $isLocked ? 'disabled' : '' ?>
                                    >
                                    <label class="form-check-label" for="role<?= $roleId ?>perm<?= $permissionId ?>">
                                        <?= esc($permission['label']) ?>
                                        <span class="d-block text-muted small"><?= esc($permission['name']) ?></span>
                                    </label>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    <?php endforeach; ?>

                    <button class="btn btn-primary w-100" type="submit" <?= $isLocked ? 'disabled' : '' ?>>
                        Enregistrer les permissions
                    </button>
                </form>
            </div>
        </div>
    <?php endforeach; ?>
</div>
<?= $this->endSection() ?>
