<?= $this->include('partials/html') ?>

<head>
    <?php echo view("partials/title-meta", array("title" => $title)) ?>
    <?= $this->include('partials/head-css') ?>
</head>

<body>
    <div class="wrapper">
        <?= $this->include('partials/menu') ?>

        <div class="page-content">
            <div class="page-container">


                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">
                                <div class="d-flex flex-wrap justify-content-between align-items-center mb-3 gap-2">
                                    <h4 class="header-title mb-0">Produits avec marge < 10%</h4>
                                    <form method="get" class="d-flex gap-2">
                                        <input type="date" name="date_debut" value="<?= esc($dateDebut) ?>" class="form-control">
                                        <input type="date" name="date_fin" value="<?= esc($dateFin) ?>" class="form-control">
                                        <button type="submit" class="btn btn-primary">Filtrer</button>
                                    </form>
                                </div>

                                <div class="table-responsive">
                                    <table id="table-marges" class="table table-striped" data-exclusion-table="marges_list">
                                        <thead>
                                            <tr>
                                                <th>Produit</th>
                                                <th>SKU</th>
                                                <th class="text-end">Qté vendue</th>
                                                <th class="text-end">CA HT</th>
                                                <th class="text-end">Prix vente moyen</th>
                                                <th class="text-end">Prix achat</th>
                                                <th class="text-end">Marge</th>
                                                <th>Actions</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php if (empty($produits)): ?>
                                                <tr>
                                                    <td colspan="8" class="text-center">Aucun produit avec une marge < 10% sur cette période</td>
                                                </tr>
                                            <?php else: ?>
                                                <?php foreach ($produits as $produit): ?>
                                                    <tr data-exclusion-id="<?= esc($produit['product_id']) ?>" data-exclusion-label="<?= esc($produit['product_name']) ?>">
                                                        <td><?= esc($produit['product_name']) ?></td>
                                                        <td><code><?= esc($produit['sku']) ?></code></td>
                                                        <td class="text-end"><?= number_format($produit['qty_total'] ?? 0, 0, ',', ' ') ?></td>
                                                        <td class="text-end"><?= number_format($produit['ca_total'] ?? 0, 2, ',', ' ') ?> €</td>
                                                        <td class="text-end"><?= number_format($produit['prix_vente_moyen'] ?? 0, 2, ',', ' ') ?> €</td>
                                                        <td class="text-end"><?= number_format($produit['prix_achat'] ?? 0, 2, ',', ' ') ?> €</td>
                                                        <td class="text-end">
                                                            <span class="badge bg-danger">
                                                                <?= number_format($produit['marge_pct'] ?? 0, 2, ',', ' ') ?> %
                                                            </span>
                                                        </td>
                                                        <td>
                                                            <a href="/admin/marges/detail/<?= $produit['product_id'] ?>?date_debut=<?= esc($dateDebut) ?>&date_fin=<?= esc($dateFin) ?>" class="btn btn-sm btn-info">Commandes</a>
                                                        </td>
                                                    </tr>
                                                <?php endforeach; ?>
                                            <?php endif; ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>

    </div>

    <?= $this->include('partials/footer-scripts') ?>
    <?= $this->include('partials/datatables') ?>
    <?= $this->include('partials/exclusion-actions') ?>

    <script>
        $(document).ready(function() {
            initDataTable('#table-marges');
        });
    </script>
</body>

</html>
