<?php


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

session_start();

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



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



$sql1 = mysqli_query($dbConn,"select * from drivers where ID='$Driver_ID'");
$row1 = mysqli_fetch_array($sql1);
$Driver_Longitude = $row1['Longitude'];
$Driver_Latitude= $row1['Latitude'];


$sql2 = mysqli_query($dbConn,"select * from trip_spots where Trip_ID='$Trip_ID' AND Spot_Number='$Spot_Number'");
$row2 = mysqli_fetch_array($sql2);
$Spot_Longitude = $row2['Longitude'];
$Spot_Latitude= $row2['Latitude'];





  $unit = 'K';											
  $theta = $Driver_Longitude - $Spot_Longitude;
  $dist = sin(deg2rad($Driver_Latitude)) * sin(deg2rad($Spot_Latitude)) +  cos(deg2rad($Driver_Latitude)) * cos(deg2rad($Spot_Latitude)) * cos(deg2rad($theta));

  $dist = acos($dist);
  $dist = rad2deg($dist);
  $miles = $dist * 60 * 1.1515;
  $unit = strtoupper($unit);										
  $Distance = round(($miles * 1.609344),1);	
  $Distance2 = round(($miles * 1.609344),1);	
  
  
    if ($Distance2<=0.2){



$sql22 = mysqli_query($dbConn,"select * from users where ID='$U_ID'");
$row22 = mysqli_fetch_array($sql22);
$User_Phone_Type = $row22['Phone_Type'];
$UU_ID = $row22['U_ID'];



if ($User_Phone_Type=='Android'){
	
	
	function sendMessage($UU_ID){
        
    
        $content = array(
            "en" => 'كن جاهزاً... سائقك يأتي إلى موقعك على بعد أقل من 200 متر !'
            );
        
        $fields = array(
            'app_id' => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
            'include_player_ids' => array($UU_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($UU_ID);
    $return["allresponses"] = $response;
    $return = json_encode( $return);
    
   // print("\n\nJSON received:\n");
   //    print($return);
   // print("\n");
	
	
	
	
}else{
	
	
	
	 function sendMessage($UU_ID){
        
        
        
        
        
        $content = array(
            "en" => 'كن جاهزاً... سائقك يأتي إلى موقعك على بعد أقل من 200 متر !'
            );
        
        $fields = array(
            'app_id' => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
            'include_player_ids' => array($UU_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($UU_ID);
    $return["allresponses"] = $response;
    $return = json_encode( $return);
    
   // print("\n\nJSON received:\n");
   //    print($return);
   // print("\n");	
   
   
	
	
	
}















	}
  
  
  if ($Distance<=0.1){
	  	 
$sql3 = mysql_query($dbConn,"select * from users_trips_not_confirm_pickup where User_ID='$U_ID'");
$num3 = mysqli_num_rows($sql3); 
if ($num3==0){
	
	
$sql4 = mysql_query($dbConn,"select * from users_trips_not_confirm_pickup where User_ID='$U_ID' AND Trip_ID='$Trip_ID'");
if (mysqli_num_rows($sql4)>0){
	
}else{
	
	$sql5 = mysqli_query($dbConn,"insert into users_trips_not_confirm_pickup (Trip_ID,User_ID,Date) values ('$Trip_ID','$U_ID','$Date'");
	
	echo "<script language='JavaScript'>
			  alert ('نعتذر ! لم تقم بتأكيد الالتقاط الخاص بك في الرحلة !');
      </script>";	

}


}elseif ($num3==1){

$sql4 = mysql_query($dbConn,"select * from users_trips_not_confirm_pickup where User_ID='$U_ID' AND Trip_ID='$Trip_ID'");
if (mysqli_num_rows($sql4)>0){
	
}else{
	
	$sql5 = mysqli_query($dbConn,"insert into users_trips_not_confirm_pickup (Trip_ID,User_ID,Date) values ('$Trip_ID','$U_ID','$Date'");

	echo "<script language='JavaScript'>
			  alert ('نعتذر ! لم تقم بتأكيد الالتقاط الخاص بك في الرحلة !');
      </script>";	

}


}elseif ($num3>=2){


$sql4 = mysql_query($dbConn,"select * from users_trips_not_confirm_pickup where User_ID='$U_ID' AND Trip_ID='$Trip_ID'");
if (mysqli_num_rows($sql4)>0){
	
}else{
	
	$sql5 = mysqli_query($dbConn,"insert into users_trips_not_confirm_pickup (Trip_ID,User_ID,Date) values ('$Trip_ID','$U_ID','$Date'");

	echo "<script language='JavaScript'>
			  alert ('نعتذر ! لم تقم بتأكيد الالتقاط الخاص بك في الرحلة !, أضفنا رسوم جزاء 0.5 دينار أردني إلى رصيدك !');
      </script>";	
	  
	  
	$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'");  

}



}	





  }
?>
