<?php
// Fichier: admin_stats_products.php
require 'includes/db.php';
require 'includes/auth.php';
require_login();
if ($_SESSION['user_role'] !== 'SUPER_ADMIN') die("Accès interdit.");

// Définition des périodes
$periods = [
    '1_MONTH' => "date_creation > date('now', '-1 month')",
    '3_MONTHS' => "date_creation > date('now', '-3 months')",
    '6_MONTHS' => "date_creation > date('now', '-6 months')",
    '1_YEAR' => "date_creation > date('now', '-1 year')",
];

$current_period = isset($_GET['p']) && array_key_exists($_GET['p'], $periods) ? $_GET['p'] : '1_MONTH';
$where_clause = $periods[$current_period];

// SQL : Top Produits sur la période
$sql = "
    SELECT p.reference, p.nom, SUM(lc.quantite) as qty, SUM(lc.quantite * lc.prix_unitaire_facture) as ca
    FROM lignes_commande lc
    JOIN commandes c ON lc.commande_id = c.id
    JOIN produits p ON lc.produit_id = p.id
    WHERE c.statut != 'REFUSE' AND c.$where_clause
    GROUP BY p.id
    ORDER BY qty DESC
";
$stats = $pdo->query($sql)->fetchAll();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Stats Produits</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="assets/style.css">
</head>
<body>
    <nav class="navbar navbar-dark bg-danger px-4 mb-4">
        <span class="navbar-brand">Statistiques Produits</span>
        <a href="dashboard.php" class="btn btn-outline-light btn-sm">Retour</a>
    </nav>
    <div class="container">
        
        <div class="btn-group mb-4">
            <a href="?p=1_MONTH" class="btn btn-<?= $current_period=='1_MONTH'?'dark':'outline-dark' ?>">1 Mois</a>
            <a href="?p=3_MONTHS" class="btn btn-<?= $current_period=='3_MONTHS'?'dark':'outline-dark' ?>">3 Mois</a>
            <a href="?p=6_MONTHS" class="btn btn-<?= $current_period=='6_MONTHS'?'dark':'outline-dark' ?>">6 Mois</a>
            <a href="?p=1_YEAR" class="btn btn-<?= $current_period=='1_YEAR'?'dark':'outline-dark' ?>">1 An</a>
        </div>

        <div class="card shadow-sm">
            <table class="table table-striped mb-0">
                <thead class="table-dark">
                    <tr>
                        <th>Rang</th>
                        <th>Produit</th>
                        <th>Quantité Vendue</th>
                        <th>CA Généré</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($stats as $k => $s): ?>
                    <tr>
                        <td><strong>#<?= $k+1 ?></strong></td>
                        <td><?= $s['nom'] ?> <small class="text-muted">(<?= $s['reference'] ?>)</small></td>
                        <td class="fs-5 fw-bold"><?= $s['qty'] ?></td>
                        <td class="text-success"><?= number_format($s['ca'], 2) ?> €</td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</body>
</html>