
<div class="modal fade" id="visualiserDevisModal<?= $d['id'] ?>" tabindex="-1" aria-labelledby="visualiserDevisModalLabel<?= $d['id'] ?>" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">

      <div class="modal-header bg-primary text-white">
        <h5 class="modal-title" id="visualiserDevisModalLabel<?= $d['id'] ?>">Détails du devis #<?= $d['id'] ?></h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Fermer"></button>
      </div>

    <div class="modal-body">
     <div class="row mb-3">
        <!-- Partie gauche -->
        <div class="col-md-6">
            <p><strong>Client :</strong> <?= $d['client_nom'] ?? '---' ?></p>
            <p><strong>Date :</strong> <?= $d['date_devis'] ?></p>
        </div>

        <!-- Partie droite -->
       <!--  <div class="col-md-6 text-end">
            <p><strong>Montant versé :</strong> <-?= number_format($d['montant_verse'], 2, ',', ' ') ?> F CFA</p>
            <p><strong>Reste à payer :</strong> <-?= number_format($d['reste_a_payer'], 2, ',', ' ') ?> F CFA</p>
        </div> -->
    </div>

    <table class="table table-bordered table-striped">
          <thead class="table-dark">
            <tr>
              <th>Produit</th>
              <th class="text-center">Quantité</th>
              <th class="text-end">PU</th>
              <th class="text-end">Sous-total</th>
            </tr>
          </thead>

          <tbody>
            <?php
          // DEBUG TEMPORAIRE
          echo '<!-- DEBUG devis '.$d['id'].' | details count = '.count($details).' -->';
          ?>
          <?php if (!empty($details)): ?>
            <?php foreach ($details as $dd): ?>
              <tr>
                <td><?= htmlspecialchars($dd['produit'], ENT_QUOTES, 'UTF-8') ?></td>
                <td class="text-center"><?= (int)$dd['quantite'] ?></td>
                <td class="text-end">
                  <?= number_format($dd['prix'], 2, ',', ' ') ?> F CFA
                </td>
                <td class="text-end">
                  <?= number_format($dd['quantite'] * $dd['prix'], 2, ',', ' ') ?> F CFA
                </td>
              </tr>
            <?php endforeach; ?>
          <?php else: ?>
            <tr>
              <td colspan="4" class="text-center text-muted">
                Aucun détail trouvé pour cette vente
              </td>
            </tr>
          <?php endif; ?>
          </tbody>

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

      <!-- Footer -->
      <div class="modal-footer">
        <a href="/pages/rapports/utils/impression.php?cat=ventes&type=facture_client&ticket=a4&id=<?= $d['id'] ?>" target="_blank" class="btn btn-success btn-sm">
          <i class="fa fa-print"></i>
        </a>
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Fermer</button>
      </div>

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