<?php
// get_portions.php - Get meal portions for modal
session_start();
require_once 'config.php';

header('Content-Type: application/json');

$meal_id = isset($_GET['meal_id']) ? (int)$_GET['meal_id'] : 0;

if ($meal_id <= 0) {
    echo json_encode([]);
    exit;
}

$query = "SELECT id, name, price FROM meal_portions WHERE meal_id = ? AND is_active = 1 ORDER BY price";
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, 'i', $meal_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

$portions = [];
while ($row = mysqli_fetch_assoc($result)) {
    $portions[] = [
        'id' => $row['id'],
        'name' => $row['name'],
        'price' => (float)$row['price']
    ];
}

echo json_encode($portions);
?>