<?php
// Required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

// Include database and object files
include_once '../config/database.php'; // Include the database connection
include_once '../objects/item.php'; // Include the item object

// Instantiate database
$database = new Database();
$db = $database->getConnection();

// Initialize item object
$item = new Item($db);

// Get the incoming data
$data = json_decode(file_get_contents("php://input"));

// Check if ID is set
if (isset($data->id)) {
    // Set the item ID to fetch
    $item->id = $data->id;

    // Call the method to get item details
    $item->getOneRow();

    // Prepare the item data array
    $item_data = array(
        "id" => $item->id,
        "category_code" => $item->category_code,
        "category" => $item->category, // Ensure these properties exist in your item class
        "type_code" => $item->type_code,
        "type" => $item->type,
        "item_code" => $item->item_code,
        "item_name" => $item->item_name,
        "barcode" => $item->barcode,
        "capital_price" => $item->capital_price,
        "sales_price" => $item->sales_price,
        "supplier" => $item->supplier
        // Add any additional fields you have for items
    );

    // Return the item data as JSON
    echo json_encode($item_data);
} else {
    // Return error if ID is not provided
    echo json_encode(array("message" => "item ID is missing."));
}
?>
