<?php
require "conn.php";



$OrderID  = $_POST["OrderID"];
$UserID   = $_POST["UserID"];

$messagebody = $_POST["messsage"]; 


$Token = "s";

foreach (getallheaders() as $name => $value) { 

	if($name == "Token"){
		
		$Token = $value;
	
	}
	
} 

$test=0;

	$res = mysqli_query($con,"SELECT * FROM Drivers WHERE DriverToken='$Token'");
	while($row = mysqli_fetch_assoc($res)){
		
		
		$test=4;

	}
	
	if(true){





        $UserID = "";
	   
	   $res = mysqli_query($con,"SELECT * FROM Orders WHERE OrderID=$OrderID ORDER BY OrderID DESC");

		$result = array();

			while($row = mysqli_fetch_assoc($res)){

			//$data = $row[0];
			$result[] = $row;
			$test=4;
			
			$UserID = $row["UserID"];
			$DelvryId = $row["DelvryId"];

			}
  
			
			
			$res = mysqli_query($con,"SELECT * FROM Users WHERE UserID=$UserID");
			
			$UserFirebaseToken = "";

			$result = array();

			while($row = mysqli_fetch_assoc($res)){

			//$data = $row[0];
			
			$LANG = $row["LANG"];
			
			$result[] = $row;
			$test=4;
			
			$UserFirebaseToken = $row["UserFirebaseToken"];

			}
			
			$res = mysqli_query($con,"SELECT FName FROM Drivers WHERE DriverID=$DelvryId");
			
			

			$result = array();

			while($row = mysqli_fetch_assoc($res)){

			//$data = $row[0];
			$result[] = $row;
			$test=4;
			
			$FName = $row["FName"];

			}
	   
	   
	   
	  
	
	
	
	        if($messagebody!=""){
				
				if($messagebody=="Audio"){
					
					if($LANG=="FR"){
						
						$messagebody = "Message vocal";
						
					}else if($LANG=="EN"){
						$messagebody = "Voice Message";
					}else{
						$messagebody = " رسالة صوتية";
					}
					
				}else if($messagebody=="Processed"){
					
					if($LANG=="EN"){
						
						$Title = "Order Preparing !";
						$messagebody = "Your order is being prepared. Stay tuned!";
						
					}else if($LANG=="FR"){
						$Title = "Préparation de votre commande en cours !";
						$messagebody = "Votre commande est en cours de préparation. Restez à l'écoute !";
					}else{
						$Title = "جاري تحضير طلبك";
						$messagebody = "  جاري تجهيز طلبك كن في الانتظار";
					}
					
				}else if($messagebody=="picked"){
					
					
					$sql="UPDATE Orders SET IsPrepared='YES' WHERE OrderID=$OrderID";
					   if(mysqli_query($con,$sql))
					   {}
					
					
					
					if($LANG=="EN"){
						
						$Title = "Order Pickup Confirmed!✅";
						$messagebody = "Good news, your order is on its way! Our delivery man has picked
up your items and is en route to deliver them to you. You can track your
order in real-time through the app. Thank you for choosing our service, and
we hope you enjoy your purchase!";
						
					}else if($LANG=="FR"){
						$Title = "Prise en charge de la commande confirmée ! ✅";
						$messagebody = "Bonne nouvelle, votre commande est en route ! Notre livreur a pris
en charge vos articles et est en route pour vous les livrer. Vous pouvez
suivre votre commande en temps réel via l'application. Merci d'avoir choisi
notre service, nous espérons que vous apprécierez votre achat !
";
					}else{
						$Title = "تأكيد استلام الطلب ✅";
						$messagebody = "أخبار سارة، طلبك في الطريق! قام مندوب التوصيل  بالاستلام وهو في طريقه لتوصيله لك. يمكنك تتبع طلبك في الوقت الحقيقي من خالل التطبيق. شكرًا !الختيارك خدمتنا، ونأمل أن تستمتع بمشترياتك";
					}
					
				}
				
				if($messagebody!="Deliverd"){
					send_notification2($UserFirebaseToken,$OrderID,$messagebody,$FName);
				}
	        }else{
	       send_notification($UserFirebaseToken,$OrderID);
            }
	   
	   
	   
	   
    

   
   
	}
	
	else{
		
		
			$message ="Error Token Eror";
			$success = true;
			$status_code = 200;
			$result = []; 
   echo json_encode(array('status_code' => $status_code,'success' => $success ,"data"=>$result,"message"=>$message));
		
	}
	
	
	function send_notification($UserFirebaseToken,$OrderID)
	{
		$url = 'https://fcm.googleapis.com/fcm/send';
		$fields =array(
			 'to' => $UserFirebaseToken,
			 'notification'=>array(
			 'title' => "Nouveau message",
			 'body' => "Vous avez reçu un message dans le chat de commande $OrderID",
			  "link"=> "http://sae-marketing.com/$OrderID",
			 "color"=>'$OrderID',
			 'data'=>'$OrderID')
			);

		$headers = array(
			'Authorization:key=AAAAEDOF67k:APA91bFMPNwvWHetPtqc1i--ztKxrPdSd7ZbTXvrm0LWFV6KHlkw5I-9yOdt6ZtBq1PXo3uVEDcJnFmbAKpNH7tTS9wiKLjAaeLzB0J0KMI6xvsZ5z0C-4Kn98VzSLp_fJs-ibpmOJY2',
			'Content-Type:application/json'
			);

	   $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt($ch, CURLOPT_POST, true);
       curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);  
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
       $result = curl_exec($ch);           
       if ($result === FALSE) {
           die('Curl failed: ' . curl_error($ch));
       }
       curl_close($ch);
       return $result;
	} 
	
	
		function send_notification2($UserFirebaseToken,$OrderID,$Message,$FNamew)
	{
		$url = 'https://fcm.googleapis.com/fcm/send';
		$fields =array(
			 'to' => $UserFirebaseToken,
			 'notification'=>array(
			 'title' => $FNamew,
			 'body' => $Message,
			  "link"=> "http://sae-marketing.com/$OrderID",
			 "color"=>'$OrderID',
			 'data'=>'$OrderID')
			);

		$headers = array(
			'Authorization:key=AAAAEDOF67k:APA91bFMPNwvWHetPtqc1i--ztKxrPdSd7ZbTXvrm0LWFV6KHlkw5I-9yOdt6ZtBq1PXo3uVEDcJnFmbAKpNH7tTS9wiKLjAaeLzB0J0KMI6xvsZ5z0C-4Kn98VzSLp_fJs-ibpmOJY2',
			'Content-Type:application/json'
			);

	   $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt($ch, CURLOPT_POST, true);
       curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);  
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
       $result = curl_exec($ch);           
       if ($result === FALSE) {
           die('Curl failed: ' . curl_error($ch));
       }
       curl_close($ch);
       return $result;
	} 
	
	
die;
mysqli_close($con);
?>