<?php
require_once '../../includes/auth.php';
require_once '../../db.php';
require_once '../../includes/header.php';
require_once '../../includes/sidebar.php';

// Only admins can manage roles
if (($_SESSION['user_role'] ?? '') !== 'admin') {
    header('Location: ' . BASE_URL . '/dashboard.php');
    exit;
}

$success = isset($_GET['success']);
$deleted = isset($_GET['deleted']);

$roles = $conn->query("
    SELECT r.*,
           (SELECT COUNT(*) FROM users u WHERE u.role_id = r.id) AS user_count
    FROM roles r
    ORDER BY r.is_system DESC, r.name
")->fetch_all(MYSQLI_ASSOC);
?>

<div class="page-header">
    <h2><i class="fas fa-shield-alt" style="color:var(--primary);margin-right:8px;"></i>Roles & Permissions</h2>
    <a href="form.php" class="btn btn-primary"><i class="fas fa-plus"></i> New Role</a>
</div>

<?php if ($success): ?>
<div class="alert alert-success" style="background:#f0fdf4;border:1px solid #bbf7d0;border-radius:8px;padding:12px 16px;margin-bottom:16px;color:#166534;display:flex;align-items:center;gap:8px;">
    <i class="fas fa-check-circle"></i> Role saved successfully.
</div>
<?php endif; ?>
<?php if ($deleted): ?>
<div class="alert alert-success" style="background:#f0fdf4;border:1px solid #bbf7d0;border-radius:8px;padding:12px 16px;margin-bottom:16px;color:#166534;display:flex;align-items:center;gap:8px;">
    <i class="fas fa-check-circle"></i> Role deleted successfully.
</div>
<?php endif; ?>

<div class="panel">
    <div class="panel-header">
        <span class="panel-title">All Roles</span>
        <span style="font-size:12px;color:var(--text-muted);"><?= count($roles) ?> records</span>
    </div>
    <div style="overflow-x:auto;">
        <table>
            <thead>
                <tr>
                    <th>Role Name</th>
                    <th>Description</th>
                    <th>Users</th>
                    <th>Type</th>
                    <th style="text-align:center;">Actions</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($roles as $role): ?>
            <tr>
                <td><strong><?= htmlspecialchars($role['name']) ?></strong></td>
                <td style="color:var(--text-muted);"><?= htmlspecialchars($role['description'] ?? '') ?></td>
                <td><?= (int)$role['user_count'] ?></td>
                <td>
                    <?php if ($role['is_system']): ?>
                    <span class="badge badge-warning">System</span>
                    <?php else: ?>
                    <span class="badge badge-gray">Custom</span>
                    <?php endif; ?>
                </td>
                <td style="text-align:center;">
                    <a href="form.php?id=<?= $role['id'] ?>" class="btn btn-outline btn-sm"><i class="fas fa-edit"></i> Edit</a>
                    <?php if (!$role['is_system']): ?>
                    <a href="delete.php?id=<?= $role['id'] ?>"
                       class="btn btn-danger btn-sm"
                       onclick="return confirm('Delete this role? Users assigned to it will lose their role.')">
                        <i class="fas fa-trash"></i>
                    </a>
                    <?php endif; ?>
                </td>
            </tr>
            <?php endforeach; ?>
            <?php if (empty($roles)): ?>
            <tr><td colspan="5" style="text-align:center;color:var(--text-muted);padding:32px;">No roles found.</td></tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php require_once '../../includes/footer.php'; ?>
