<?php
session_start();
header('Content-Type: application/json');
include_once('../../includes/config.php');

// Check if user is logged in
if (!isset($_SESSION['uid'])) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized access']);
    exit();
}

$userId = $_SESSION['uid'];

// Get hold bill ID
$holdBillId = isset($_GET['id']) ? intval($_GET['id']) : 0;

if ($holdBillId === 0) {
    echo json_encode(['success' => false, 'message' => 'Hold bill ID is required']);
    exit();
}

// Get hold bill details with items
$query = "SELECT * FROM tblhold_bills WHERE ID = $holdBillId";
$result = mysqli_query($con, $query);

if (!$result || mysqli_num_rows($result) === 0) {
    echo json_encode(['success' => false, 'message' => 'Hold bill not found']);
    exit();
}

$holdBill = mysqli_fetch_assoc($result);

// Decode items JSON
$holdBill['Items'] = json_decode($holdBill['ItemsJSON'], true);

// Remove the JSON string from response (we send Items array instead)
unset($holdBill['ItemsJSON']);

echo json_encode([
    'success' => true,
    'data' => $holdBill
]);

mysqli_close($con);
?>
