<?php
include '../db.php';

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

$query = "
    SELECT 
        bc.component_item_id,
        bc.quantity,
        i.name as material_name,
        i.cost_price as unit_cost
    FROM bom_master bm
    JOIN bom_components bc ON bm.id = bc.bom_id
    JOIN items i ON bc.component_item_id = i.id
    WHERE bm.item_id = ?
    ORDER BY i.name
";

$stmt = $conn->prepare($query);
$stmt->bind_param("i", $item_id);
$stmt->execute();
$result = $stmt->get_result();

$data = [];
while ($row = $result->fetch_assoc()) {
    $data[] = $row;
}

$stmt->close();

header('Content-Type: application/json');
echo json_encode($data);
?>
