<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
set_time_limit(5);
require_once('includes/load.php');
$platform = 1;
$orderSn = $_GET['order'];
$shop = $_GET['shop'];
$sql = "SELECT access_token, shop_id, refresh_token, code FROM shop where platform_id = $platform and shop_id = $shop";
$stmt = $db->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();

// // Check if any record was found
if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $response = getDetailOrder($row['access_token'], $row['refresh_token'], $row['shop_id'], $orderSn);
    insertOrderDetails($response, $row['shop_id']);
    echo json_encode($response);
    $responseTrac = getTrackingNumber($row['access_token'], $row['refresh_token'], $orderSn, $row['shop_id']);
    echo "</br>";
    $responseShippingParamJson = getShippingParameter($row['access_token'], $row['refresh_token'], $orderSn, $row['shop_id']);

    echo $responseTrac;
    echo "</br>";

    echo $responseShippingParamJson;
    //     $query = "INSERT INTO queue_shipment (order_sn, shop_id, access_token, refresh_token, log_json, platform_id, flag_delete)
    // SELECT 
    //     '$orderSn' AS order_sn,
    //     shop.shop_id,
    //     shop.access_token,
    //     shop.refresh_token,
    //     '{}' AS log_json,
    //     shop.platform_id,
    //     0 AS flag_delete
    // FROM 
    //     shop
    // WHERE 
    //     shop.shop_id = " . $row['shop_id'];
    //     $db->query($query);
} else {
    echo json_encode(["message" => "No access token found"]);
}