<?php
include 'db.php';

$id = intval($_GET['id'] ?? 0);

if(!$id){
    echo json_encode([]);
    exit;
}

$sql = "
    SELECT 
        p.id,
        p.name,
        p.unit,
        p.quantity,
        IFNULL(pp.qty_per_person,0) AS qty_per_person
    FROM perishables p
    LEFT JOIN perishable_parameters pp 
        ON pp.perishable_id = p.id
    WHERE p.id = ?
    LIMIT 1
";

$stmt = $conn->prepare($sql);
$stmt->bind_param("i",$id);
$stmt->execute();
$res = $stmt->get_result()->fetch_assoc();

echo json_encode($res ?: []);