<?php
ob_start();
require_once('../../../../wp-config.php');

$order_id = $_POST["order_id"];

//$order_id = 2416;

$order = new WC_Order( $order_id );

//	$order = wc_get_order( $order_id );

$order_items = $order->get_items();
$pieces = 0;
foreach( $order_items as $product ) {
	//$product_details[] = $product['name']."x".$product['qty'];
	//$product_details[] = $product['name'];
	$pieces = $pieces + $product['qty'];
}	
//echo $product_list = implode( ',', $product_details );
$productDetails = $pieces." VEGA.pk Product(s)";

$email = $order->billing_email;
$phone = $order->billing_phone;
$shipping_type = $order->get_shipping_method();
$shipping_cost = $order->get_total_shipping();

// $CodAmount = $order->get_subtotal() + $order->get_total_shipping() ;
$codAmount = $order->get_total();

$firstName = empty($order->get_shipping_first_name()) ? $order->get_billing_first_name() : $order->get_shipping_first_name();
$lastName = empty($order->get_shipping_last_name()) ? $order->get_billing_last_name() : $order->get_shipping_last_name();
$phone = $order->get_billing_phone();
$email = $order->get_billing_email();
$city = empty($order->get_shipping_city()) ? $order->get_billing_city() : $order->get_shipping_city();
// $city = getCityCode($city);

$address_1 = empty($order->get_billing_address_1()) ? $order->get_shipping_address_1() : $order->get_billing_address_1();
$address_2 = empty($order->get_billing_address_2()) ? $order->get_shipping_address_2() : $order->get_billing_address_2();

$vendorId = "5ef0510076984c095100d454";
$apiSecret = "a86dc4f4-4e9d-4896-90cd-54734e7e45fe-5ef0510076984c095100d454";

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.swyftlogistics.com:3000/api/$vendorId/api-upload",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS =>"[\n    {\n      \"ORDER_ID\": \"$order_id\",\n      \"ORDER_TYPE\": \"COD\",\n      \"CONSIGNEE_FIRST_NAME\": \"$firstName\",\n      \"CONSIGNEE_LAST_NAME\": \"$lastName\",\n      \"CONSIGNEE_EMAIL\": \"$email\",\n      \"CONSIGNEE_PHONE\": \"$phone\",\n      \"CONSIGNEE_CITY\": \"$city\",\n      \"CONSIGNEE_ADDRESS\": \"$address_1  $address_2\",\n      \"PACKAGING\": \"Flyer\",\n      \"ORIGIN_CITY\": \"LHE\",\n      \"PIECES\": 1,\n      \"COD\": $codAmount,\n      \"DESCRIPTION\": \"$productDetails\",\n      \"WEIGHT\": 0,\n      \"SHIPPER_ADDRESS_ID\": \"PL-356263\"\n    }\n  ]\n  ",
  CURLOPT_HTTPHEADER => array(
	"Content-Type: application/json",
	"Authorization: $apiSecret"
  ),
));

$result = curl_exec($curl);

$slcnres = get_post_meta( $order_id , '_slcnres', true );
if(empty($slcnres))
	add_post_meta($order_id, '_slcnres', $result);
else
	update_post_meta($order_id, '_slcnres', $result);
	
$result1 = $result;

curl_close($curl);

$result = (explode(",",$result));
print_r ($result);
$cn = (explode(":",$result[6]));
$cn = str_replace('"','',$cn[1]);


ob_end_clean();
if ($cn == ""){
	$msg = (explode(":",$result[0]));
	$msg = str_replace('"','',$msg[2]);
	$note = "Swyft Logistics booking status: ".$msg;
	// Add the note
	$order->add_order_note( $note );
	echo $msg;
}
else {
    $result1 = (explode(",",$result1));
    //print_r ($result);
    $cn = (explode(":",$result1[5]));
    $cn = str_replace('"','',$cn[1]);
    
	$note = "SL booking CN#: ".$cn;
	// add order private note
	$order->add_order_note( $note );
	// add slcn to post meta
	$slcn = get_post_meta( $order_id , '_slcn', true );
	if(empty($slcn))
	    add_post_meta($order_id, '_slcn', $cn);
	else
	    update_post_meta($order_id, '_slcn', $cn);
	echo "true";
	echo ",".$cn;
}

function getCityCode($cityname){

    $vendorId = "5ef0510076984c095100d454";
    $apiSecret = "a86dc4f4-4e9d-4896-90cd-54734e7e45fe-5ef0510076984c095100d454";
    
    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://api.swyftlogistics.com:3000/api/$vendorId/get-cities",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
      CURLOPT_HTTPHEADER => array(
    	"Content-Type: application/json",
    	"Authorization: $apiSecret"
      ),
    ));
    
    $result = curl_exec($curl);
    curl_close($curl);
    
    $result = str_replace('[','',$result);
    $result = str_replace(']','',$result);
    $result = str_replace('{','',$result);
    $result = str_replace('}','',$result);
    $result = str_replace('"','',$result);
    $result = str_replace('name:','',$result);
    $result = str_replace('code:','',$result);
        
    $result = (explode(",",$result));

    
    $cityname = strtolower($cityname);
    $cityname =  ucfirst($cityname);
    
    $max = count($result);
    for ($x = 0; $x < $max ; $x++) {
        if($result[$x]==$cityname){
            return $result[$x+1];
            break;
        }
    }
    return null;
}
?>