<?php
include 'db.php';
header('Content-Type: application/json');

$product_id = intval($_GET['product_id']);

// Fetch restocks
$restocks = [];
$r = $conn->query("SELECT DATE(created_at) as date, quantity FROM product_stock_log WHERE product_id=$product_id AND type='Restock' ORDER BY created_at DESC");
while($row = $r->fetch_assoc()) $restocks[] = $row;

// Fetch dispatches
$dispatches = [];
$d = $conn->query("SELECT DATE(dispatch_date) as date, quantity, dispatched_by FROM stock_dispatch_requests WHERE product_id=$product_id AND status='Approved' ORDER BY dispatch_date DESC");
while($row = $d->fetch_assoc()) $dispatches[] = $row;

echo json_encode([
    'restocks' => $restocks,
    'dispatches' => $dispatches
]);
?>