<?php
/**
* Template Name: MNP CN Tracking
*/
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>MNP 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 = 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 +1;
//					}	
//					//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";
					$consignment =	"545887010000002";

					//API Url
					$url = "http://mnpcourier.com/mycodapi/api/Tracking/Consignment_Tracking";

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

					//The JSON data.
					$jsonData = array(
						'username' => $username,
						'password'	=> $password,
						'consignment'	=> $consignment
					);

					//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
echo				$result = curl_exec($ch);
					$result = (explode(",",$result));

					//	print_r ($result);
					//echo "<br />";	
//					$status = (explode(":",$result[0]));
					//print_r ($status);
					//echo "<br />";
					//$status = (explode(":",$status));	
					//echo "<br />";
//					$status = str_replace('"','',$status[1]);
					//echo "status: ".$status;
					//echo "<hr />";

					//echo "cn: ".$cn;

//					echo "<br />";	
//					if ($status == "false" ){
//
//						$msg = (explode(":",$result[1]));
//						$msg = str_replace('"','',$msg[1]);
//						echo $note = "MNP booking status: ".$msg;}
//					else {
//						$cn = (explode(":",$result[2]));
//						$cn = str_replace('}]','',$cn[1]);
//						$cn = str_replace('"','',$cn);	
//						echo $note = "MNP booking CN#: ".$cn;}
					// Add the note
//					$order->add_order_note( $note );
					// END  - bookingatmnp    
					}
					?>
							<br /><br /><br />
						</div>
					</div>
				</div>				
			</div>
		</div>
	</div>
</div>
<?php get_footer();