<?php
require_once("../config/config.php");
header('Content-Type: application/json');

$rc_id = (int)($_GET['rc_id'] ?? 0);
if ($rc_id <= 0) {
    echo json_encode(['success' => false, 'message' => 'Invalid RC']);
    exit;
}

$sql = "
    SELECT rci.item_id, im.name, im.model_no, rci.returned_qty, rci.rate, rci.uom, rci.serial_no
    FROM return_challan_items rci
    JOIN inventory_master im ON rci.item_id = im.id
    WHERE rci.rc_id = ?
";

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

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

echo json_encode(['success' => true, 'items' => $items]);
?>