<?php

declare(strict_types=1);

ob_start();

?>

<div class="d-flex justify-content-between align-items-center mb-4">

    <div>

        <h2 class="mb-1">
            Role Management
        </h2>

        <p class="text-muted mb-0">
            Manage system roles and access groups.
        </p>

    </div>

    <a
        href="/public/roles/create"
        class="btn btn-primary">

        <i class="bi bi-plus-lg me-1"></i>

        Add Role

    </a>

</div>

<?php if (!empty($permissionsUpdated)): ?>

    <div class="alert alert-success">
        Role permissions updated successfully.
    </div>

<?php endif; ?>
<?php if (!empty($created)): ?>

    <div class="alert alert-success">
        Role created successfully.
    </div>

<?php endif; ?>


<?php if (!empty($updated)): ?>

    <div class="alert alert-success">
        Role updated successfully.
    </div>

<?php endif; ?>


<?php if (!empty($statusChanged)): ?>

    <div class="alert alert-success">
        Role status updated successfully.
    </div>

<?php endif; ?>


<?php if (!empty($error)): ?>

    <div class="alert alert-danger">

        <?= htmlspecialchars(
            $error,
            ENT_QUOTES,
            'UTF-8'
        ) ?>

    </div>

<?php endif; ?>


<div class="card shadow-sm border-0">

    <div class="card-body">

        <div class="table-responsive">

            <table class="table table-hover align-middle">

                <thead>

                <tr>

                    <th>ID</th>

                    <th>Role</th>

                    <th>Description</th>

                    <th>Users</th>

                    <th>Permissions</th>

                    <th>Status</th>

                    <th class="text-end">
                        Actions
                    </th>

                </tr>

                </thead>

                <tbody>

                <?php if (empty($roles)): ?>

                    <tr>

                        <td
                            colspan="7"
                            class="text-center text-muted py-4">

                            No roles found.

                        </td>

                    </tr>

                <?php else: ?>

                    <?php foreach ($roles as $role): ?>

                        <tr>

                            <td>
                                <?= (int) $role['id'] ?>
                            </td>

                            <td>

                                <strong>

                                    <?= htmlspecialchars(
                                        $role['role_name'],
                                        ENT_QUOTES,
                                        'UTF-8'
                                    ) ?>

                                </strong>

                            </td>

                            <td>

                                <?= htmlspecialchars(
                                    (string) $role['description'],
                                    ENT_QUOTES,
                                    'UTF-8'
                                ) ?>

                            </td>

                            <td>

                                <span class="badge text-bg-light">

                                    <?= (int) $role['user_count'] ?>

                                </span>

                            </td>

                            <td>

                                <span class="badge text-bg-light">

                                    <?= (int) $role['permission_count'] ?>

                                </span>

                            </td>

                            <td>

                                <?php if (
                                    (int) $role['active'] === 1
                                ): ?>

                                    <span class="badge text-bg-success">
                                        Active
                                    </span>

                                <?php else: ?>

                                    <span class="badge text-bg-secondary">
                                        Inactive
                                    </span>

                                <?php endif; ?>

                            </td>

                            <td class="text-end">

                                <div class="btn-group">

                                    <a
                                        href="/public/roles/edit?id=<?= (int) $role['id'] ?>"
                                        class="btn btn-sm btn-outline-primary"
                                        title="Edit">

                                        <i class="bi bi-pencil"></i>

                                    </a>

                                    <a
                                        href="/public/roles/permissions?id=<?= (int) $role['id'] ?>"
                                        class="btn btn-sm btn-outline-dark"
                                        title="Permissions">

                                        <i class="bi bi-shield-check"></i>

                                    </a>

                                    <?php if (
                                        $role['role_name']
                                        !== 'Administrator'
                                    ): ?>

                                        <form
                                            method="POST"
                                            action="/public/roles/toggle-status"
                                            class="d-inline">

                                            <input
                                                type="hidden"
                                                name="id"
                                                value="<?= (int) $role['id'] ?>">

                                            <button
                                                type="submit"
                                                class="btn btn-sm <?= (int) $role['active'] === 1
                                                    ? 'btn-outline-danger'
                                                    : 'btn-outline-success' ?>">

                                                <i class="bi <?= (int) $role['active'] === 1
                                                    ? 'bi-x-circle'
                                                    : 'bi-check-circle' ?>">
                                                </i>

                                            </button>

                                        </form>

                                    <?php endif; ?>

                                </div>

                            </td>

                        </tr>

                    <?php endforeach; ?>

                <?php endif; ?>

                </tbody>

            </table>

        </div>

    </div>

</div>

<?php

$content = ob_get_clean();

$title = 'Role Management';

include APP_PATH .
    '/Modules/Administration/Views/layouts/admin.php';