<?php
require "conn.php";



$OrderID  = $_POST["OrderID"];
$DriverID = $_POST["DriverID"];
$Rating   = $_POST["Rating"];
$Review   = $_POST["Review"];

$ShopID = $_POST["ShopID"];
$RatingShop   = $_POST["RatingShop"];
$ReviewShop   = $_POST["ReviewShop"];


$Token = "s";

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

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

$test=0;

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

	}
	
	$res = mysqli_query($con,"SELECT UserID FROM Orders WHERE OrderID=$OrderID");
	while($row = mysqli_fetch_assoc($res)){
		
		
		$UserID=$row["UserID"];

	}
	
	 $sql="INSERT INTO DriverRates (DriverID,UserID,RATE) VALUES ('$DriverID','$UserID','$Rating');";
			   if(mysqli_query($con,$sql))
			   {
				   
			   }
		
		$sql="INSERT INTO ShopRates (ShopID,UserID,RATE) VALUES ('$ShopID','$UserID','$RatingShop');";
			   if(mysqli_query($con,$sql))
			   {
				   
			   }	
	
	if(true){

   $sql="UPDATE Orders SET OrderState='Rated',OrderRate=$Rating,OrderReview='$Review' WHERE OrderID=$OrderID";
   if(mysqli_query($con,$sql))
   {
	   
	  // $sql="UPDATE Drivers SET DriverRate=($Rating+DriverRate)/DriverOrdersNum WHERE DriverID=$DriverID";
		//	   if(mysqli_query($con,$sql))
			//   {
				   
			//   }
	   
	   	
	   
	   
	   
	   $sql="UPDATE Drivers SET TotolRate=TotolRate+1 WHERE DriverID=$DriverID";
	   if(mysqli_query($con,$sql))
       {
		   
	   }
	   
	   
	   
	   if($Review!=""){
	    $sql="UPDATE Drivers SET TotalReview=TotalReview+1 WHERE DriverID=$DriverID";
	   if(mysqli_query($con,$sql))
       {
		   
	   }
	   
	   }
	   
	   $sql="UPDATE Shops SET RateTime = RateTime +1, RatePoints=RatePoints+$RatingShop WHERE ShopID=$ShopID";
	   if(mysqli_query($con,$sql))
       {
		   
	   }
	   
	   $sql="UPDATE Shops SET ShopRate = RatePoints/RateTime WHERE ShopID=$ShopID";
	   if(mysqli_query($con,$sql))
       {
		   
	   }
	   
	   
	
	$message ="Finished Sucssessfuly";
    $success = true;
    $status_code = 200;
	$result = []; 
    echo json_encode(array('status_code' => $status_code,'success' => $success ,"data"=>$result,"message"=>$message));
    
    
    $sql="INSERT INTO DriverNotification (OrderID,DriverID,NotificationText) VALUES ('$OrderID','$DriverID','Vous êtes évalué $Rating pour le numéro de commande $OrderID');";
   if(mysqli_query($con,$sql))
   {
       
   }
   
   
   $DriverToken = "";
   
   $res = mysqli_query($con,"SELECT * FROM Drivers WHERE DriverID=$DriverID");
	while($row = mysqli_fetch_assoc($res)){
		
		
		$DriverToken = $row["FirebaseDriverToken"];
		
		$test=4;
	//	send_notification($DriverToken,$OrderID,$Rating);
	}
    

   }
   else
   {

	$message ="Error Updated";
    $success = false;
    $status_code = 200;
	$result = []; 
    echo json_encode(array('status_code' => $status_code,'success' => $success ,"data"=>$result,"message"=>$message));

   }
   
	}
	
	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($DriverToken,$OrderID,$Rating)
	{
		$url = 'https://fcm.googleapis.com/fcm/send';
		$fields =array(
			 'to' => $DriverToken,
			 'notification'=>array(
			 'title' => "Vous êtes évalué",
			 'body' => "Vous êtes évalué $Rating pour le numéro de commande $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);
?>