<?php
require_once "../../config/db.php";

$id = (int)$_GET['id'];

$cmd = $conn->query("
    SELECT c.*, cl.nom as client
    FROM commandes_clients c
    JOIN clients cl ON cl.idClient = c.client_id
    WHERE c.id=$id
")->fetch(PDO::FETCH_ASSOC);

$details = $conn->query("
    SELECT 
        d.id,
        p.nom as produit,
        d.prix,
        d.quantite,
        d.quantite_livree,
        d.quantite_facturee,
        (d.quantite_livree - d.quantite_facturee) as reste

    FROM commandes_clients_details d
    JOIN produits p ON p.id = d.produit_id

    WHERE d.commande_id = $id
    HAVING reste > 0
")->fetchAll(PDO::FETCH_ASSOC);

echo json_encode([
    'commande' => $cmd,
    'details' => $details
]);