<?php


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

session_start();

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



$U_ID = $_SESSION['U_Log'];




if (!$_SESSION['U_Log'])
echo '<script language="JavaScript">
 document.location="../../Users-Sign-In.php";
</script>';





$sql3 = mysqli_query($dbConn,"select * from users where ID='$U_ID'");
$row3 = mysqli_fetch_array($sql3);
$Name=$row3['Name'];
$Phone_Number=$row3['Phone_Number'];
$Gender=$row3['Gender'];
$Photo=$row3['Photo'];
$lon=$row3['Longitude'];
$lat=$row3['Latitude'];
$Total_Rates=$row3['Total_Rates'];


$TP_ID = $_GET['TP_ID'];
	if ($TP_ID==''){



	$From_Longitude = $_GET['lng1'];	
	$lng1 = $_GET['lng1'];	
	$From_Latitude = $_GET['lat1'];	
	$lat1 = $_GET['lat1'];	
	$To_Longitude = $_GET['lng2'];	
	$To_Latitude = $_GET['lat2'];	
	$From_Location = $_GET['From_Location'];	
	$To_Location = $_GET['To_Location'];	
	$Date = $_GET['Date'];
	$Time = $_GET['Time'];
	$Total_KM_Distance = $_GET['Total_KM_Distance'];	
	$Total_Price = $_GET['Total_Price'];	
	$Total_Price_All_Riders = $_GET['Total_Price_All_Riders'];	
	$Payment_Type = $_GET['Payment_Type'];	
	$Total_Riders = $_GET['Total_Riders'];	
	$Gender = $_GET['Gender'];	
	
	
	
$Date  = date("Y-m-d");

	
	
$sql1 = mysqli_query($dbConn,"insert into trips_privates (Date,User_ID,Driver_ID,Gender,Total_Riders,From_Longitude,From_Latitude,To_Longitude,To_Latitude,From_Location,To_Location,Total_KM_Distance,Total_Price,Payment_Type,Status)
values ('$Date','$U_ID','','$Gender','$Total_Riders','$From_Longitude','$From_Latitude','$To_Longitude','$To_Latitude','$From_Location','$To_Location','$Total_KM_Distance','$Total_Price','$Payment_Type','Pending')");




$sql2 = mysqli_query($dbConn,"select * from trips_privates where User_ID='$U_ID' AND Status='Pending'");
$row2 = mysqli_fetch_array($sql2);
$TP_ID = $row2['ID'];




$sql3 = mysqli_query($dbConn,"select * from drivers where Avalability_Status='Online' AND Accept_Private_Trips_Gender_Status='Mix gender' AND Accept_Private_Trips_Status='True'");
while ($row3 = mysqli_fetch_array($sql3)){

$Driver_ID = $row3['ID'];
$Driver_Longitude = $row3['Longitude'];
$Driver_Latitude = $row3['Latitude'];



  $unit = 'K';											
  $theta = $Driver_Longitude - $From_Longitude;
  $dist = sin(deg2rad($Driver_Latitude)) * sin(deg2rad($From_Latitude)) +  cos(deg2rad($Driver_Latitude)) * cos(deg2rad($From_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){
		
		
		


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




if ($Driver_Phone_Type=='Android'){
	
	
	function sendMessage($DU_ID){
        
    
        $content = array(
            "en" => 'You have a new private trip rider request !'
            );
        
        $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" => 'You have a new private trip rider request !'
            );
        
        $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");	
   
   
	
	
	
}

}else{
	
	
	
}



}

	
	
	
	
	
	
	
	
	
	
	}else{
		
		
		$U_ID = $_GET['U_ID'];	
	$From_Longitude = $_GET['From_Longitude'];	
	$From_Latitude = $_GET['From_Latitude'];	
	$To_Longitude = $_GET['To_Longitude'];	
	$To_Latitude = $_GET['To_Latitude'];	
	$From_Location = $_GET['From_Location'];	
	$To_Location = $_GET['To_Location'];	
	$Total_KM_Distance = $_GET['Total_KM_Distance'];	
	$Total_Price = $_GET['Total_Price'];	
	$Payment_Type = $_GET['Payment_Type'];	
	$Total_Riders = $_GET['Total_Riders'];	
	
		$lng1 = $_GET['From_Longitude'];	

		$lat1 = $_GET['From_Latitude'];	



$Date  = date("Y-m-d");
		
		
	
$sql3 = mysqli_query($dbConn,"select * from drivers where Avalability_Status='Online' AND Accept_Private_Trips_Gender_Status='Mix gender' AND Accept_Private_Trips_Status='True'");
while ($row3 = mysqli_fetch_array($sql3)){

$Driver_ID = $row3['ID'];
$Driver_Longitude = $row3['Longitude'];
$Driver_Latitude = $row3['Latitude'];



  $unit = 'K';											
  $theta = $Driver_Longitude - $From_Longitude;
  $dist = sin(deg2rad($Driver_Latitude)) * sin(deg2rad($From_Latitude)) +  cos(deg2rad($Driver_Latitude)) * cos(deg2rad($From_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){
		
		
		


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




if ($Driver_Phone_Type=='Android'){
	
	
	function sendMessage($DU_ID){
        
    
        $content = array(
            "en" => 'You have a new private trip rider request !'
            );
        
        $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" => 'You have a new private trip rider request !'
            );
        
        $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");	
   
   
	
	
	
}

}else{
	
	
	
}



}
		
		
		
		
		
		
		
		
		
		
	}
	
	
	

?>

<!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="../../../css/bootstrap.min.css">
    <!-- FontAwesome -->
    <link rel="stylesheet" href="../../../css/fontawesome.css">
    <!-- Main Style -->
    <link rel="stylesheet" href="../../../css/styles.css">
    <title>We are looking for your driver</title>
	
	    <link rel="shortcut icon" href="../../../images/icon.ico"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,user-scalable=0"/>









	<style>
@font-face {
   font-family: myFirstFont;
   src: url(../../../fonts/Arial-Rounded-MT-Bold-Regular.ttf);
   font-size:8px;
}

body {
   font-family: myFirstFont !important;
}


@font-face {
   font-family: myFirstFont2;
   src: url(../../../fonts/GE-SS-Two-Bold.otf);
   font-size:8px;
}





</style>




</head>
<body>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script type="text/javascript">
        function getLocation() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function(position) {
                    var latitude = position.coords.latitude;
                    var longitude = position.coords.longitude;

                    // Send this to your PHP script via AJAX or other means
                    sendDataToServer(latitude, longitude);
                });
            } else {
                alert("Geolocation is not supported by this browser.");
            }
        }

        function sendDataToServer(latitude, longitude) {
            // Using fetch API to send data to PHP
            fetch('update-my-location-1.php?U_ID=<?php echo $U_ID; ?>	', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    latitude: latitude,
                    longitude: longitude
                })
            })
            .then(response => response.json())
            .then(data => {
                console.log('Success:', data);
            })
            .catch((error) => {
                console.error('Error:', error);
            });
        }

        // Keep updating every 7 seconds
        setInterval(getLocation, 7000);
    </script>


<!--Loading Container Start-->
<div id="load" class="loading-overlay display-flex flex-column justify-content-center align-items-center">
    <div class="primary-color font-28 fas fa-spinner fa-spin"></div>
</div>
<!--Loading Container End-->


<div class="row h-100">
    <div class="col-xs-12 col-sm-12 remaining-height">

        <!--Page Title & Icons Start-->
        <div class="header-icons-container position-relative text-center">
            <span class="float-left menu-open closed">
                    <img src="../../../icons/menu.svg" alt="Menu Hamburger Icon">
            </span>
            <span class="title"><br>We are looking for your driver ...</span>
            
<span class="float-right" style="margin-top:-27px !important">
                    <img src="../../../images/icon.png" alt="Logo" width="14px" height="20px">
                </span>
				
        </div>
        <!--Page Title & Icons End-->

       
  
     
				
		<div class="text-center header-icon-logo-margin header-icon-logo-margin-extra">
                <img src="../../../images/logo.png" width="75%" alt="Main Logo">
            </div>




<!--Loading Container Start-->
<div id="load" class="loading-overlay display-flex flex-column justify-content-center align-items-center">
    <div class="primary-color font-28 fas fa-spinner fa-spin"></div>
</div>
<!--Loading Container End-->



<script type="text/javascript">   
    function Redirect() 
    {  
        window.location="private-mix-gender-4.php?TP_ID=<?php echo $TP_ID; ?>&U_ID=<?php echo $U_ID; ?>"; 
    } 
    setTimeout('Redirect()', 5000);   
</script>











        
    </div>

   <?php
   include 'menu.php';
   ?>

</body>
</html>