<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

role_module_boot();

$pageTitle = 'Roles';
$roles = role_list();

require dirname(__DIR__, 2) . '/includes/header.php';
require dirname(__DIR__, 2) . '/includes/sidebar.php';
require dirname(__DIR__, 2) . '/includes/topbar.php';
require dirname(__DIR__, 2) . '/includes/flash.php';
?>
<section class="panel-card">
    <div class="panel-card-header mb-3">
        <div>
            <h2 class="h4 mb-0">Roles and permissions</h2>
        </div>
        <a class="btn btn-jlfc" href="<?php echo e(base_url('app/modules/roles/create.php')); ?>"><i class="bi bi-shield-plus"></i> Add role</a>
    </div>

    <div class="table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th>Role</th>
                    <th>Description</th>
                    <th>Permissions</th>
                    <th>Assigned users</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($roles as $role): ?>
                    <tr>
                        <td><strong><?php echo e($role['name']); ?></strong></td>
                        <td><?php echo e($role['description'] ?: 'No description'); ?></td>
                        <td><?php echo e((string) $role['permission_count']); ?></td>
                        <td><?php echo e((string) $role['user_count']); ?></td>
                        <td class="text-end">
                            <div class="btn-group btn-group-sm">
                                <a class="btn btn-outline-secondary" href="<?php echo e(base_url('app/modules/roles/view.php?id=' . (int) $role['id'])); ?>">View</a>
                                <a class="btn btn-outline-secondary" href="<?php echo e(base_url('app/modules/roles/edit.php?id=' . (int) $role['id'])); ?>">Edit</a>
                                <a class="btn btn-outline-danger" href="<?php echo e(base_url('app/modules/roles/delete.php?id=' . (int) $role['id'])); ?>">Delete</a>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>