<!-- Filtres -->
<div class="card shadow-sm mb-4">
    <div class="card-body py-2">
        <?= form_open('expenses', ['method' => 'get', 'class' => 'row g-2 align-items-end']); ?>
            <div class="col-md-2">
                <label class="form-label small mb-1">Recherche</label>
                <input type="text" name="search" class="form-control form-control-sm"
                       placeholder="ID, fournisseur..." value="<?= htmlspecialchars($filters['search'] ?? ''); ?>">
            </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="pending"   <?= ($filters['status'] ?? '') == 'pending'   ? 'selected' : ''; ?>>En attente</option>
                    <option value="validated" <?= ($filters['status'] ?? '') == 'validated' ? 'selected' : ''; ?>>Validée</option>
                    <option value="rejected"  <?= ($filters['status'] ?? '') == 'rejected'  ? 'selected' : ''; ?>>Rejetée</option>
                    <option value="batched"   <?= ($filters['status'] ?? '') == 'batched'   ? 'selected' : ''; ?>>Batchée</option>
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label small mb-1">Catégorie</label>
                <select name="category_id" class="form-select form-select-sm">
                    <option value="">Toutes</option>
                    <?php foreach ($categories as $cat): ?>
                    <option value="<?= $cat->id; ?>" <?= ($filters['category_id'] ?? '') == $cat->id ? 'selected' : ''; ?>>
                        <?= $cat->label; ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-1">
                <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-1">
                <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-4 d-flex gap-2">
                <button class="btn btn-sm btn-primary"><i class="bi bi-search"></i> Filtrer</button>
                <a href="<?= site_url('expenses'); ?>" class="btn btn-sm btn-outline-secondary">Reset</a>
                <?php if ($this->auth_lib->has_permission('expense_create')): ?>
                <a href="<?= site_url('expenses/create'); ?>" class="btn btn-sm btn-success ms-auto">
                    <i class="bi bi-plus-lg"></i> Nouvelle dépense
                </a>
                <?php endif; ?>
            </div>
        <?= form_close(); ?>
    </div>
</div>

<?php if ($msg = $this->session->flashdata('warning')): ?>
<div class="alert alert-warning alert-dismissible fade show">
    <i class="bi bi-exclamation-triangle"></i> <?= $msg; ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<!-- Tableau -->
<div class="card shadow-sm">
    <div class="table-responsive">
        <table class="table table-hover mb-0">
            <thead class="table-light">
                <tr>
                    <th>ID</th>
                    <th>Date</th>
                    <th>Fournisseur</th>
                    <th>Description</th>
                    <th>Catégorie</th>
                    <th>Lieu</th>
                    <th class="text-end">Montant</th>
                    <th>Statut</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($expenses)): ?>
                <tr><td colspan="9" class="text-center text-muted py-4">Aucune dépense trouvée.</td></tr>
                <?php else: ?>
                <?php foreach ($expenses as $e): ?>
                <?php
                    $status_map = [
                        'draft'     => ['secondary', 'pencil',          'Brouillon'],
                        'pending'   => ['warning',   'hourglass-split', 'En attente'],
                        'validated' => ['success',   'check-circle',    'Validée'],
                        'rejected'  => ['danger',    'x-circle',        'Rejetée'],
                        'batched'   => ['info',      'collection',      'Batchée'],
                    ];
                    $s = $status_map[$e->status] ?? ['secondary', 'circle', $e->status];
                ?>
                <tr>
                    <td><a href="<?= site_url('expenses/view/' . $e->id); ?>" class="text-decoration-none"><code><?= $e->expense_id; ?></code></a></td>
                    <td class="small"><?= date('d/m/Y', strtotime($e->expense_date)); ?></td>
                    <td class="fw-semibold"><?= htmlspecialchars($e->supplier); ?></td>
                    <td class="small"><?= character_limiter(htmlspecialchars($e->description), 40); ?></td>
                    <td><span class="badge bg-light text-dark"><?= $e->category_label; ?></span></td>
                    <td><span class="badge bg-light text-dark"><i class="bi bi-geo-alt"></i> <?= $e->location_name; ?></span></td>
                    <td class="text-end fw-semibold"><?= number_format($e->amount, 0, ',', ' '); ?></td>
                    <td>
                        <span class="badge bg-<?= $s[0]; ?> <?= $s[0] == 'warning' ? 'text-dark' : ''; ?>">
                            <i class="bi bi-<?= $s[1]; ?>"></i> <?= $s[2]; ?>
                        </span>
                    </td>
                    <td class="text-end">
                        <a href="<?= site_url('expenses/view/' . $e->id); ?>" class="btn btn-sm btn-outline-primary">
                            <i class="bi bi-eye"></i>
                        </a>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

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