<?php
$formatMoney = static fn($value): string => number_format((float)$value, 2, ',', ' ') . ' DH';
$formatQty = static fn($value): string => number_format((float)$value, 0, ',', ' ');
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="darlink-title mb-1">Commande <?= htmlspecialchars($commande['numero']) ?></h1>
        <p class="text-muted mb-0"><?= htmlspecialchars($commande['fournisseur_nom']) ?> — <?= htmlspecialchars(date('d/m/Y', strtotime((string)$commande['date_commande']))) ?></p>
    </div>
    <div class="d-flex gap-2">
        <a href="index.php?page=commandes_fournisseurs" class="btn btn-darlink-outline-secondary">← Liste</a>
        <a href="index.php?page=receptions_fournisseurs" class="btn btn-outline-secondary">Journal des réceptions</a>
        <?php
        $statutCommande = (string)($commande['statut'] ?? '');
        $commandeReceptionnee = in_array($statutCommande, ['partiellement_reçue', 'reçue'], true) || (int)($commande['est_receptionnee'] ?? 0) === 1;
        $commandeActive = (int)($commande['actif'] ?? 1) === 1;
        ?>
        <?php if ($commandeActive && !$commandeReceptionnee): ?>
            <a href="index.php?page=commandes_fournisseurs_edit&id=<?= (int)$commande['id'] ?>" class="btn btn-outline-secondary">Modifier</a>
        <?php endif; ?>
        <?php if ($commandeReceptionnee): ?>
            <a href="index.php?page=factures_fournisseurs_create&commande_id=<?= (int)$commande['id'] ?>" class="btn btn-outline-primary">Enregistrer la facture fournisseur</a>
        <?php endif; ?>
        <?php if ($commandeActive && !in_array((string)($commande['statut'] ?? ''), ['brouillon', 'annulée', 'reçue'], true)): ?>
            <a href="index.php?page=commandes_fournisseurs_receive&id=<?= (int)$commande['id'] ?>" class="btn btn-outline-success">Réceptionner</a>
        <?php elseif ($commandeReceptionnee): ?>
            <a href="index.php?page=commandes_fournisseurs_receive&id=<?= (int)$commande['id'] ?>" class="btn btn-outline-success disabled" aria-disabled="true">Réception terminée</a>
        <?php endif; ?>
    </div>
</div>

<?php if (!empty($receptions)): ?>
<div class="darlink-card p-4 mt-3">
    <h5 class="mb-3">Historique des réceptions</h5>
    <div class="table-responsive">
        <table class="table darlink-table align-middle">
            <thead><tr><th>Date</th><th>N° réception</th><th>Bon de livraison</th><th class="text-end">Quantité</th><th>Statut</th></tr></thead>
            <tbody>
            <?php foreach ($receptions as $reception): ?>
                <tr>
                    <td data-sort-value="<?= htmlspecialchars($reception['date_reception']) ?>"><?= date('d/m/Y', strtotime($reception['date_reception'])) ?></td>
                    <td><?= htmlspecialchars($reception['numero']) ?></td>
                    <td><?= htmlspecialchars($reception['reference_bl'] ?: '-') ?></td>
                    <td class="text-end" data-sort-value="<?= (float)$reception['quantite_totale'] ?>"><?= $formatQty($reception['quantite_totale']) ?></td>
                    <td><?= htmlspecialchars($reception['statut']) ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<div class="row g-3 mb-3">
    <div class="col-md-6">
        <div class="darlink-card p-4 h-100">
            <h5>Fournisseur</h5>
            <div class="mt-3">
                <strong><?= htmlspecialchars($commande['fournisseur_nom']) ?></strong><br>
                <span class="text-muted"><?= htmlspecialchars($commande['code_fournisseur']) ?></span><br>
                <?= htmlspecialchars($commande['ville'] ?: '') ?><br>
                <?= htmlspecialchars($commande['telephone'] ?: '') ?><br>
                <?= htmlspecialchars($commande['email'] ?: '') ?>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="darlink-card p-4 h-100">
            <h5>Commande</h5>
            <div class="row mt-3 g-2">
                <div class="col-md-6"><strong>Numéro :</strong> <?= htmlspecialchars($commande['numero']) ?></div>
                <div class="col-md-6"><strong>Statut :</strong> <?= htmlspecialchars($commande['statut']) ?></div>
                <div class="col-md-6"><strong>Date :</strong> <?= htmlspecialchars(date('d/m/Y', strtotime((string)$commande['date_commande']))) ?></div>
                <div class="col-md-6"><strong>Livraison prévue :</strong> <?= !empty($commande['date_livraison_prevue']) ? htmlspecialchars(date('d/m/Y', strtotime((string)$commande['date_livraison_prevue']))) : '-' ?></div>
                <div class="col-md-12"><strong>Référence fournisseur :</strong> <?= htmlspecialchars($commande['reference_fournisseur'] ?: '-') ?></div>
            </div>
        </div>
    </div>
</div>

<div class="darlink-card p-4 mb-3">
    <h5 class="mb-3">Lignes de commande</h5>
    <div class="table-responsive">
        <table class="table darlink-table align-middle">
            <thead>
            <tr>
                <th>Réf.</th>
                <th>Désignation</th>
                <th class="text-end">Qté.</th>
                <th class="text-end">Reçu</th>
                <th class="text-end">Reste</th>
                <th class="text-end">Prix achat HT</th>
                <th class="text-end">TVA</th>
                <th class="text-end">Total HT</th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($lines as $line): ?>
                <tr>
                    <td><?= htmlspecialchars($line['reference'] ?: '-') ?></td>
                    <td><?= htmlspecialchars($line['designation']) ?></td>
                    <td class="text-end"><?= $formatQty($line['quantite']) ?></td>
                    <td class="text-end"><?= $formatQty($line['quantite_recue'] ?? 0) ?></td>
                    <td class="text-end"><?= $formatQty(max(0, (float)$line['quantite'] - (float)($line['quantite_recue'] ?? 0))) ?></td>
                    <td class="text-end"><?= $formatMoney($line['prix_achat_ht']) ?></td>
                    <td class="text-end"><?= number_format((float)$line['taux_tva'], 2, ',', ' ') ?> %</td>
                    <td class="text-end"><strong><?= $formatMoney($line['total_ht']) ?></strong></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<div class="row g-3">
    <div class="col-md-8">
        <div class="darlink-card p-4 h-100">
            <h5>Observations</h5>
            <p class="text-muted mb-0 mt-3"><?= nl2br(htmlspecialchars(trim((string)($commande['observations'] ?? '')) ?: 'Aucune observation.')) ?></p>
        </div>
    </div>
    <div class="col-md-4">
        <div class="darlink-card p-4 h-100">
            <div class="d-flex justify-content-between mb-2"><span>Total HT</span><strong><?= $formatMoney($commande['total_ht']) ?></strong></div>
            <div class="d-flex justify-content-between mb-2"><span>TVA</span><strong><?= $formatMoney($commande['total_tva']) ?></strong></div>
            <hr>
            <div class="d-flex justify-content-between h5"><span>Total TTC</span><strong><?= $formatMoney($commande['total_ttc']) ?></strong></div>
        </div>
    </div>
</div>
