<?= $this->extend('layout/main') ?>
<?= $this->section('content') ?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h5 class="m-0">Liste des Factures</h5>
    <div class="d-flex gap-2">
        <a href="/invoices/import" class="btn btn-outline-primary"><i class="bi bi-file-earmark-excel"></i> Importer Excel</a>
        <a href="/invoices/create" class="btn btn-primary"><i class="bi bi-plus-lg"></i> Nouvelle Facture</a>
    </div>
</div>

<div class="table-card">
    <div class="row mb-3">
        <div class="col-md-4">
            <form method="GET" class="d-flex gap-2">
                <input type="text" name="search" class="form-control" placeholder="Rechercher..." value="<?= $search ?? '' ?>">
                <button type="submit" class="btn btn-outline-secondary"><i class="bi bi-search"></i></button>
            </form>
        </div>
        <div class="col-md-3">
            <form method="GET" id="statusFilter">
                <select name="status" class="form-select" onchange="this.form.submit()">
                    <option value="">Tous les statuts</option>
                    <option value="brouillon" <?= ($status ?? '') === 'brouillon' ? 'selected' : '' ?>>Brouillon</option>
                    <option value="envoyee" <?= ($status ?? '') === 'envoyee' ? 'selected' : '' ?>>Envoyee</option>
                    <option value="payee" <?= ($status ?? '') === 'payee' ? 'selected' : '' ?>>Payee</option>
                    <option value="partielle" <?= ($status ?? '') === 'partielle' ? 'selected' : '' ?>>Partielle</option>
                    <option value="retard" <?= ($status ?? '') === 'retard' ? 'selected' : '' ?>>Retard</option>
                </select>
            </form>
        </div>
    </div>

    <table class="table table-hover" id="invoicesTable">
        <thead>
            <tr>
                <th>N Facture</th>
                <th>Client</th>
                <th>Designation</th>
                <th>Date</th>
                <th>Echeance</th>
                <th class="text-end">Total TTC</th>
                <th>Statut</th>
                <th class="text-center">Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($invoices as $inv): ?>
                <tr>
                    <td><a href="/invoices/view/<?= $inv['id'] ?>" class="fw-bold text-decoration-none"><?= $inv['invoice_number'] ?></a></td>
                    <td><?= $inv['reference_client'] ?? 'N/A' ?></td>
                    <td><?= $inv['designation_global'] ?? 'N/A' ?></td>
                    <td><?= date('d/m/Y', strtotime($inv['date'])) ?></td>
                    <td><?= $inv['due_date'] ? date('d/m/Y', strtotime($inv['due_date'])) : '-' ?></td>
                    <td class="text-end fw-bold"><?= number_format($inv['total_ttc'], 2, ',', ' ') ?> TND</td>
                    <td><span class="badge-status status-<?= $inv['status'] ?>"><?= ucfirst($inv['status']) ?></span></td>
                    <td class="text-center">
                        <div class="btn-group btn-group-sm">
                            <a href="/invoices/view/<?= $inv['id'] ?>" class="btn btn-outline-primary" title="Voir"><i class="bi bi-eye"></i></a>
                            <a href="/invoices/pdf/<?= $inv['id'] ?>" class="btn btn-outline-danger" title="PDF"><i class="bi bi-file-pdf"></i></a>
                            <?php if ($inv['status'] === 'brouillon'): ?>
                                <a href="/invoices/edit/<?= $inv['id'] ?>" class="btn btn-outline-warning" title="Modifier"><i class="bi bi-pencil"></i></a>
                                <button class="btn btn-outline-danger" onclick="deleteInvoice(<?= $inv['id'] ?>)" title="Supprimer"><i class="bi bi-trash"></i></button>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?= $pager->links() ?>
</div>

<?= $this->endSection() ?>

<?= $this->section('scripts') ?>
<script>
function deleteInvoice(id) {
    Swal.fire({
        title: 'Confirmer la suppression?',
        text: 'Cette action est irreversible!',
        icon: 'warning',
        showCancelButton: true,
        confirmButtonText: 'Oui, supprimer',
        cancelButtonText: 'Annuler'
    }).then((result) => {
        if (result.isConfirmed) {
            fetch('/invoices/delete/' + id, {
                method: 'POST',
                headers: { 'X-Requested-With': 'XMLHttpRequest' }
            })
            .then(r => r.json())
            .then(data => {
                if (data.success) {
                    Swal.fire('Supprime!', '', 'success').then(() => location.reload());
                } else {
                    Swal.fire('Erreur', data.message, 'error');
                }
            });
        }
    });
}

$(document).ready(function() {
    $('#invoicesTable').DataTable({
        paging: false,
        info: false,
        searching: false,
        language: { url: '//cdn.datatables.net/plug-ins/1.13.7/i18n/fr-FR.json' }
    });
});
</script>
<?= $this->endSection() ?>
