<?php
ob_start();
/**
* Template Name: CallCourier DO
*/

//$order_id = $_GET["cn"];
$order_id = $_POST["order_id"];
//$order_id = 5682;

$order = new WC_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 );
$Description = $pieces." VEGA.pk Product(s)";

$email = $order->billing_email;
$phone = $order->billing_phone;
$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();
}
												
$DestCityName = "";
if ( empty($order->get_shipping_city())  ){
	$DestCityName = $order->get_billing_city();}
else{
	$DestCityName = $order->get_shipping_city();
}
// $DestCityName = "LAHORE1";
$DestCityName = strtoupper($DestCityName);
$DestCityId = searchForDestCityId($DestCityName);

// $url = "http://cod.callcourier.com.pk/api/CallCourier/SaveBooking?loginId=test-0001"; // test account
//	$url = "http://cod.callcourier.com.pk/api/CallCourier/SaveBooking?loginId=LHR-05157"; // pehnava.online
	$url = "http://cod.callcourier.com.pk/api/CallCourier/SaveBooking?loginId=LHR-05662"; // VEGA.pk
	
$url .= "&ConsigneeName=".$ConsigneeName;
$url .= "&ConsigneeRefNo=".$order_id;
$url .= "&ConsigneeCellNo=".$phone;
$url .= "&Address=".$address;
$url .= "&Origin=Lahore";
$url .= "&DestCityId=".$DestCityId;
$url .= "&ServiceTypeId=7";
$url .= "&Pcs=".$pieces;
$url .= "&Weight=.5";
$url .= "&Description=".$Description;
$url .= "&SelOrigin=Domestic";
$url .= "&CodAmount=".$CodAmount;
$url .= "&SpecialHandling=false";
$url .= "&MyBoxId=1";
$url .= "&Holiday=False";
$url .= "&remarks=Please call before delivery";	
$url .= "&ShipperName=VEGA.pk";
$url .= "&ShipperCellNo=03368480123";
$url .= "&ShipperArea=280";
$url .= "&ShipperCity=1";
$url .= "&ShipperAddress=LAHORE";
$url .= "&ShipperLandLineNo=03368480123";
$url .= "&ShipperEmail=sales@vega.pk";

echo $url =  str_replace(' ', '%20', $url);
// echo "<hr >";

echo $result = file_get_contents($url);

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

$result1 = $result;
echo "<hr >";						
$result = (explode(",",$result));
$status = explode(":",$result[5]);					
$status = str_replace('"','',$status[1]);
//echo "<strong>Status:</strong> ".$status;
ob_end_clean();
if ($status == "true" ){
    $cn = (explode(":",$result[3]));
    $cn = str_replace('"','',$cn[1]);	
    $note = "CallCourier booking CN#: ".$cn;
    $cccn = get_post_meta( $order_id , '_cccn', true );
    if(empty($cccn))
    	add_post_meta($order_id, '_cccn', $cn);
    else
        update_post_meta($order_id, '_cccn', $cn);
    $order->add_order_note( $note );
    echo "true";
    echo ",".$cn;
    }
else {
    $status = explode('"',$result1);
    $note = "CallCourier booking status: ".$status[19];
    $order->add_order_note( $note );
    echo $note;
}
?>