<?php
include 'db.php';
$item_id = $_GET['item_id'] ?? 0;

$sql = "SELECT 
    pi.unit_cost AS price,
    v.name AS vendor,
    DATE(p.purchase_date) AS date
  FROM purchase_items pi
  JOIN purchases p ON pi.purchase_id = p.id
  JOIN vendors v ON p.vendor_id = v.id
  WHERE pi.item_id = ?
  ORDER BY p.purchase_date DESC
  LIMIT 1";

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

echo json_encode($data ?: []);
?>
