<?php
set_time_limit(300);
require_once('includes/load.php');
$order_sn = $_GET['order'];

// Pisahkan string order_sn yang dipisahkan koma menjadi array
$order_sn_array = explode(',', $order_sn);

// Pastikan setiap item di dalam array didefinisikan dan aman
$order_sn_array = array_map(function ($item) use ($connection) {
    return "'" . $item . "'";
}, $order_sn_array);

// Gabungkan array kembali menjadi string yang dipisahkan koma
$order_sn_safe = implode(',', $order_sn_array);

$sql = "SELECT s.access_token, s.shop_id, s.refresh_token, s.code, o.order_sn 
        FROM shop s 
        JOIN orders o ON o.shop_id = s.shop_id 
        WHERE o.order_sn IN ($order_sn_safe)";
$stmt = $db->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();
// Check if any record was found
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $listDataOrder = [];
        $dataOrder = array();
        $shop_id = $row['shop_id'];
        $access_token = $row['access_token'];
        $refresh_token = $row['refresh_token'];
        $arrayOrder = $row['order_sn'];

        // Ubah string menjadi array
        $orderList = explode(",", $arrayOrder);
        $countOrder = count($orderList);
        $arrayPackingList = array();

        for ($i = 0; $i < $countOrder; $i++) {
            $updateStok = 0;
            $addressId = 0;
            $zipcode = "";
            $response = getDetailOrder(
                $access_token,
                $refresh_token,
                $shop_id,
                $orderList[$i]
            );
            insertOrderDetails($response, $shop_id);
            // $responseShippingParamJson = getShippingParameter($access_token, $refresh_token, $orderList[$i], $shop_id);
            // $responseShippingParam = json_decode($responseShippingParamJson, true);
            // $dataWarehouseDetailJson = getWarehouseDetail($access_token, $refresh_token, $shop_id);
            // $dataWarehouseDetail = json_decode($dataWarehouseDetailJson, true);
            // if (empty($dataWarehouseDetail['error'])) {
            //     $criteria = "location_id";
            //     $order = getDataOrderByOrderSn($orderList[$i]);
            //     $value = $order['product_location_id'] ?? "";
            //     $warehouse = findWarehouse($dataWarehouseDetail['response'], $criteria, $value);
            //     $addressId = $warehouse['address_id'];
            //     $zipcode = $warehouse['zipcode'];
            // } else {
            //     $countdataShippingParameter = count($responseShippingParam['response']['pickup']['address_list']);
            //     if ($countdataShippingParameter === 1) {
            //         $addressId = $responseShippingParam['response']['pickup']['address_list'][0]['address_id'];
            //         $zipcode = $responseShippingParam['response']['pickup']['address_list'][0]['zipcode'];
            //     } else {
            //         echo json_encode(["message" => "Failed to find warehouse"]);
            //         return;
            //     }
            // }

            // $pickupTimeId = findPickupTimeId($addressId, $responseShippingParam['response']['pickup']['address_list']);
            // $shipOrderJson = shipOrder($access_token, $refresh_token, $shop_id, $orderList[$i], $addressId);
            // $updateShippingOrderJson = updateShippingOrder($access_token, $refresh_token, $shop_id, $orderList[$i], $addressId, $pickupTimeId);
            $getTrackingNumber = getTrackingNumber($access_token, $refresh_token, $orderList[$i], $shop_id);
            echo $getTrackingNumber;
            // Decode respons JSON
            $response = json_decode($getTrackingNumber, true); // Menggunakan true untuk mendapatkan array asosiatif
            $resi = $response['response']['tracking_number'];
            // $updateStok = moveOrdersToAwbAndUpdateStok($orderList[$i], $resi, $zipcode);
            $url = downloadAwb($access_token, $refresh_token, $orderList[$i], $shop_id);
            // repositoryOrdersPackingQueue($orderList[$i], $zipcode, $url, $updateStok, $resi);
        }
    }

    echo json_encode(["message" => "Successfully"]);
} else {
    echo json_encode(["message" => "No access token found"]);
}