<?php
require 'includes/db.php';
require 'includes/auth.php';
require_login();

$id = $_GET['id'];
// Sécurité : Vérifie que la commande appartient au CSE
$sql = "SELECT c.*, b.nom, b.prenom, e.nom as cse_nom FROM commandes c JOIN beneficiaires b ON c.beneficiaire_id = b.id JOIN entreprises e ON b.entreprise_id = e.id WHERE c.id = ? AND b.entreprise_id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$id, $_SESSION['entreprise_id']]);
$cmd = $stmt->fetch();

if(!$cmd) die("Commande introuvable ou accès refusé.");

$lignes = $pdo->prepare("SELECT * FROM lignes_commande WHERE commande_id = ?");
$lignes->execute([$id]);
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bon de Commande <?= $cmd['reference'] ?></title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
        .header { display: flex; justify-content: space-between; border-bottom: 2px solid #c5a059; padding-bottom: 20px; margin-bottom: 20px; }
        .logo { font-size: 24px; font-weight: bold; color: #c5a059; }
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        th, td { border: 1px solid #ddd; padding: 10px; text-align: left; }
        th { background: #eee; }
        .total { text-align: right; margin-top: 20px; font-size: 18px; font-weight: bold; }
        @media print { .no-print { display: none; } }
    </style>
</head>
<body onload="window.print()">
    <button class="no-print" onclick="window.print()" style="padding:10px; cursor:pointer;">IMPRIMER / SAUVEGARDER EN PDF</button>
    <button class="no-print" onclick="window.history.back()" style="padding:10px;">Retour</button>

    <div class="header">
        <div class="logo">NAÏTA NATURE</div>
        <div>
            <strong>Bon de Commande</strong><br>
            Réf: <?= $cmd['reference'] ?><br>
            Date: <?= date('d/m/Y', strtotime($cmd['date_creation'])) ?>
        </div>
    </div>

    <p>
        <strong>Client :</strong> <?= $cmd['cse_nom'] ?><br>
        <strong>Bénéficiaire :</strong> <?= $cmd['nom'] ?> <?= $cmd['prenom'] ?>
    </p>

    <table>
        <thead><tr><th>Produit</th><th>Qté</th><th>Prix Unit.</th><th>Total</th></tr></thead>
        <tbody>
            <?php foreach($lignes as $l): ?>
            <tr>
                <td><?= $l['nom_produit_historique'] ?></td>
                <td><?= $l['quantite'] ?></td>
                <td><?= $l['prix_unitaire_facture'] ?> €</td>
                <td><?= number_format($l['quantite']*$l['prix_unitaire_facture'], 2) ?> €</td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <div class="total">
        Total TTC : <?= number_format($cmd['total_ttc'], 2) ?> €<br>
        <?php if($cmd['montant_cagnotte_utilise'] > 0): ?>
            <span style="font-size:0.8em; color:green;">Dont Cagnotte : -<?= $cmd['montant_cagnotte_utilise'] ?> €</span><br>
            <strong>Net à Payer : <?= number_format($cmd['total_ttc'] - $cmd['montant_cagnotte_utilise'], 2) ?> €</strong>
        <?php endif; ?>
    </div>
</body>
</html>