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

<?= $this->section('content') ?>

<div class="d-flex justify-content-between align-items-center mb-3">
    <h1 class="h5 mb-0">Roles</h1>
    <a href="<?= base_url('roles/create') ?>" class="btn btn-primary btn-sm">
        <i class="fa-solid fa-plus me-1"></i>Add Role
    </a>
</div>

<div class="stat-card">
    <div class="table-responsive">
        <table class="table align-middle mb-0">
            <thead>
                <tr>
                    <th>Role</th>
                    <th>Description</th>
                    <th>Permissions</th>
                    <th>Type</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($roles as $r): ?>
                    <tr>
                        <td class="fw-semibold"><?= esc($r['name']) ?></td>
                        <td class="text-muted small"><?= esc($r['description'] ?: '—') ?></td>
                        <td><span class="badge text-bg-light border"><?= (int) $permCounts[$r['id']] ?> assigned</span></td>
                        <td><?= $r['is_system'] ? '<span class="badge text-bg-secondary">System</span>' : '<span class="badge text-bg-light border">Custom</span>' ?></td>
                        <td class="text-end">
                            <a href="<?= base_url('roles/' . $r['id'] . '/edit') ?>" class="btn btn-sm btn-outline-secondary">
                                <i class="fa-solid fa-key me-1"></i>Edit / Permissions
                            </a>
                            <?php if (! $r['is_system']): ?>
                                <form action="<?= base_url('roles/' . $r['id'] . '/delete') ?>" method="post" class="d-inline" onsubmit="return confirm('Delete this role?');">
                                    <?= csrf_field() ?>
                                    <button type="submit" class="btn btn-sm btn-outline-danger"><i class="fa-solid fa-trash"></i></button>
                                </form>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

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