<?php use App\Models\AdminUser; ?>

<div class="d-flex flex-column flex-lg-row justify-content-between align-items-lg-center gap-3 mb-4">
    <div>
        <h1 class="h3 fw-bold mb-1">Usuários do painel</h1>
        <p class="text-muted mb-0">Crie, edite, ative e desative acessos do painel de RH.</p>
    </div>
    <a href="<?= e(baseUrl('admin/usuarios/novo')) ?>" class="btn btn-primary">Novo usuário</a>
</div>

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

<?php if (!empty($error)): ?>
    <div class="alert alert-danger rounded-4"><?= e($error) ?></div>
<?php endif; ?>

<div class="card border-0 shadow-sm">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table align-middle">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Nome</th>
                        <th>E-mail</th>
                        <th>Tipo</th>
                        <th>Status</th>
                        <th>Criado em</th>
                        <th>Atualizado em</th>
                        <th class="text-end">Ações</th>
                    </tr>
                </thead>
                <tbody>
                <?php if (empty($users)): ?>
                    <tr>
                        <td colspan="8" class="text-center text-muted py-5">Nenhum usuário cadastrado.</td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($users as $user): ?>
                        <?php $isSelf = isset($adminUser['id']) && (int) $adminUser['id'] === (int) $user['id']; ?>
                        <tr>
                            <td>#<?= e((string) $user['id']) ?></td>
                            <td>
                                <div class="fw-semibold"><?= e($user['name']) ?></div>
                                <?php if ($isSelf): ?>
                                    <div class="small text-muted">Usuário logado</div>
                                <?php endif; ?>
                            </td>
                            <td><?= e($user['email']) ?></td>
                            <td>
                                <span class="badge <?= AdminUser::isAdmin($user) ? 'text-bg-dark' : 'text-bg-info' ?>">
                                    <?= e(AdminUser::roleLabel((string) $user['role'])) ?>
                                </span>
                            </td>
                            <td>
                                <span class="badge <?= (int) $user['is_active'] === 1 ? 'text-bg-success' : 'text-bg-secondary' ?>">
                                    <?= (int) $user['is_active'] === 1 ? 'Ativo' : 'Inativo' ?>
                                </span>
                            </td>
                            <td><?= e(formatDate(substr((string) $user['created_at'], 0, 10))) ?></td>
                            <td><?= e(formatDate(substr((string) $user['updated_at'], 0, 10))) ?></td>
                            <td class="text-end">
                                <div class="d-inline-flex flex-wrap justify-content-end gap-2">
                                    <a href="<?= e(baseUrl('admin/usuarios/' . $user['id'] . '/editar')) ?>" class="btn btn-sm btn-outline-dark">Editar</a>

                                    <form method="post" action="<?= e(baseUrl('admin/usuarios/' . $user['id'] . '/status')) ?>" class="d-inline">
                                        <?= csrf_field() ?>
                                        <button type="submit" class="btn btn-sm <?= (int) $user['is_active'] === 1 ? 'btn-outline-warning' : 'btn-outline-success' ?>">
                                            <?= (int) $user['is_active'] === 1 ? 'Desativar' : 'Ativar' ?>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
