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

<?= $this->section('content') ?>
<div class="row g-4">
    <div class="col-lg-8">
        <div class="core-card p-4">
            <div class="d-flex flex-column flex-lg-row justify-content-between gap-3 mb-3">
                <div>
                    <h2 class="h5 mb-1">Utilisateurs</h2>
                    <p class="text-muted mb-0">Les permissions ne sont pas choisies ici : elles sont héritées du rôle attribué.</p>
                </div>
                <a class="btn btn-outline-primary btn-sm align-self-lg-start" href="<?= site_url('/roles') ?>">Gérer les rôles</a>
            </div>
            <div class="table-responsive">
                <table class="table align-middle">
                    <thead>
                    <tr>
                        <th>Email</th>
                        <th>Nom</th>
                        <th>Rôle</th>
                        <th>Statut</th>
                        <th>Dernière connexion</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($users as $user): ?>
                        <?php $userRoles = $rolesByUser[(int) $user['id']] ?? []; ?>
                        <tr>
                            <td><?= esc($user['email']) ?></td>
                            <td><?= esc(trim(($user['firstname'] ?? '') . ' ' . ($user['lastname'] ?? ''))) ?></td>
                            <td>
                                <?php if ($userRoles === []): ?>
                                    <span class="text-muted">Aucun rôle</span>
                                <?php else: ?>
                                    <?php foreach ($userRoles as $role): ?>
                                        <span class="badge text-bg-light text-dark border me-1"><?= esc($role['label']) ?></span>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </td>
                            <td><span class="badge text-bg-success"><?= esc($user['status']) ?></span></td>
                            <td><?= esc($user['last_login_at'] ?? '-') ?></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="col-lg-4">
        <div class="core-card p-4">
            <h2 class="h5 mb-1">Créer un utilisateur</h2>
            <p class="text-muted small mb-3">Choisissez un rôle. Les permissions exactes se règlent dans la page Rôles.</p>
            <form method="post" action="<?= site_url('/users') ?>">
                <?= csrf_field() ?>
                <div class="mb-3">
                    <label class="form-label">Email</label>
                    <input class="form-control" name="email" type="email" value="<?= old('email') ?>" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">Mot de passe</label>
                    <input class="form-control" name="password" type="password" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">Prénom</label>
                    <input class="form-control" name="firstname" value="<?= old('firstname') ?>">
                </div>
                <div class="mb-3">
                    <label class="form-label">Nom</label>
                    <input class="form-control" name="lastname" value="<?= old('lastname') ?>">
                </div>
                <div class="mb-3">
                    <label class="form-label">Rôle</label>
                    <select class="form-select" name="role_id" required>
                        <?php foreach ($roles as $role): ?>
                            <option value="<?= esc($role['id']) ?>" <?= (string) old('role_id') === (string) $role['id'] ? 'selected' : '' ?>>
                                <?= esc($role['label']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <button class="btn btn-primary w-100" type="submit">Créer</button>
            </form>
        </div>
    </div>
</div>
<?= $this->endSection() ?>
