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

$title = "Matières premières";

$articles = $conn->query("
    SELECT 
        a.id,
        a.code,
        a.nom,
        a.stock_actuel,
        a.seuil_alerte,
        a.cout_achat,
        u.nom AS unite,
        u.id AS unite_id
    FROM articles a
    JOIN unites u ON u.id = a.unite_id
    ORDER BY a.nom
")->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="container-fluid">
    <h1 class="mt-4">Matières premières</h1>

    <div class="card">
        <div class="card-header d-flex justify-content-between">
            <span>Liste des matières</span>
            <button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#ajouterArticleModal">
                <i class="fa fa-plus"></i> Ajouter
            </button>
        </div>

        <div class="card-body">
            <table id="articlesTable" class="table table-bordered table-striped">
                <thead class="table-dark">
                    <tr>
                        <th>Code</th>
                        <th>Nom</th>
                        <th>Stock</th>
                        <th>Seuil</th>
                        <th>État</th>
                        <th>Coût achat</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($articles as $a): 
                    $stock_ok = $a['stock_actuel'] >= $a['seuil_alerte'];
                ?>
                    <tr>
                        <td><?= htmlspecialchars($a['code']) ?></td>
                        <td><?= htmlspecialchars($a['nom']) ?></td>
                        <td><?= number_format($a['stock_actuel'], 0, ',', ' ') ?> <?= $a['unite'] ?></td>
                        <td><?= $a['seuil_alerte'] ?></td>

                        <!-- BADGE STOCK -->
                        <td>
                            <?php if ($stock_ok): ?>
                                <span class="badge bg-success">🟢 Stock OK</span>
                            <?php else: ?>
                                <span class="badge bg-danger">🔴 Stock bas</span>
                            <?php endif; ?>
                        </td>

                        <td><?= number_format($a['cout_achat'], 2) ?></td>

                        <td>
                            <button class="btn btn-warning btn-sm"
                                    data-bs-toggle="modal"
                                    data-bs-target="#modifierArticleModal<?= $a['id'] ?>">
                                <i class="fa fa-edit"></i>
                            </button>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>


<?php include __DIR__ . "/modal_ajouter_matiere.php"; ?>
<?php foreach ($articles as $a): ?>
    <?php include __DIR__ . "/modal_modifier_matiere.php"; ?>
<?php endforeach; ?>

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

<script>
$('#articlesTable').DataTable({
    responsive: true,
    order: [[1, 'asc']],
    language: { url: "/eagle_client_istanbulcups/public/js/fr-FR.json" }
});
</script>
