<!-- Filtres -->
<div class="card shadow-sm mb-4">
    <div class="card-body py-2">
        <?= form_open('users/logs', ['method' => 'get', 'class' => 'row g-2 align-items-end']); ?>
            <div class="col-md-2">
                <label class="form-label small mb-1">Module</label>
                <select name="module" class="form-select form-select-sm">
                    <option value="">Tous</option>
                    <?php foreach (['auth','users','expenses','requests','cashboxes','transfers','batches','partner'] as $m): ?>
                    <option value="<?= $m; ?>" <?= ($filters['module'] ?? '') == $m ? 'selected' : ''; ?>><?= $m; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label small mb-1">Du</label>
                <input type="date" name="date_from" class="form-control form-control-sm" 
                       value="<?= $filters['date_from'] ?? ''; ?>">
            </div>
            <div class="col-md-2">
                <label class="form-label small mb-1">Au</label>
                <input type="date" name="date_to" class="form-control form-control-sm" 
                       value="<?= $filters['date_to'] ?? ''; ?>">
            </div>
            <div class="col-md-2">
                <button class="btn btn-sm btn-primary"><i class="bi bi-search"></i> Filtrer</button>
            </div>
        <?= form_close(); ?>
    </div>
</div>

<!-- Tableau des logs -->
<div class="card shadow-sm">
    <div class="table-responsive">
        <table class="table table-sm table-hover mb-0">
            <thead class="table-light">
                <tr>
                    <th>Date/Heure</th>
                    <th>Utilisateur</th>
                    <th>Action</th>
                    <th>Module</th>
                    <th>Réf.</th>
                    <th>Détails</th>
                    <th>IP</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($logs)): ?>
                <tr><td colspan="7" class="text-center text-muted py-4">Aucun log trouvé.</td></tr>
                <?php else: ?>
                <?php foreach ($logs as $log): ?>
                <tr>
                    <td class="small"><?= date('d/m/Y H:i:s', strtotime($log->created_at)); ?></td>
                    <td class="fw-semibold"><?= htmlspecialchars($log->full_name ?? '—'); ?></td>
                    <td>
                        <?php
                            $action_icons = [
                                'login' => 'box-arrow-in-right text-success',
                                'logout' => 'box-arrow-right text-muted',
                                'login_failed' => 'x-circle text-danger',
                                'user_created' => 'person-plus text-primary',
                                'user_updated' => 'pencil text-info',
                                'user_status_changed' => 'toggle-on text-warning',
                                'password_changed' => 'key text-secondary',
                                'password_reset' => 'key-fill text-warning',
                            ];
                            $icon = $action_icons[$log->action] ?? 'circle text-muted';
                        ?>
                        <i class="bi bi-<?= $icon; ?>"></i>
                        <span class="small"><?= $log->action; ?></span>
                    </td>
                    <td><span class="badge bg-light text-dark"><?= $log->module; ?></span></td>
                    <td class="small text-muted"><?= $log->record_id ?: '—'; ?></td>
                    <td class="small"><?= htmlspecialchars($log->details ?? ''); ?></td>
                    <td class="small text-muted"><?= $log->ip_address; ?></td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
