<?= $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="page-title-box">
                            <h4 class="page-title"><?= esc($title) ?></h4>
                        </div>
                    </div>
                </div>

                <div class="row row-cols-xxl-4 row-cols-md-2 row-cols-1 g-3">
                    <div class="col">
                        <div class="card">
                            <div class="card-body">
                                <div class="d-flex justify-content-between align-items-center">
                                    <div>
                                        <p class="text-muted mb-1">Période 1</p>
                                        <h5 class="fw-semibold mb-0"><?= esc(date('d/m/Y', strtotime($p1Debut))) ?> → <?= esc(date('d/m/Y', strtotime($p1Fin))) ?></h5>
                                    </div>
                                    <div class="avatar-sm bg-primary rounded d-flex align-items-center justify-content-center">
                                        <i class="ti ti-calendar text-white fs-20"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col">
                        <div class="card">
                            <div class="card-body">
                                <div class="d-flex justify-content-between align-items-center">
                                    <div>
                                        <p class="text-muted mb-1">Période 2</p>
                                        <h5 class="fw-semibold mb-0"><?= esc(date('d/m/Y', strtotime($p2Debut))) ?> → <?= esc(date('d/m/Y', strtotime($p2Fin))) ?></h5>
                                    </div>
                                    <div class="avatar-sm bg-success rounded d-flex align-items-center justify-content-center">
                                        <i class="ti ti-calendar-event text-white fs-20"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col">
                        <div class="card">
                            <div class="card-body">
                                <div class="d-flex justify-content-between align-items-center">
                                    <div>
                                        <p class="text-muted mb-1">Produits en déclin</p>
                                        <h3 class="fw-semibold mb-0"><?= number_format(count($produits), 0, ',', ' ') ?></h3>
                                    </div>
                                    <div class="avatar-sm bg-warning rounded d-flex align-items-center justify-content-center">
                                        <i class="ti ti-trending-down text-white fs-20"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="col">
                        <div class="card">
                            <div class="card-body">
                                <div class="d-flex justify-content-between align-items-center">
                                    <div>
                                        <p class="text-muted mb-1">CA perdu vs P1</p>
                                        <h3 class="fw-semibold mb-0 text-danger"><?= number_format($totalCaPerdu, 0, ',', ' ') ?> €</h3>
                                    </div>
                                    <div class="avatar-sm bg-danger rounded d-flex align-items-center justify-content-center">
                                        <i class="ti ti-currency-euro text-white fs-20"></i>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row mt-4">
                    <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">
                                    <div>
                                        <h4 class="header-title mb-1">Paramètres de comparaison</h4>
                                        <p class="text-muted mb-0 small">
                                            Période 2 = dernière fenêtre glissante, Période 1 = fenêtre précédente de même taille.
                                        </p>
                                    </div>
                                    <form method="get" class="d-flex gap-2 align-items-center flex-wrap">
                                        <label class="text-nowrap mb-0">Fin</label>
                                        <input type="date" name="date_fin" value="<?= esc($dateFin) ?>" class="form-control">
                                        <label class="text-nowrap mb-0">Fenêtre (j)</label>
                                        <input type="number" name="fenetre" value="<?= (int) $fenetreJours ?>" class="form-control" style="max-width:90px">
                                        <label class="text-nowrap mb-0">Seuil (%)</label>
                                        <input type="number" name="seuil" value="<?= (float) $seuilDeclin ?>" step="1" class="form-control" style="max-width:90px">
                                        <button type="submit" class="btn btn-primary">Comparer</button>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row mt-4">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">
                                <h4 class="header-title mb-3">Produits en déclin</h4>
                                <div class="table-responsive">
                                    <table id="table-declin-produits" class="table table-striped">
                                        <thead>
                                            <tr>
                                                <th>SKU</th>
                                                <th>Produit</th>
                                                <th>Marque</th>
                                                <th>Statut</th>
                                                <th class="text-end">Qté P1</th>
                                                <th class="text-end">CA P1</th>
                                                <th class="text-end">Qté P2</th>
                                                <th class="text-end">CA P2</th>
                                                <th class="text-end">Var. CA</th>
                                                <th class="text-end">Var. %</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php if (empty($produits)): ?>
                                                <tr>
                                                    <td colspan="10" class="text-center">Aucun produit en déclin sur cette période avec ce seuil.</td>
                                                </tr>
                                            <?php else: ?>
                                                <?php foreach ($produits as $produit): ?>
                                                    <tr>
                                                        <td><code><?= esc($produit['sku']) ?></code></td>
                                                        <td><?= esc($produit['product_name']) ?></td>
                                                        <td><?= esc($produit['brand'] ?: '-') ?></td>
                                                        <td>
                                                            <?php if (($produit['status_value'] ?? 0) == 1): ?>
                                                                <span class="badge bg-success">Actif</span>
                                                            <?php else: ?>
                                                                <span class="badge bg-danger">Inactif</span>
                                                            <?php endif; ?>
                                                        </td>
                                                        <td class="text-end" data-order="<?= (float) $produit['qty_p1'] ?>"><?= number_format($produit['qty_p1'], 0, ',', ' ') ?></td>
                                                        <td class="text-end" data-order="<?= (float) $produit['revenue_p1'] ?>"><?= number_format($produit['revenue_p1'], 0, ',', ' ') ?> €</td>
                                                        <td class="text-end" data-order="<?= (float) $produit['qty_p2'] ?>"><?= number_format($produit['qty_p2'], 0, ',', ' ') ?></td>
                                                        <td class="text-end" data-order="<?= (float) $produit['revenue_p2'] ?>"><?= number_format($produit['revenue_p2'], 0, ',', ' ') ?> €</td>
                                                        <td class="text-end <?= $produit['variation_revenue'] < 0 ? 'text-danger' : 'text-success' ?>" data-order="<?= (float) $produit['variation_revenue'] ?>">
                                                            <?= number_format($produit['variation_revenue'], 0, ',', ' ') ?> €
                                                        </td>
                                                        <td class="text-end <?= ($produit['variation_pct'] !== null && $produit['variation_pct'] < 0) ? 'text-danger' : 'text-success' ?>" data-order="<?= $produit['variation_pct'] !== null ? (float) $produit['variation_pct'] : 0 ?>">
                                                            <?= $produit['variation_pct'] !== null ? number_format($produit['variation_pct'], 1, ',', ' ') . ' %' : '-' ?>
                                                        </td>
                                                    </tr>
                                                <?php endforeach; ?>
                                            <?php endif; ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row mt-4">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">
                                <h4 class="header-title mb-3">Marques en déclin</h4>
                                <div class="table-responsive">
                                    <table id="table-declin-marques" class="table table-striped">
                                        <thead>
                                            <tr>
                                                <th>Marque</th>
                                                <th class="text-end">Qté P1</th>
                                                <th class="text-end">CA P1</th>
                                                <th class="text-end">Qté P2</th>
                                                <th class="text-end">CA P2</th>
                                                <th class="text-end">Var. CA</th>
                                                <th class="text-end">Var. %</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php if (empty($marques)): ?>
                                                <tr>
                                                    <td colspan="7" class="text-center">Aucune marque en déclin sur cette période avec ce seuil.</td>
                                                </tr>
                                            <?php else: ?>
                                                <?php foreach ($marques as $marque): ?>
                                                    <tr>
                                                        <td><?= esc($marque['brand'] ?: '-') ?></td>
                                                        <td class="text-end" data-order="<?= (float) $marque['qty_p1'] ?>"><?= number_format($marque['qty_p1'], 0, ',', ' ') ?></td>
                                                        <td class="text-end" data-order="<?= (float) $marque['revenue_p1'] ?>"><?= number_format($marque['revenue_p1'], 0, ',', ' ') ?> €</td>
                                                        <td class="text-end" data-order="<?= (float) $marque['qty_p2'] ?>"><?= number_format($marque['qty_p2'], 0, ',', ' ') ?></td>
                                                        <td class="text-end" data-order="<?= (float) $marque['revenue_p2'] ?>"><?= number_format($marque['revenue_p2'], 0, ',', ' ') ?> €</td>
                                                        <td class="text-end <?= $marque['variation_revenue'] < 0 ? 'text-danger' : 'text-success' ?>" data-order="<?= (float) $marque['variation_revenue'] ?>">
                                                            <?= number_format($marque['variation_revenue'], 0, ',', ' ') ?> €
                                                        </td>
                                                        <td class="text-end <?= ($marque['variation_pct'] !== null && $marque['variation_pct'] < 0) ? 'text-danger' : 'text-success' ?>" data-order="<?= $marque['variation_pct'] !== null ? (float) $marque['variation_pct'] : 0 ?>">
                                                            <?= $marque['variation_pct'] !== null ? number_format($marque['variation_pct'], 1, ',', ' ') . ' %' : '-' ?>
                                                        </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') ?>

    <script>
        $(document).ready(function() {
            initDataTable('#table-declin-produits', {
                order: [[9, 'asc']],
                pageLength: 25
            });

            initDataTable('#table-declin-marques', {
                order: [[6, 'asc']],
                pageLength: 25
            });
        });
    </script>

</body>
</html>
