<?php

header("Content-Type: application/json");

require_once("../config/database.php");

$conn = getDB();

$order_number =
$_GET['order_number'] ?? '';

if(empty($order_number)){

    echo json_encode([

        "success" => false,

        "message" => "Order number missing"
    ]);

    exit;
}

$order_number =
mysqli_real_escape_string(
    $conn,
    $order_number
);

$orderQuery =
$conn->query("

SELECT * FROM orders

WHERE order_number='$order_number'

LIMIT 1
");

if($orderQuery->num_rows == 0){

    echo json_encode([

        "success" => false,

        "message" => "Order not found"
    ]);

    exit;
}

$order =
$orderQuery->fetch_assoc();

$orderId =
$order['id'];

$itemsQuery =
$conn->query("

SELECT * FROM order_items

WHERE order_id='$orderId'

");

$items = [];

while($row =
$itemsQuery->fetch_assoc()){

    $row['product_image'] =

    "https://globinvest.online/api/uploads/products/" .

    $row['product_image'];

    $items[] = $row;
}

echo json_encode([

    "success" => true,

    "order" => $order,

    "items" => $items
]);