<?php
/**
* Template Name: CallCourier CN#
*/
get_header();
?>
<div id="Content">
	<div class="content_wrapper clearfix">
		<div class="sections_group">
			<div class="entry-content" itemprop="mainContentOfPage">
				<div class="section the_content has_content">
					<div class="section_wrapper">
						<div class="the_content_wrapper">
					<?php
					if( current_user_can('administrator') ) {

					while ( have_posts() ) {
					?><h2>CallCourier Booking</h2>
					<?php	
						the_post();
						$mfn_builder = new Mfn_Builder_Front( get_the_ID() );
						$mfn_builder->show();	
					}
					// $order_id = 2806;
					$order_id = $_GET["cn"];						
					echo "<h3>Order Number: $order_id </h3>";
					echo "<hr />";


					$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 +1;
					}	
					//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";
					$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";


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

					echo $result = file_get_contents($url);
					echo "<hr >";						
					$result = (explode(",",$result));
					$status = explode(":",$result[5]);					
					$status = str_replace('"','',$status[1]);
					echo "<strong>Status:</strong> ".$status;


					echo "<hr >";
						
					if ($status == "true" ){
						$cn = (explode(":",$result[3]));
						$cn = str_replace('"','',$cn[1]);	
						$note = "CallCourier booking CN#: ".$cn;						
						}
					else {
						$status = explode(":",$result[5]);					
						$status = str_replace('"','',$status[1]);											
						$note = "CallCourier booking status: ".$status;}
					echo "<strong>Order note:</strong> ".$note;	
					// Add the note
				$order->add_order_note( $note );
					// END  - bookingatmnp    
					}					
					?>
							<br /><br /><br />
						</div>
					</div>
				</div>				
			</div>
		</div>
	</div>
</div>
<?php get_footer();