<?php


ini_set('session.gc_maxlifetime', 30*60);

session_start();

include '../../../includes/config.php';



$TU_ID = $_GET['TU_ID'];
$Trip_ID = $_GET['Trip_ID'];
$Driver_ID = $_GET['Driver_ID'];
$U_ID = $_GET['U_ID'];

$Date = date("Y-m-d");



$sql1 = mysql_query($dbConn,"select * from trips_users where ID='$TU_ID'");
$row1 = mysql_fetch_array($sql1);
$U_Longitude = $row1['From_Longitude'];
$U_Latitude = $row1['From_Latitude'];



$sql2 = mysql_query($dbConn,"select * from drivers where ID='$Driver_ID'");
$row2 = mysql_fetch_array($sql2);
$D_Longitude = $row2['Longitude'];
$D_Latitude = $row2['Latitude'];


  $unit = 'K';											
  $theta = $U_Longitude - $D_Longitude;
  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));

  $dist = acos($dist);
  $dist = rad2deg($dist);
  $miles = $dist * 60 * 1.1515;
  $unit = strtoupper($unit);										
  $Distance = round(($miles * 1.609344),1);	


if ($Distance<2){


$sql3 = mysql_query($dbConn,"select * from users_trips_cancelation where User_ID='$U_ID'");
$num3 = mysqli_num_rows($sql3);

if ($num3==0){
	
	$sql4 = mysqli_query($dbConn,"insert into users_trips_cancelation (Trip_ID,User_ID,Date) values ('$T_ID','$U_ID','$Date'");


	echo "<script language='JavaScript'>
			  alert ('Alert ! you cancel your request before driver arrived within 2 km !');
      </script>";
	  
	  $sql5 = mysqli_query($dbConn,"delete from trips_users WHERE ID='$TU_ID'");






	
}elseif ($num3==1){
	
	
	$sql4 = mysqli_query($dbConn,"insert into users_trips_cancelation (Trip_ID,User_ID,Date) values ('$T_ID','$U_ID','$Date'");


	echo "<script language='JavaScript'>
			  alert ('Alert ! you cancel your request before driver arrived within 2 km for second time, in your next cancelation there will be penalty fees 0.5 JOD !');
      </script>";
	  
	  $sql5 = mysqli_query($dbConn,"delete from trips_users WHERE ID='$TU_ID'");


	
	
}elseif ($num3>=2){



	$sql4 = mysqli_query($dbConn,"insert into users_trips_cancelation (Trip_ID,User_ID,Date) values ('$T_ID','$U_ID','$Date'");


	echo "<script language='JavaScript'>
			  alert ('Alert ! you cancel your request before driver arrived within 2 km, we added penalty fees 0.5 JOD to your balance !');
      </script>";
	  
	  $sql5 = mysqli_query($dbConn,"delete from trips_users WHERE ID='$TU_ID'");
	  
	  
	  $sql6 = mysqli_query($dbConn,"select * from users where ID='$U_ID'");
	  $row6 = mysqli_fetch_array($sql6);
	  $Penalty_Balance = $row6['Penalty_Balance'];
	  $New_Penalty_Balance = $Penalty_Balance + 0.5;
	  
	  
	  $sql7 = mysqli_query($dbConn,"update users set Penalty_Balance='$New_Penalty_Balance' where ID='$U_ID'");
	  
	  
	  






}



}else{




	  $sql5 = mysqli_query($dbConn,"delete from trips_users WHERE ID='$TU_ID'");


	
	
	


}	





$sql22 = mysqli_query($dbConn,"select * from drivers where ID='$Driver_ID'");
$row22 = mysqli_fetch_array($sql22);
$Driver_Phone_Type = $row22['Phone_Type'];
$DU_ID = $row22['U_ID'];



if ($Driver_Phone_Type=='Android'){
	
	
	function sendMessage($DU_ID){
        
    
        $content = array(
            "en" => 'Trip rider request has been canceled !'
            );
        
        $fields = array(
            'app_id' => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
            'include_player_ids' => array($DU_ID),
            'data' => array("foo" => "bar"),
            'contents' => $content
        );
        
        $fields = json_encode($fields);
       // print("\nJSON sent:\n");
       // print($fields);
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        $response = curl_exec($ch);
        curl_close($ch);
        
        return $response;
    }
    
    $response = sendMessage($DU_ID);
    $return["allresponses"] = $response;
    $return = json_encode( $return);
    
   // print("\n\nJSON received:\n");
   //    print($return);
   // print("\n");
	
	
	
	
}else{
	
	
	
	 function sendMessage($DU_ID){
        
        
        
        
        
        $content = array(
            "en" => 'Trip rider request has been canceled !'
            );
        
        $fields = array(
            'app_id' => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
            'include_player_ids' => array($DU_ID),
            'data' => array("foo" => "bar"),
            'contents' => $content
        );
        
        $fields = json_encode($fields);
       // print("\nJSON sent:\n");
       // print($fields);
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        $response = curl_exec($ch);
        curl_close($ch);
        
        return $response;
    }
    
    $response = sendMessage($DU_ID);
    $return["allresponses"] = $response;
    $return = json_encode( $return);
    
   // print("\n\nJSON received:\n");
   //    print($return);
   // print("\n");	
   
   
	
	
	
}























	
	
	echo '<script language="JavaScript">
	document.location="shared-same-gender.php";
	</script>';
	


?>
