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

$product_id = (int)($_GET['product_id'] ?? 0);

$sql = "
    SELECT 
        id AS purchase_item_id,
        batch_no,
        available_qty,
        mrp,
        selling_price,
        gst_percent
    FROM purchase_items
    WHERE product_id = $product_id
      AND available_qty > 0
    ORDER BY created_at ASC
";

$res = $conn->query($sql);
$data = [];

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

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