<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">
            <i class="bi bi-truck me-2"></i>Bons de livraison
        </h1>
        <p class="text-muted mb-0"><?= count($bons) ?> bon(s) enregistré(s)</p>
    </div>

    <a href="index.php?page=devis" class="btn darlink-btn-primary">
        <i class="bi bi-arrow-repeat me-1"></i> Créer depuis un devis
    </a>
</div>

<div class="darlink-card darlink-table-card">
    <div class="darlink-table-toolbar">
        <div class="darlink-table-toolbar-left">
            <div class="darlink-table-search">
                <i class="bi bi-search"></i>
                <input type="text" id="blSearch" placeholder="Rechercher un BL...">
            </div>

            <select id="blStatusFilter" class="darlink-table-filter">
                <option value="">Tous les statuts</option>
                <option value="préparé">Préparé</option>
                <option value="livré">Livré</option>
                <option value="facturé">Facturé</option>
            </select>
        </div>

        <div class="darlink-table-toolbar-right">
            <button type="button" class="darlink-toolbar-btn" onclick="location.reload();">
                <i class="bi bi-arrow-clockwise"></i> Actualiser
            </button>
        </div>
    </div>

    <div class="table-responsive darlink-table-wrapper">
        <table class="table align-middle darlink-table darlink-table-modern" id="blTable">
            <thead>
            <tr>
                <th class="darlink-sortable" data-column="0">Numéro <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="1">Client <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="2">Date <span class="sort-indicator">↕</span></th>
                <th class="darlink-sortable" data-column="3">Statut <span class="sort-indicator">↕</span></th>
                <th class="text-end darlink-sortable" data-column="4">Total TTC <span class="sort-indicator">↕</span></th>
                <th class="text-end">Actions</th>
            </tr>
            </thead>

            <tbody>
            <?php if (empty($bons)): ?>
                <tr>
                    <td colspan="6" class="text-center text-muted py-5">
                        Aucun bon de livraison pour le moment.
                    </td>
                </tr>
            <?php endif; ?>

            <?php foreach ($bons as $bon): ?>
                <tr data-edit-url="index.php?page=bons_livraison_show&id=<?= (int)$bon['id'] ?>"
                    data-status="<?= htmlspecialchars($bon['statut']) ?>">
                    <td>
                        <strong class="darlink-table-code">
                            <?= htmlspecialchars($bon['numero']) ?>
                        </strong>
                    </td>

                    <td class="fw-semibold">
                        <?= htmlspecialchars($bon['client_nom']) ?>
                    </td>

                    <td><?= htmlspecialchars($bon['date_bl']) ?></td>

                    <td>
                        <span class="darlink-badge darlink-badge-light">
                            <?= htmlspecialchars($bon['statut']) ?>
                        </span>
                    </td>

                    <td class="text-end fw-semibold">
                        <?= number_format((float)$bon['total_ttc'], 2, ',', ' ') ?> MAD
                    </td>

                    <td class="text-end darlink-actions">
                        <a class="darlink-icon-btn darlink-icon-btn-view"
                           href="index.php?page=bons_livraison_show&id=<?= (int)$bon['id'] ?>"
                           title="Voir">
                            <i class="bi bi-eye"></i>
                        </a>

                        <a class="darlink-icon-btn darlink-icon-btn-edit"
                           target="_blank"
                           href="index.php?page=bons_livraison_pdf&id=<?= (int)$bon['id'] ?>"
                           title="PDF">
                            <i class="bi bi-file-earmark-pdf"></i>
                        </a>

                        <form method="post"
                              action="index.php?page=factures_from_bl"
                              class="d-inline"
                              onsubmit="return confirm('Créer une facture depuis ce bon de livraison ?');">
                            <input type="hidden" name="bon_livraison_id" value="<?= (int)$bon['id'] ?>">
                            <button type="submit"
                                    class="darlink-icon-btn darlink-icon-btn-order"
                                    title="Facturer">
                                <i class="bi bi-receipt"></i>
                            </button>
                        </form>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<script>
function filterBl(){
    const s = (document.getElementById('blSearch')?.value || '').toLowerCase();
    const st = (document.getElementById('blStatusFilter')?.value || '').toLowerCase();

    document.querySelectorAll('#blTable tbody tr').forEach(r => {
        let ok = r.innerText.toLowerCase().includes(s);
        if (st && (r.dataset.status || '').toLowerCase() !== st) ok = false;
        r.style.display = ok ? '' : 'none';
    });
}

['blSearch','blStatusFilter'].forEach(id => {
    document.getElementById(id)?.addEventListener('input', filterBl);
    document.getElementById(id)?.addEventListener('change', filterBl);
});
</script>