<?php
$produits = $conn->query("SELECT id, nom FROM produits ORDER BY nom")->fetchAll(PDO::FETCH_ASSOC);
$articles = $conn->query("SELECT id, nom FROM articles ORDER BY nom")->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="modal fade" id="ajouterFicheModal" tabindex="-1">
  <div class="modal-dialog modal-lg">
    <form method="POST" action="/eagle_client_istanbulcups/api/production/ajouter_fiche.php">
      <div class="modal-content">

        <div class="modal-header bg-primary text-white">
          <h5 class="modal-title">Nouvelle fiche technique</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
        </div>

        <div class="modal-body">

          <!-- Produit fini -->
          <div class="mb-3">
            <label class="form-label">Produit fini *</label>
            <select name="produit_id" class="form-select" required>
              <option value="">— Sélectionner —</option>
              <?php foreach ($produits as $p): ?>
                <option value="<?= $p['id'] ?>"><?= htmlspecialchars($p['nom']) ?></option>
              <?php endforeach; ?>
            </select>
          </div>

          <hr>

          <h6>Matières premières</h6>

          <div id="ligneContainer">
            <div class="row ligne mb-2">
              <div class="col-md-6">
                <select name="article_id[]" class="form-select" required>
                  <option value="">— Article —</option>
                  <?php foreach ($articles as $a): ?>
                    <option value="<?= $a['id'] ?>"><?= htmlspecialchars($a['nom']) ?></option>
                  <?php endforeach; ?>
                </select>
              </div>

              <div class="col-md-4">
                <input type="number" step="0.001" name="quantite[]" class="form-control" placeholder="Quantité" required>
              </div>

              <div class="col-md-2">
                <button type="button" class="btn btn-danger btnRemove">✕</button>
              </div>
            </div>
          </div>

          <button type="button" id="btnAddLigne" class="btn btn-sm btn-secondary">
            ➕ Ajouter une ligne
          </button>

        </div>

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

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