<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/iscms_layout.php';

$conn = iscms_init();
iscms_require_roles(['SUPER_ADMIN', 'ADMIN']);

$pageTitle = 'Roles';
$activeNav = 'users';

$sql = "SELECT r.id, r.role_name, r.role_code, r.description, r.is_system_role, r.is_active, r.created_at,
               d.dept_name
        FROM roles r
        LEFT JOIN departments d ON r.dept_id = d.id
        ORDER BY r.role_name ASC
        LIMIT 200";
$result = $conn->query($sql);
$rows = $result ? $result->fetch_all(MYSQLI_ASSOC) : [];
$dbError = $result ? null : $conn->error;

iscms_render_head($pageTitle);
iscms_render_sidebar($activeNav);
iscms_render_topbar($pageTitle);
?>
    <div class="page-header">
        <h1>Roles</h1>
        <p>Role catalogue used by users and menu access rules.</p>
    </div>

    <div class="section-card mb-4">
        <div class="section-header">
            <h2><i class="ri-links-line"></i> Related</h2>
        </div>
        <div class="quick-actions">
            <a class="qa-btn" href="role_permissions.php"><i class="ri-key-2-line"></i> Role permissions</a>
            <a class="qa-btn" href="list_users.php"><i class="ri-team-line"></i> Users</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-shield-user-line"></i> Roles</h2>
            <span class="text-muted" style="font-size:.75rem"><?= count($rows) ?> shown</span>
        </div>
        <div class="table-responsive">
            <?php if ($dbError): ?>
                <div class="alert alert-danger m-3" style="font-size:.85rem">Unable to load data: <?= iscms_h($dbError) ?></div>
            <?php else: ?>
            <table class="ssbs-table">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Code</th>
                        <th>Department</th>
                        <th>System role</th>
                        <th>Active</th>
                        <th>Description</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="6"><i class="ri-inbox-line me-2"></i>No roles</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <?php $d = (string) ($r['description'] ?? ''); $dDisp = strlen($d) > 100 ? substr($d, 0, 97) . '...' : $d; ?>
                        <tr>
                            <td><strong><?= iscms_h($r['role_name']) ?></strong></td>
                            <td class="iscms-mono"><small><?= iscms_h($r['role_code']) ?></small></td>
                            <td><small><?= iscms_val($r['dept_name'] ?? null) ?></small></td>
                            <td><?= !empty($r['is_system_role']) ? '<span class="badge bg-info-subtle text-primary" style="font-size:.65rem">Yes</span>' : '<span class="badge bg-secondary-subtle text-secondary" style="font-size:.65rem">No</span>' ?></td>
                            <td><?= !empty($r['is_active']) ? '<span class="badge bg-success-subtle text-success" style="font-size:.65rem">Yes</span>' : '<span class="badge bg-secondary-subtle text-secondary" style="font-size:.65rem">No</span>' ?></td>
                            <td><small><?= $dDisp !== '' ? iscms_h($dDisp) : '<span class="text-muted">-</span>' ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
