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

// $order_id = 2620;
$order_id = $_POST["order_id"];

//	echo "order_id ".$order_id;

//	echo "<h3>Order Number: $order_id </h3>";
//	echo "<hr />";

$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();

$ConsigneeName ="";
if ( (empty($order->get_shipping_first_name()))  && (empty($order->get_shipping_last_name())) ){
	$ConsigneeName = $order->get_billing_first_name().' '.$order->get_billing_last_name();}
else{
	$ConsigneeName = $order->get_shipping_first_name().' '.$order->get_shipping_last_name();
}

$address = "";
if ( (empty($order->get_shipping_address_1()))  && (empty($order->get_shipping_address_2())) ){
	$address = $order->get_billing_address_1().' '.$order->get_billing_address_2();}
else{
	$address = $order->get_shipping_address_1().' '.$order->get_shipping_address_2();
}

$destinationCityName = "";
if ( empty($order->get_shipping_city())  ){
	$destinationCityName = $order->get_billing_city();}
else{
	$destinationCityName = $order->get_shipping_city();
}

//$DestCityId = searchForDestCityId1( $DestCityName );
$consigneeEmail = $order->get_billing_email();

$destinationCityName = ucfirst(strtolower($destinationCityName));
$username = "amer_1t181";
$password = "@thvQiiK7@QTzzK";

//API Url
$url = "https://mnpcourier.com/mycodapi/api/Booking/InsertBookingData";

//Initiate cURL.
$ch = curl_init($url);

//The JSON data.
$jsonData = array(
	'username' => $username,
	'password'	=> $password,
	'consigneeName'	=> $ConsigneeName,
	'consigneeAddress'	=> $address,
	'consigneeMobNo'	=> $phone,
	'consigneeEmail'	=> $consigneeEmail,
	'destinationCityName'	=> $destinationCityName,
	'pieces'	=> $pieces,
	'weight'	=> 0.5,
	'codAmount'	=> $codAmount,
	'custRefNo'	=> $order_id,
	'productDetails'	=> $productDetails,
	'fragile'	=> 'Yes',
	'Service'	=> 'O',
	'Remarks'	=> 'Please call before delivery',
	'InsuranceValue'	=> '0'
);

//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);

//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 

//this tells curl to return the page in the form of a variable, which is returned when you run the statement curl_exec. 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

// Actually if you dont want to store it in a string, just not show it then:
//curl_setopt($ch, CURLOPT_NOBODY,1);

//Execute the request
$result = curl_exec($ch);

$mnpcnres = get_post_meta( $order_id , '_mnpcnres', true );
if(empty($mnpcnres))
	add_post_meta($order_id, '_mnpcnres', $result);
else
	update_post_meta($order_id, '_mnpcnres', $result);

$result = (explode(",",$result));	
$status = (explode(":",$result[0]));
$status = str_replace('"','',$status[1]);

ob_end_clean();
if ($status == "false" ){
	$msg = (explode(":",$result[1]));
	$msg = str_replace('"','',$msg[1]);
	$note = "MNP booking status: ".$msg;
	// Add the note
	$order->add_order_note( $note );
	echo $msg;

}
else {
	$cn = (explode(":",$result[2]));
	$cn = str_replace('}]','',$cn[1]);
	$cn = str_replace('"','',$cn);	
	$note = "MNP booking CN#: ".$cn;
	// add order private note
	$order->add_order_note( $note );
	// add mnp cn to post meta
	$mnpcn = get_post_meta( $order_id , '_mnpcn', true );
	if(empty($mnpcn))
		add_post_meta($order_id, '_mnpcn', $cn);
	else
	    update_post_meta($order_id, '_mnpcn', $cn);
	echo "true";
	echo ",".$cn;
}
?>