<?php

define("servername" ,"localhost");
define("username" , "upstreamlogistic_pos_spotwash");
define("password" , ',y@9-d$twcIi');
define("dbname" , "upstreamlogistic_pos_spotwash");




// Create connection
$conn = new mysqli(servername, username, password, dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " );
} 

//include_once("../mySQL.php");

echo "working....<br />";



	function sendNotification($conn, $id, $toPhone, $msg, $sms_type){
	    
        if($sms_type == "2"){
    	    //$msg = $msg;
    	}else{
    	  $msg = substr($msg, 0, 130)."...";
    	}
    
    
	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL,"http://skylinesms.com/api/v2/json/messages");
	curl_setopt($ch, CURLOPT_POST, 1);
	//curl_setopt($ch, CURLOPT_POSTFIELDS,
	  //          "postvar1=value1&postvar2=value2&postvar3=value3");

	// In real life you should use something like:
	
	
	
	 curl_setopt($ch, CURLOPT_POSTFIELDS, 
			 http_build_query(array(
			 'token' => '3691da3aa1615bbd54f59e63a2a045e8',
			 'to' => $toPhone,
			 'from' => 'Spotwash',
			 'message' => $msg
			 )));

	// Receive server response ...
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	$server_output = curl_exec($ch);

	curl_close ($ch);
//{"status":"Success","reference":"agie5lj8pyqffgof64liw5b4hn1mos5"}
$data = json_decode($server_output, true);
// Further processing ...
	
	
	if ($data['status'] == "Success") { 
		//...
			$status = $data['status'];
			$reference = $data['reference'];
			echo "Success <br />";

		 $sqlData = "UPDATE `sms_notification` SET `sms_to` = '$toPhone', `ext_status` = '$status', `ext_ref` = '$reference' WHERE `sms_notification`.`sms_id` = $id;";
			$resultData = $conn->query($sqlData);
				
		} else {
		// ... 
		 $sqlData = "UPDATE `sms_notification` SET `sms_to` = '$toPhone', `ext_status` = '$status', `ext_ref` = '$reference' WHERE `sms_notification`.`sms_id` = $id;";
			$resultData = $conn->query($sqlData);
			echo "Failed <br />";
		 }
		 
	}


	 $sqlData = "SELECT customer.customer_fname, `sms_id`, `sms_type`, sms_other_msg, `sms_laundry_code`, `laundry_invoice_number`, `sms_client_code`, LPAD(substr(`sms_to`,2), 12, '256') AS sms_to, `sms_job_price`, `sms_receipt_total`, `sms_basket_items`, `sms_status`, `ext_status`, `sms_mart_code`, `sms_created_by`, `sms_created_on`, `ext_ref`, `sms_date`, `sms_time`, `sms_timestamp`  FROM `sms_notification`,customer WHERE sms_notification.sms_client_code = customer.customer_code AND sms_notification.ext_status = 'pending' ORDER BY `sms_notification`.`sms_id` ASC LIMIT 10 ";

//	echo $sqlData = 	"SELECT customer.customer_fname, sms_notification.* FROM `sms_notification`,customer WHERE sms_notification.sms_client_code = customer.customer_code  ORDER BY `sms_notification`.`sms_id` ASC LIMIT 50 ";
		$resultData = $conn->query($sqlData);
		$json = array();
		

		if ($resultData->num_rows > 0) {
			// output data of each row
			while($row = $resultData->fetch_assoc()) {
				//$output[]=$row;
		//$array[] =$row;
		    $sms_type = $row['sms_type'];
			$id = $row['sms_id'];
			$toPhone =  $row['sms_to'];
			$phone = preg_replace('/\D+/', '', $toPhone);
					    //echo $msg = "Spotwash: Hi ".$row['customer_fname'] .", #". $row['laundry_invoice_number'] ." is ready for pickup. Cost ".$row['sms_job_price'] .", Paid ".$row['sms_receipt_total'] .". Items ".$row['sms_basket_items'] .". Its Always a pleasure serving you";

		         $msg = "Spotwash: Hi ".$row['customer_fname'] .", #". $row['laundry_invoice_number'] ." is ready for pickup. Cost UGX ". number_format($row['sms_job_price'],2) .", Paid UGX ". number_format($row['sms_receipt_total'], 2) .". Its always a pleasure serving you. Thank you.";
		    
		    if($row['sms_type'] == "1"){
		        
		         $msg = "Spotwash: Hi ".$row['customer_fname'] .", #". $row['laundry_invoice_number'] .", Cost UGX ". number_format($row['sms_job_price'],2) .", Paid UGX ". number_format($row['sms_receipt_total'], 2) ." has delayed in our storage facility, Please pick it up. Thank you.";
		    }else if($row['sms_type'] == "2"){
		        
		         $msg = "Spotwash Report:".$row['sms_other_msg'];
		         
		        
		    }else if($row['sms_type'] == "21"){
		        
		         $msg = $row['sms_other_msg'];
		         
		        
		    }else if($row['sms_type'] == "22"){
		        
		         $msg = $row['sms_other_msg'];
		         
		        
		    }
		    echo $msg;
			sendNotification($conn, $id, $phone, $msg, $sms_type);
			}



		} else {
			echo "0 results";
		}

		/*
			$id = $row['sms_id'];
			$toPhone = "256788084769";
			echo $phone = preg_replace('/\D+/', '', $toPhone);
			$msg = "TEST MSG";
			//sendNotification($toPhone, $msg);
			*/
		
		$conn->close();
?>

