<?php require dirname(__DIR__) . '/layouts/app.php'; ?>

<section class="page-heading">
    <div>
        <p class="eyebrow">Administration</p>
        <h1>Utilisateurs</h1>
    </div>
    <?php if (Auth::can('users.create')): ?>
        <a class="button-link" href="<?= url('/users/create') ?>">Ajouter</a>
    <?php endif; ?>
</section>

<?php if (!empty($notice)): ?>
    <div class="alert alert-success"><?= e($notice) ?></div>
<?php endif; ?>

<section class="table-panel">
    <table>
        <thead>
            <tr>
                <th>Nom</th>
                <th>Email</th>
                <th>Role</th>
                <th>Statut</th>
                <th>Creation</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($users as $item): ?>
                <tr>
                    <td><?= e($item['name']) ?></td>
                    <td><?= e($item['email']) ?></td>
                    <td><span class="role-badge"><?= e($item['role']) ?></span></td>
                    <td><?= e($item['status']) ?></td>
                    <td><?= e($item['created_at']) ?></td>
                    <td class="actions">
                        <?php if (Auth::can('users.edit')): ?>
                            <a href="<?= url('/users/edit?id=' . $item['id']) ?>">Modifier</a>
                        <?php endif; ?>
                        <?php if (Auth::can('users.delete') && (int) $item['id'] !== (int) ($_SESSION['user_id'] ?? 0)): ?>
                            <form method="post" action="<?= url('/users/delete') ?>" onsubmit="return confirm('Supprimer cet utilisateur ?');">
                                <input type="hidden" name="_token" value="<?= csrf_token() ?>">
                                <input type="hidden" name="id" value="<?= (int) $item['id'] ?>">
                                <button class="link-danger" type="submit">Supprimer</button>
                            </form>
                        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</section>

<?php require dirname(__DIR__) . '/partials/pagination.php'; ?>

<?php require dirname(__DIR__) . '/layouts/app_footer.php'; ?>
