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

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

      <div class="modal-body">

        <div class="card mb-3 p-2">
          <div class="row">
            <div class="col-md-3"><b>N° :</b> <?= $c['numero'] ?></div>
            <div class="col-md-3"><b>Date :</b> <?= date('d/m/Y H:i', strtotime($c['date_commande'])) ?></div>
            <div class="col-md-3"><b>Client :</b> <?= $c['client_nom'] ?></div>
            <div class="col-md-3">
              <b>Statut :</b>
              <span class="badge bg-<?= $c['statut']=='livree'?'success':'warning' ?>">
                <?= $c['statut'] ?>
              </span>
            </div>
          </div>
        </div>

        <form id="formLivraison<?= $c['id'] ?>">
          <input type="hidden" name="commande_id" value="<?= $c['id'] ?>">

          <div class="table-responsive">
            <table class="table table-bordered table-sm">
              <thead class="table-dark">
                <tr>
                  <th>Produit</th>
                  <th>Cmd</th>
                  <th>Livré</th>
                  <th>Reste</th>
                  <th>Livrer</th>
                </tr>
              </thead>

              <tbody>
              <?php foreach ($details as $d): 
                $reste = $d['quantite'] - $d['quantite_livree'];
              ?>
              <tr>
                <td><?= $d['produit'] ?></td>
                <td><?= $d['quantite'] ?></td>
                <td><?= $d['quantite_livree'] ?></td>
                <td><span class="badge bg-danger"><?= $reste ?></span></td>

                <td style="width:120px;">
                  <input type="number"
                         name="livraisons[<?= $d['id'] ?>]"
                         class="form-control form-control-sm"
                         min="0"
                         max="<?= $reste ?>"
                         value="0">
                </td>
              </tr>
              <?php endforeach; ?>
              </tbody>
            </table>
          </div>
        </form>

      </div>

      <div class="modal-footer">
        <button class="btn btn-secondary" data-bs-dismiss="modal">Annuler</button>

        <button class="btn btn-primary btnConfirmLivraisonPartielle"
                data-id="<?= $c['id'] ?>">
          Confirmer
        </button>
      </div>

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

<script>
document.addEventListener("click", function(e){

  const btn = e.target.closest(".btnConfirmLivraisonPartielle");
  if(!btn) return;

  e.preventDefault();

  const id = btn.dataset.id;

  const form = document.getElementById("formLivraison"+id);

  if(!form){
    alert("Formulaire introuvable !");
    return;
  }

  const formData = new FormData(form);

  fetch("/eagle_client_istanbulcups/api/devis/livrer_partiel.php", {
    method: "POST",
    body: formData
  })
  .then(res => res.text()) //  IMPORTANT (pas json)
  .then(res => {

    console.log(res); // DEBUG

    // Si ton PHP redirige → reload
    window.location.href = "/eagle_client_istanbulcups/index.php?page=commandes_clients";

  })
  .catch(err => {
    console.error(err);
    alert("Erreur serveur");
  });

});
</script>