<?php
include 'db.php';

$action = $_POST['action'] ?? '';
$meal_id = intval($_POST['meal_id'] ?? 0);

if($action=='remove'){
    $material_id = intval($_POST['material_id'] ?? 0);
    $conn->query("DELETE FROM meal_parameters WHERE meal_id=$meal_id AND material_id=$material_id");
    exit('Material removed successfully.');
}

if($action=='add'){
    $material_id = intval($_POST['material_id'] ?? 0);
    // qty_per_people and people_count must be pre-defined in system
    // Here you can set a default system value, e.g., 1kg per 4 people
    $default_qty = 1;  // 1 kg
    $default_people = 4;

    $conn->query("
        INSERT INTO meal_parameters (meal_id, material_id, qty_per_people, people_count)
        VALUES ($meal_id, $material_id, $default_qty, $default_people)
    ");
    exit('Material added successfully with system pre-defined quantity.');
}
