<?php
/**
* Template Name: MNP
*/
get_header();
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>

<div id = 'Content'>
<div class = 'content_wrapper clearfix'>
<div class = 'sections_group'>
<div class = 'entry-content' itemprop = 'mainContentOfPage'>

	
<?php
while ( have_posts() ) {

    the_post();

    $mfn_builder = new Mfn_Builder_Front( get_the_ID() );
    $mfn_builder->show();

}

$order_id = 2806;

$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";
	
//API Url
$url = "http://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'	=> 1213,
	'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);
$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]);
	$note = "MNP booking status: ".$msg;}
else {
	$cn = (explode(":",$result[2]));
	$cn = str_replace('}]','',$cn[1]);
	$cn = str_replace('"','',$cn);	
	$note = "MNP booking CN#: ".$cn;}
	
// echo $note;

	
// print_r ($result);
//	echo $result;

	//	echo "Result: ".$result;

// echo $CodAmount.'<br />';
	
	
$posting_url = "http://mnpcourier.com/mycodapi/api/Booking/InsertBookingData";
$posting_url = "";
	
	$url =  add_query_arg( array(
	'username' => 'amer_1t181',
	'password'	=> '@thvQiiK7@QTzzK',
	'consigneeName'	=> $ConsigneeName,
	'consigneeAddress'	=> $address,
	'consigneeMobNo'	=> $phone,
	'consigneeEmail'	=> $consigneeEmail,
	'destinationCityName'	=> $destinationCityName,
	'pieces'	=> 1,
	'weight'	=> 1.0,
	'codAmount'	=> $codAmount,
	'custRefNo'	=> $order_id,
	'productDetails'	=> 'one White t shirt medium',
	'fragile'	=> 'Yes',
	'Service'	=> 'S',
	'Remarks'	=> 'Please call before delivery',
	'InsuranceValue'	=> '0',
	),
	$posting_url
	); 
	
	// echo $url;
	?>

<div id="serverResponse"></div>
<script type="text/javascript"> 
	const xhr = new XMLHttpRequest ();
	data = "username=<?php echo $username; ?>";
	data = data + "&password=<?php echo $password; ?>";
	data = data + "&consigneeName=<?php echo $ConsigneeName; ?>";
	data = data + "&consigneeAddress=<?php echo $address; ?>";
	data = data + "&consigneeMobNo=<?php echo $phone; ?>";
	data = data + "&consigneeEmail=<?php echo $consigneeEmail; ?>";
	data = data + "&destinationCityName=<?php echo $destinationCityName; ?>";
	data = data + "&pieces=1";
	data = data + "&weight=1.0";
	data = data + "&codAmount=<?php echo $codAmount; ?>";
	data = data + "&custRefNo=<?php echo $order_id; ?>";
	data = data + "&productDetails=one White t shirt medium";
	data = data + "&fragile=Yes";
	data = data + "&Service=S";
	data = data + "&Remarks=Please call before delivery";
	data = data + "&InsuranceValue=0";
	
	
	xhr.onload = function () {
	const serverResponse = document.getElementById("serverResponse");
	serverResponse.innerHTML = this.responseText;
	}
	
	// xhr.open("POST", "http://localhost/vega/wp-content/themes/child/dom.php"); 
	xhr.open("POST", "http://mnpcourier.com/mycodapi/api/Booking/InsertBookingData"); 
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	// xhr.send(data);
</script>
	
<?php
function searchForDestCityId1( $DestCityName ) {
    $DestCityName = strtoupper( $DestCityName );
    $url = 'http://cod.callcourier.com.pk/API/CallCourier/GetCityListByService?serviceID=7';
    $url =  str_replace( ' ', '%20', $url );
    $response = file_get_contents( $url );
    $response = json_decode( $response );
    $DestCityId = 0;
    for ( $i = 0; $i < count( $response );
    $i++ ) {
        foreach ( $response[$i] as $key => $value ) {
            // echo $key.' : '.$value;
            if ( $key == 'CityID' ) {
                $DestCityId = $value;
            }
            if ( $key == 'CityName' ) {
                $DestCityName1 = $value;
            }
            //	echo '<br />';
        }
        if ( $DestCityName1 === $DestCityName ) {
            return $DestCityId;
        }
    }
    return null;
}
?>

</div>

</div>
</div>
</div>
<script>
function mnpbooking(id){
	alert(1231);
	$.post("http://mnpcourier.com/mycodapi/api/Booking/InsertBookingData",
	// $.post("<?php echo get_stylesheet_directory_uri();?>/mnp-post.php",
	{
		username: "<?php echo $username; ?>",
		password: "<?php echo $password; ?>",
		consigneeName: "<?php echo $ConsigneeName; ?>",
		consigneeAddress: "<?php echo $address; ?>",
		consigneeMobNo: "<?php echo $phone; ?>",
		consigneeEmail: "<?php echo $consigneeEmail; ?>",
		destinationCityName: "<?php echo $destinationCityName; ?>",
		pieces: "1",
		weight: "1.0",
		codAmount: "<?php echo $codAmount; ?>",
		custRefNo: "<?php echo $order_id; ?>",
		productDetails: "one White t shirt medium",
		fragile: "Yes",
		Service: "S",
		Remarks: "Please call before delivery",
		InsuranceValue:	=> "0",
	},		
	function ( data, status ) {
		// document.getElementById("mnpresult").innerHTML = data;
		alert (data);
//		alert(status);
//		alert(status);
	});
}
</script>
