<!-- Filtres -->
<div class="card shadow-sm mb-4">
    <div class="card-body py-2">
        <?= form_open('users', ['method' => 'get', 'class' => 'row g-2 align-items-end']); ?>
            <div class="col-md-3">
                <label class="form-label small mb-1">Recherche</label>
                <input type="text" name="search" class="form-control form-control-sm" 
                       placeholder="Nom, email..." value="<?= htmlspecialchars($filters['search'] ?? ''); ?>">
            </div>
            <div class="col-md-2">
                <label class="form-label small mb-1">Rôle</label>
                <select name="role_id" class="form-select form-select-sm">
                    <option value="">Tous</option>
                    <?php foreach ($roles as $r): ?>
                    <option value="<?= $r->id; ?>" <?= ($filters['role_id'] ?? '') == $r->id ? 'selected' : ''; ?>>
                        <?= $r->label; ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label small mb-1">Localisation</label>
                <select name="location_id" class="form-select form-select-sm">
                    <option value="">Toutes</option>
                    <?php foreach ($locations as $loc): ?>
                    <option value="<?= $loc->id; ?>" <?= ($filters['location_id'] ?? '') == $loc->id ? 'selected' : ''; ?>>
                        <?= $loc->name; ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label small mb-1">Statut</label>
                <select name="status" class="form-select form-select-sm">
                    <option value="">Tous</option>
                    <option value="active"   <?= ($filters['status'] ?? '') == 'active'   ? 'selected' : ''; ?>>Actif</option>
                    <option value="inactive" <?= ($filters['status'] ?? '') == 'inactive' ? 'selected' : ''; ?>>Inactif</option>
                    <option value="suspended"<?= ($filters['status'] ?? '') == 'suspended'? 'selected' : ''; ?>>Suspendu</option>
                </select>
            </div>
            <div class="col-md-3 d-flex gap-2">
                <button class="btn btn-sm btn-primary"><i class="bi bi-search"></i> Filtrer</button>
                <a href="<?= site_url('users'); ?>" class="btn btn-sm btn-outline-secondary">Reset</a>
                <?php if ($this->auth_lib->has_permission('user_manage')): ?>
                <a href="<?= site_url('users/create'); ?>" class="btn btn-sm btn-success ms-auto">
                    <i class="bi bi-plus-lg"></i> Nouveau
                </a>
                <?php endif; ?>
            </div>
        <?= form_close(); ?>
    </div>
</div>

<!-- Tableau -->
<div class="card shadow-sm">
    <div class="table-responsive">
        <table class="table table-hover mb-0">
            <thead class="table-light">
                <tr>
                    <th>Nom complet</th>
                    <th>Username</th>
                    <th>Email</th>
                    <th>Rôle</th>
                    <th>Localisation</th>
                    <th>Statut</th>
                    <th>Dernier accès</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($users)): ?>
                <tr><td colspan="8" class="text-center text-muted py-4">Aucun utilisateur trouvé.</td></tr>
                <?php else: ?>
                <?php foreach ($users as $u): ?>
                <tr>
                    <td class="fw-semibold"><?= htmlspecialchars($u->full_name); ?></td>
                    <td><code><?= htmlspecialchars($u->username); ?></code></td>
                    <td><?= htmlspecialchars($u->email); ?></td>
                    <td><span class="badge bg-primary"><?= $u->role_label; ?></span></td>
                    <td><?= $u->location_name ?: '<span class="text-muted">—</span>'; ?></td>
                    <td>
                        <?php
                            $status_class = ['active' => 'success', 'inactive' => 'secondary', 'suspended' => 'danger'];
                            $status_label = ['active' => 'Actif', 'inactive' => 'Inactif', 'suspended' => 'Suspendu'];
                        ?>
                        <span class="badge bg-<?= $status_class[$u->status]; ?>">
                            <?= $status_label[$u->status]; ?>
                        </span>
                    </td>
                    <td class="small text-muted">
                        <?= $u->last_login_at ? date('d/m/Y H:i', strtotime($u->last_login_at)) : '—'; ?>
                    </td>
                    <td class="text-end">
                        <?php if ($this->auth_lib->has_permission('user_manage')): ?>
                        <div class="btn-group btn-group-sm">
                            <a href="<?= site_url('users/edit/' . $u->id); ?>" 
                               class="btn btn-outline-primary" title="Modifier">
                                <i class="bi bi-pencil"></i>
                            </a>
                            <a href="<?= site_url('users/toggle_status/' . $u->id); ?>" 
                               class="btn btn-outline-<?= $u->status === 'active' ? 'warning' : 'success'; ?>"
                               title="<?= $u->status === 'active' ? 'Désactiver' : 'Activer'; ?>"
                               onclick="return confirm('Confirmer le changement de statut ?');">
                                <i class="bi bi-<?= $u->status === 'active' ? 'pause-circle' : 'play-circle'; ?>"></i>
                            </a>
                            <a href="<?= site_url('users/reset_password/' . $u->id); ?>" 
                               class="btn btn-outline-secondary" title="Réinitialiser mot de passe">
                                <i class="bi bi-key"></i>
                            </a>
                        </div>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <!-- Pagination -->
    <?php if ($total > $limit): ?>
    <div class="card-footer bg-white d-flex justify-content-between align-items-center">
        <small class="text-muted"><?= $total; ?> utilisateur(s)</small>
        <nav>
            <ul class="pagination pagination-sm mb-0">
                <?php $total_pages = ceil($total / $limit); ?>
                <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                <li class="page-item <?= $i == $page ? 'active' : ''; ?>">
                    <a class="page-link" href="<?= site_url('users?' . http_build_query(array_merge($filters, ['page' => $i]))); ?>">
                        <?= $i; ?>
                    </a>
                </li>
                <?php endfor; ?>
            </ul>
        </nav>
    </div>
    <?php endif; ?>
</div>
