<!-- En-tête -->
<div class="row mb-4">
    <div class="col-md-8">
        <p class="text-muted mb-0">Les dépenses validées sont regroupées en batchs hebdomadaires envoyés à la compagnie maritime.</p>
    </div>
    <div class="col-md-4 text-end">
        <?php if ($unbatched_count > 0 && $this->auth_lib->has_permission('batch_create')): ?>
        <a href="<?= site_url('batches/create'); ?>" class="btn btn-success">
            <i class="bi bi-plus-circle"></i> Créer un batch
            <span class="badge bg-white text-success ms-1"><?= $unbatched_count; ?> dépense(s)</span>
        </a>
        <?php elseif ($unbatched_count == 0): ?>
        <span class="text-muted"><i class="bi bi-check-circle text-success"></i> Aucune dépense en attente de batch</span>
        <?php endif; ?>
    </div>
</div>

<!-- Filtres -->
<div class="card shadow-sm mb-4">
    <div class="card-body py-2">
        <?= form_open('batches', ['method' => 'get', 'class' => 'row g-2 align-items-end']); ?>
            <div class="col-md-2">
                <label class="form-label small mb-1">Année</label>
                <select name="year" class="form-select form-select-sm">
                    <?php for ($y = date('Y'); $y >= date('Y') - 2; $y--): ?>
                    <option value="<?= $y; ?>" <?= ($filters['year'] ?? '') == $y ? 'selected' : ''; ?>><?= $y; ?></option>
                    <?php endfor; ?>
                </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="draft"        <?= ($filters['status'] ?? '') == 'draft'        ? 'selected' : ''; ?>>Brouillon</option>
                    <option value="finalized"    <?= ($filters['status'] ?? '') == 'finalized'    ? 'selected' : ''; ?>>Finalisé</option>
                    <option value="sent"         <?= ($filters['status'] ?? '') == 'sent'         ? 'selected' : ''; ?>>Envoyé</option>
                    <option value="acknowledged" <?= ($filters['status'] ?? '') == 'acknowledged' ? 'selected' : ''; ?>>Accusé</option>
                </select>
            </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 -->
<div class="card shadow-sm">
    <div class="table-responsive">
        <table class="table table-hover mb-0">
            <thead class="table-light">
                <tr>
                    <th>Batch ID</th>
                    <th>Semaine</th>
                    <th>Période</th>
                    <th class="text-center">Dépenses</th>
                    <th class="text-end">Total</th>
                    <th>Statut</th>
                    <th>Créé par</th>
                    <th>ZIP</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($batches)): ?>
                <tr><td colspan="9" class="text-center text-muted py-4">Aucun batch trouvé.</td></tr>
                <?php else: ?>
                <?php foreach ($batches as $b): ?>
                <?php
                    $status_map = [
                        'draft'        => ['secondary', 'pencil',       'Brouillon'],
                        'finalized'    => ['primary',   'check2-circle','Finalisé'],
                        'sent'         => ['success',   'send',         'Envoyé'],
                        'acknowledged' => ['info',      'check2-all',   'Accusé'],
                    ];
                    $s = $status_map[$b->status] ?? ['secondary', 'circle', $b->status];
                ?>
                <tr>
                    <td>
                        <a href="<?= site_url('batches/view/' . $b->id); ?>" class="text-decoration-none">
                            <code class="fw-bold"><?= $b->batch_id; ?></code>
                        </a>
                    </td>
                    <td>S<?= str_pad($b->week_number, 2, '0', STR_PAD_LEFT); ?> / <?= $b->year; ?></td>
                    <td class="small">
                        <?= date('d/m', strtotime($b->period_start)); ?> → <?= date('d/m/Y', strtotime($b->period_end)); ?>
                    </td>
                    <td class="text-center">
                        <span class="badge bg-light text-dark"><?= $b->expense_count; ?></span>
                    </td>
                    <td class="text-end fw-bold"><?= number_format($b->total_amount, 0, ',', ' '); ?></td>
                    <td>
                        <span class="badge bg-<?= $s[0]; ?>">
                            <i class="bi bi-<?= $s[1]; ?>"></i> <?= $s[2]; ?>
                        </span>
                    </td>
                    <td class="small"><?= htmlspecialchars($b->created_by_name); ?></td>
                    <td>
                        <?php if ($b->zip_path): ?>
                        <a href="<?= site_url('batches/download/' . $b->id); ?>" class="btn btn-sm btn-outline-primary" title="Télécharger ZIP">
                            <i class="bi bi-file-zip"></i>
                        </a>
                        <?php else: ?>
                        <span class="text-muted">—</span>
                        <?php endif; ?>
                    </td>
                    <td class="text-end">
                        <a href="<?= site_url('batches/view/' . $b->id); ?>" class="btn btn-sm btn-outline-primary">
                            <i class="bi bi-eye"></i>
                        </a>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
