<?php $formatQty = static fn($v): string => number_format((float)$v, 3, ',', ' '); ?>
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">Réceptions fournisseurs</h1>
        <p class="text-muted mb-0">Journal détaillé des livraisons et corrections de stock.</p>
    </div>
    <a href="index.php?page=commandes_fournisseurs" class="btn btn-outline-secondary">Commandes fournisseurs</a>
</div>

<form class="darlink-card p-3 mb-4" method="get">
    <input type="hidden" name="page" value="receptions_fournisseurs">
    <div class="row g-2">
        <div class="col-md-3"><input class="form-control" name="q" value="<?= htmlspecialchars($filters['q']) ?>" placeholder="Réception, BL, commande…"></div>
        <div class="col-md-3">
            <select class="form-select" name="fournisseur_id">
                <option value="0">Tous les fournisseurs</option>
                <?php foreach ($fournisseurs as $f): ?>
                    <option value="<?= (int)$f['id'] ?>" <?= (int)$filters['fournisseur_id'] === (int)$f['id'] ? 'selected' : '' ?>><?= htmlspecialchars($f['raison_sociale']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-2">
            <select class="form-select" name="statut">
                <option value="">Tous les statuts</option>
                <option value="active" <?= $filters['statut'] === 'active' ? 'selected' : '' ?>>Actives</option>
                <option value="annulée" <?= $filters['statut'] === 'annulée' ? 'selected' : '' ?>>Annulées</option>
            </select>
        </div>
        <div class="col-md-2"><input type="date" class="form-control" name="date_from" value="<?= htmlspecialchars($filters['date_from']) ?>"></div>
        <div class="col-md-2"><input type="date" class="form-control" name="date_to" value="<?= htmlspecialchars($filters['date_to']) ?>"></div>
        <div class="col-12 d-flex justify-content-end gap-2 mt-2">
            <a href="index.php?page=receptions_fournisseurs" class="btn btn-outline-secondary">Réinitialiser</a>
            <button class="btn darlink-btn-primary">Filtrer</button>
        </div>
    </div>
</form>

<div class="darlink-card p-3">
    <div class="table-responsive">
        <table class="table darlink-table align-middle">
            <thead><tr>
                <th>Date</th><th>Réception</th><th>BL fournisseur</th><th>Commande</th>
                <th>Fournisseur</th><th class="text-end">Quantité</th><th>Statut</th><th data-sortable="false">Actions</th>
            </tr></thead>
            <tbody>
            <?php foreach ($receptions as $r): ?>
                <tr>
                    <td data-sort-value="<?= htmlspecialchars($r['date_reception']) ?>"><?= date('d/m/Y', strtotime($r['date_reception'])) ?></td>
                    <td><?= htmlspecialchars($r['numero']) ?></td>
                    <td><?= htmlspecialchars($r['reference_bl'] ?: '-') ?></td>
                    <td><a href="index.php?page=commandes_fournisseurs_show&id=<?= (int)$r['commande_id'] ?>"><?= htmlspecialchars($r['commande_numero']) ?></a></td>
                    <td><?= htmlspecialchars($r['fournisseur_nom']) ?></td>
                    <td class="text-end" data-sort-value="<?= (float)$r['quantite_totale'] ?>"><?= $formatQty($r['quantite_totale']) ?></td>
                    <td><span class="badge <?= $r['statut'] === 'active' ? 'bg-success' : 'bg-secondary' ?>"><?= htmlspecialchars($r['statut']) ?></span></td>
                    <td>
                        <?php if ($r['statut'] === 'active'): ?>
                            <form method="post" action="index.php?page=receptions_fournisseurs_cancel" class="d-flex gap-1" onsubmit="return confirm('Annuler cette réception et corriger le stock ?');">
                                <input type="hidden" name="id" value="<?= (int)$r['id'] ?>">
                                <input class="form-control form-control-sm" name="motif" placeholder="Motif obligatoire" required>
                                <button class="btn btn-sm btn-outline-danger">Annuler</button>
                            </form>
                        <?php else: ?>
                            <small class="text-muted"><?= htmlspecialchars($r['motif_annulation'] ?: 'Annulée') ?></small>
                        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            <?php if (!$receptions): ?><tr><td colspan="8" class="text-center text-muted py-4">Aucune réception.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
