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

/* ============================
   LISTE DES APPROS
============================ */
$appos = $conn->query("
    SELECT 
        a.id,
        a.numero,
        a.date_appro,
        a.statut,
        a.annule,
        a.created_at,
        u.nom AS utilisateur
    FROM appro a
    LEFT JOIN utilisateurs u ON u.id = a.utilisateur_id
    ORDER BY a.id DESC
")->fetchAll(PDO::FETCH_ASSOC);

$title = "Approvisionnement";
?>

<div class="container-fluid px-2">

    <h1 class="mt-4">Entrée de stock</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">Appro</li>
    </ol>

    <div class="card mb-4">

        <div class="card-header d-flex justify-content-between align-items-center">
            <span><i class="fa fa-boxes"></i> Liste des entrées de stock</span>
            <button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#ajouterApproModal">
                <i class="fas fa-plus"></i> Nouvel entrée
            </button>
        </div>

        <div class="card-body">

            <table id="approTable" class="table table-bordered table-striped datatable">
                <thead class="table-dark">
                    <tr>
                        <th>#</th>
                        <th>Numéro</th>
                        <th>Date</th>
                        <th>Utilisateur</th>
                        <th>Statut</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($appos as $appro): ?>
                    <tr>
                        <td><?= $appro['id'] ?></td>
                        <td><?= htmlspecialchars($appro['numero']) ?></td>
                        <td><?= $appro['date_appro'] ?></td>
                        <td><?= htmlspecialchars($appro['utilisateur'] ?? '---') ?></td>
                        <td>
                            <?php if ($appro['annule']): ?>
                                <span class="badge bg-secondary">Annulé</span>
                            <?php else: ?>
                                <span class="badge bg-success">Validé</span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <?php if ($appro['annule'] !== 1): ?>
                              <!-- <button class="btn btn-danger btn-sm btnAnnulerAppro"
                                        data-bs-toggle="modal"
                                        data-bs-target="#modalAnnulerAppro"
                                        data-id="<-?= $appro['id'] ?>">
                                     <i class="fa fa-ban"></i>
                             </button> -->
                             <button class="btn btn-info btn-sm" 
                                        data-bs-toggle="modal" 
                                        data-bs-target="#visualiserApproModal<?= $appro['id'] ?>">
                                    <i class="fa fa-eye"></i>
                             </button>
                                <a href="/eagle_client_istanbulcups/api/appro/imprimer.php?id=<?= $appro['id'] ?>" 
                                   target="_blank" 
                                   class="btn btn-success btn-sm">
                                    <i class="fa fa-print"></i>
                                </a>
                                <!--?php else: ?-->
                                <!-- <span class="badge bg-secondary">Annulée</span> -->
                                <?php endif; ?>
                         </td>
                    </tr>
                  <?php endforeach; ?>
                </tbody>
            </table>

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


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

<?php foreach ($appos as $appro): ?>
    <?php

    $details = $conn->query("
    SELECT 
        p.nom AS produit,
        ad.quantite
    FROM appro_details ad
    JOIN produits p ON p.id = ad.produit_id
    JOIN appro a ON a.id = ad.appro_id
    WHERE ad.appro_id =" . intval($appro['id'])
    )->fetchAll(PDO::FETCH_ASSOC);
?>
<?php include __DIR__ . "/modal_visualiser_appro.php"; ?>
<?php include __DIR__ . "/modal_annuler_appro.php"; ?>
<?php endforeach; ?>

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

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

<script>
document.addEventListener("DOMContentLoaded", function() {
    const annulerButtons = document.querySelectorAll(".btnAnnulerVente");
    const inputId = document.getElementById("annulerAchatId");

    annulerButtons.forEach(btn => {
        btn.addEventListener("click", function() {
            inputId.value = this.dataset.id;
        });
    });
});
</script>
