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

$campagne_id = $_GET['id'];

// Infos Campagne
$stmt = $pdo->prepare("SELECT c.*, e.nom as cse_nom FROM campagnes c JOIN entreprises e ON c.entreprise_id = e.id WHERE c.id = ?");
$stmt->execute([$campagne_id]);
$camp = $stmt->fetch();

// REQUETE MAGIQUE : Groupe par produit et somme les quantités
$sql = "
    SELECT p.reference, p.nom, SUM(lc.quantite) as total_qty, p.prix_unitaire
    FROM lignes_commande lc
    JOIN commandes c ON lc.commande_id = c.id
    JOIN produits p ON lc.produit_id = p.id
    WHERE c.campagne_id = ? AND c.statut != 'REFUSE'
    GROUP BY p.id
    ORDER BY p.nom ASC
";
$stmtP = $pdo->prepare($sql);
$stmtP->execute([$campagne_id]);
$lignes = $stmtP->fetchAll();

// Calcul total global
$total_campagne = 0;
foreach($lignes as $l) { $total_campagne += ($l['total_qty'] * $l['prix_unitaire']); }
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Master Commande - <?= $camp['nom'] ?></title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 900px; margin: 0 auto; padding: 20px; }
        .header { border-bottom: 3px solid #000; padding-bottom: 20px; margin-bottom: 30px; display: flex; justify-content: space-between; }
        h1 { margin: 0; color: #c5a059; }
        table { width: 100%; border-collapse: collapse; }
        th, td { border: 1px solid #ccc; padding: 10px; text-align: left; }
        th { background: #eee; }
        .total { text-align: right; margin-top: 20px; font-size: 1.5em; font-weight: bold; }
        @media print { .no-print { display:none; } }
    </style>
</head>
<body onload="window.print()">
    <button class="no-print" onclick="window.print()">Imprimer / PDF</button>

    <div class="header">
        <div>
            <h1>COMMANDE GLOBALE</h1>
            <h3>Client : <?= $camp['cse_nom'] ?></h3>
            <p>Campagne : <?= $camp['nom'] ?></p>
        </div>
        <div style="text-align:right">
            Date : <?= date('d/m/Y') ?><br>
            <strong>CUMUL DES VENTES</strong>
        </div>
    </div>

    <table>
        <thead>
            <tr>
                <th>Réf</th>
                <th>Produit</th>
                <th>Quantité Totale</th>
                <th>Valeur (Indicatif)</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach($lignes as $l): ?>
            <tr>
                <td><strong><?= $l['reference'] ?></strong></td>
                <td><?= $l['nom'] ?></td>
                <td style="font-size:1.2em; font-weight:bold;"><?= $l['total_qty'] ?></td>
                <td><?= number_format($l['total_qty'] * $l['prix_unitaire'], 2) ?> €</td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <div class="total">
        CA Total Campagne : <?= number_format($total_campagne, 2) ?> €
    </div>
</body>
</html>