<?php
require_once __DIR__ . "/../../config/db.php";
require_once __DIR__ . '/../../includes/check_auth.php';

$title = "Fiches Techniques";

// Récupérer toutes les fiches techniques
$fiches = $conn->query("
    SELECT ft.*, p.nom AS produit
    FROM fiches_techniques ft
    JOIN produits p ON ft.produit_id = p.id
    ORDER BY ft.id DESC
")->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="container-fluid">
    <h1 class="mt-4">Fiches Techniques</h1>
    <ol class="breadcrumb mb-4">
        <li class="breadcrumb-item"><a href="/eagle_client_istanbulcups/index.php?page=dashboard">Tableau de bord</a></li>
        <li class="breadcrumb-item active">Fiches Techniques</li>
    </ol>

    <div class="card mb-4">
        <div class="card-header d-flex justify-content-between align-items-center">
            <span><i class="fa fa-file-alt"></i> Liste des fiches</span>
            <button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#ajouterFicheModal">
                <i class="fas fa-plus"></i> Ajouter
            </button>
        </div>

        <div class="card-body">
            <table id="fichesTable" class="table table-bordered table-striped datatable">
                <thead class="table-dark">
                    <tr>
                        <th>ID</th>
                        <th>Produit</th>
                        <th>Fiche ID</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($fiches as $fiche): ?>
                    <tr>
                        <td><?= $fiche['id'] ?></td>
                        <td><?= htmlspecialchars($fiche['produit']) ?></td>
                        <td><?= htmlspecialchars($fiche['id']) ?></td>
                        <td>
                            <button class="btn btn-info btn-sm" data-bs-toggle="modal" data-bs-target="#visualiserFicheModal<?= $fiche['id'] ?>">
                                <i class="fa fa-eye"></i>
                            </button>
                            <button class="btn btn-danger btn-sm btnSupprimerFiche" data-id="<?= $fiche['id'] ?>">
                                <i class="fa fa-trash"></i>
                            </button>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include __DIR__ . "/modal_ajouter_fiche.php"; ?>

<?php foreach ($fiches as $fiche): ?>
    <?php
    // Charger les détails de la vente (produits, dépôts, etc.)
        $details = $conn->query("
        SELECT fl.quantite, a.nom, a.unite_id, u.nom AS unite
        FROM fiche_lignes fl
        JOIN articles a ON a.id = fl.article_id
        JOIN unites u ON u.id = a.unite_id
        WHERE fl.fiche_id = " . intval($fiche['id'])
    )->fetchAll(PDO::FETCH_ASSOC);

    ?>
    <?php include __DIR__ . "/modal_visualiser_fiche.php"; ?>

<?php endforeach; ?>

<?php include __DIR__ . '/../../includes/layout_end.php'; ?>

<script>
$(document).ready(function () {
    $('#fichesTable').DataTable({
        responsive: true,
        language: {
            url: "/eagle_client_istanbulcups/public/js/fr-FR.json"
        }
    });
});
</script>

<script>
document.addEventListener('DOMContentLoaded', () => {

  const container = document.getElementById('ligneContainer');
  const addBtn = document.getElementById('btnAddLigne');

  addBtn.addEventListener('click', () => {
    const ligne = container.querySelector('.ligne').cloneNode(true);
    ligne.querySelectorAll('input').forEach(i => i.value = '');
    container.appendChild(ligne);
  });

  container.addEventListener('click', e => {
    if (e.target.classList.contains('btnRemove')) {
      const lignes = container.querySelectorAll('.ligne');
      if (lignes.length > 1) {
        e.target.closest('.ligne').remove();
      }
    }
  });

});
</script>

