<div class="modal fade" id="voirCommande<?= $c['id'] ?>" tabindex="-1">
  <div class="modal-dialog modal-xl">
    <div class="modal-content">

      <div class="modal-header bg-info text-white">
        <h5>Commande <?= htmlspecialchars($c['numero']) ?></h5>
        <button class="btn-close" data-bs-dismiss="modal"></button>
      </div>

      <div class="modal-body">

        <div class="row mb-3">
          <div class="col-md-6">
            <p><strong>Client :</strong> <?= htmlspecialchars($c['client_nom']) ?></p>
            <p><strong>Date :</strong> <?= date('d/m/Y H:i', strtotime($c['date_commande'])) ?></p>
          </div>

          <div class="col-md-6 text-end">
            <p><strong>Statut :</strong>
              <?php if($c['statut']=='en_cours'): ?>
                <span class="badge bg-warning">En cours</span>
              <?php elseif($c['statut']=='partielle'): ?>
                <span class="badge bg-primary">Partiellement livrée</span>
              <?php elseif($c['statut']=='livree'): ?>
                <span class="badge bg-success">Livrée</span>
              <?php endif; ?>
            </p>
          </div>
        </div>

        <table class="table table-bordered table-striped">
          <thead class="table-dark">
            <tr>
              <th>Produit</th>
              <th class="text-center">Qté commandée</th>
              <th class="text-center">Qté livrée</th>
              <th class="text-center">Reste</th>
              <th class="text-end">PU</th>
              <th class="text-end">Sous-total</th>
            </tr>
          </thead>

          <tbody>
            <?php if (!empty($details)): ?>
              <?php foreach ($details as $d): 
                $livree = (float)($d['quantite_livree'] ?? 0);
                $reste  = (float)$d['quantite'] - $livree;
              ?>
              <tr>
                <td><?= htmlspecialchars($d['produit']) ?></td>

                <td class="text-center"><?= (float)$d['quantite'] ?></td>

                <td class="text-center">
                  <span class="badge bg-success">
                    <?= $livree ?>
                  </span>
                </td>

                <td class="text-center">
                  <?php if($reste > 0): ?>
                    <span class="badge bg-danger"><?= $reste ?></span>
                  <?php else: ?>
                    <span class="badge bg-secondary">0</span>
                  <?php endif; ?>
                </td>

                <td class="text-end">
                  <?= number_format($d['prix'], 2, ',', ' ') ?> F CFA
                </td>

                <td class="text-end">
                  <?= number_format($d['quantite'] * $d['prix'], 2, ',', ' ') ?> F CFA
                </td>
              </tr>
              <?php endforeach; ?>
            <?php else: ?>
              <tr>
                <td colspan="6" class="text-center text-muted">
                  Aucun détail trouvé
                </td>
              </tr>
            <?php endif; ?>
          </tbody>

          <tfoot class="table-light">
            <tr>
              <th colspan="5" class="text-end">Total HT</th>
              <th class="text-end"><?= number_format($c['total_ht'],2,',',' ') ?> F CFA</th>
            </tr>
            <tr>
              <th colspan="5" class="text-end">Taxe</th>
              <th class="text-end"><?= number_format($c['taxe'],2,',',' ') ?> F CFA</th>
            </tr>
            <tr>
              <th colspan="5" class="text-end">Remise</th>
              <th class="text-end"><?= number_format($c['remise'],2,',',' ') ?> F CFA</th>
            </tr>
            <tr class="table-success">
              <th colspan="5" class="text-end">Total TTC</th>
              <th class="text-end"><?= number_format($c['total_ttc'],2,',',' ') ?> F CFA</th>
            </tr>
          </tfoot>
        </table>

      </div>

      <div class="modal-footer">

        <!-- Impression -->
        <a href="/eagle_client_istanbulcups/pages/rapports/utils/impression.php?cat=ventes&type=commande_client&id=<?= $c['id'] ?>"
           target="_blank"
           class="btn btn-success btn-sm">
          <i class="fa fa-print"></i> Imprimer
        </a>

        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
          Fermer
        </button>

      </div>

    </div>
  </div>
</div>