<div class="modal fade" id="voirFacture<?= $f['id'] ?>" tabindex="-1">
  <div class="modal-dialog modal-xl modal-dialog-scrollable" style="max-width:95%;">
    <div class="modal-content">

      <!-- HEADER -->
      <div class="modal-header bg-primary text-white">
        <h5>
          Facture <?= htmlspecialchars($f['numero']) ?>
        </h5>
        <button class="btn-close" data-bs-dismiss="modal"></button>
      </div>

      <!-- BODY -->
      <div class="modal-body">

        <!-- INFOS -->
        <div class="card mb-3 p-3">
          <div class="row">

            <div class="col-md-3">
              <b>N° :</b><br>
              <?= htmlspecialchars($f['numero']) ?>
            </div>

            <div class="col-md-3">
              <b>Date :</b><br>
              <?= date('d/m/Y H:i', strtotime($f['date_facture'])) ?>
            </div>

            <div class="col-md-3">
              <b>Client :</b><br>
              <?= htmlspecialchars($f['client_nom']) ?>
            </div>

            <div class="col-md-3">
              <b>Statut :</b><br>
              <span class="badge bg-<?= 
                $f['statut']=='payee' ? 'success' :
                ($f['statut']=='partiellement_payee' ? 'warning' : 'danger')
              ?>">
                <?= ucfirst(str_replace('_',' ',$f['statut'])) ?>
              </span>
            </div>

          </div>
        </div>

        <!-- TABLE DETAILS -->
        <div class="table-responsive">
          <table class="table table-bordered table-striped table-sm">
            <thead class="table-dark">
              <tr>
                <th>Produit</th>
                <th class="text-center">Quantité</th>
                <th class="text-end">Prix unitaire</th>
                <th class="text-end">Total</th>
              </tr>
            </thead>

            <tbody>
            <?php
            $details = $conn->query("
              SELECT fd.*, p.nom AS produit
              FROM factures_clients_details fd
              JOIN produits p ON p.id = fd.produit_id
              WHERE fd.facture_id = ".intval($f['id'])
            )->fetchAll(PDO::FETCH_ASSOC);
            ?>

            <?php if (!empty($details)): ?>
              <?php foreach ($details as $d): ?>
              <tr>
                <td><?= htmlspecialchars($d['produit']) ?></td>
                <td class="text-center"><?= $d['quantite'] ?></td>
                <td class="text-end"><?= number_format($d['prix'],2,',',' ') ?> FCFA</td>
                <td class="text-end"><?= number_format($d['total'],2,',',' ') ?> FCFA</td>
              </tr>
              <?php endforeach; ?>
            <?php else: ?>
              <tr>
                <td colspan="4" class="text-center text-muted">
                  Aucun détail
                </td>
              </tr>
            <?php endif; ?>

            </tbody>

            <!-- TOTALS -->
            <tfoot class="table-light">

              <tr>
                <th colspan="3" class="text-end">Total HT</th>
                <th class="text-end">
                  <?= number_format($f['total_ht'],2,',',' ') ?> FCFA
                </th>
              </tr>

              <tr>
                <th colspan="3" class="text-end">Taxe</th>
                <th class="text-end">
                  <?= number_format($f['taxe'],2,',',' ') ?> FCFA
                </th>
              </tr>

              <tr>
                <th colspan="3" class="text-end">Remise</th>
                <th class="text-end">
                  <?= number_format($f['remise'],2,',',' ') ?> FCFA
                </th>
              </tr>

              <tr class="table-success">
                <th colspan="3" class="text-end">Total TTC</th>
                <th class="text-end">
                  <?= number_format($f['total_ttc'],2,',',' ') ?> FCFA
                </th>
              </tr>

            </tfoot>
          </table>
        </div>

      </div>

      <!-- FOOTER -->
      <div class="modal-footer">

        <!-- IMPRIMER -->
           <?php if($f['reste_a_payer'] == 0): ?>
            <a href="pages/rapports/utils/impression.php?type=facture&id=<?= $f['id'] ?>"
                target="_blank"
                 class="btn btn-secondary btn-sm">
                 <i class="fa fa-print"></i>
            </a>
            <?php endif; ?>

        <!-- ENCAISSER -->
        <?php if($f['statut'] != 'payee'): ?>
        <button class="btn btn-primary btnEncaisser"
                data-bs-toggle="modal"
                data-bs-target="#encaisser<?= $f['id'] ?>">
          <i class="fa fa-money-bill"></i> Encaisser
        </button>
        <?php endif; ?>

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

      </div>

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