<?php

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



// Connect to your MySQL database
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sheeld";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$Gender = $_GET['Gender'];
$Total_Riders = $_GET['Total_Riders'];
$Total_KM_Distance = $_GET['Total_KM_Distance'];
$Date = $_GET['Date'];
$From_Location = $_GET['From_Location'];	
$To_Location = $_GET['To_Location'];	
$Time = $_GET['Time'];
$Total_Price = $_GET['Total_Price'];	
$Total_Price_All_Riders = $_GET['Total_Price_All_Riders'];
$Payment_Type = $_GET['Payment_Type'];	
$U_ID = $_GET['U_ID'];	
$Driver_ID = $_GET['Driver_ID'];
$TTrip_ID = $_GET['Trip_ID'];
$Spot_Number = $_GET['Spot_Number'];
$TU_ID = $_GET['TU_ID'];
$Date = $_GET['Date'];



$sql112233 = mysqli_query($dbConn,"select * from drivers");
$row112233 = mysql_fetch_array($sql112233);
$DDD_Longitude = $row112233['Longitude'];
$DDD_Latitude = $row112233['Latitude'];


$sql445566 = mysqli_query($dbConn,"select * from trip_spots where Trip_ID='$Trip_ID' AND Spot_Number='$Spot_Number'");
$row445566 = mysql_fetch_array($sql445566);
$SSS_Longitude = $row445566['Longitude'];
$SSS_Latitude = $row445566['Latitude'];









  $unit = 'K';											
  $theta = $DDD_Longitude - $SSS_Longitude;
  $dist = sin(deg2rad($DDD_Latitude)) * sin(deg2rad($SSS_Latitude)) +  cos(deg2rad($DDD_Latitude)) * cos(deg2rad($SSS_Latitude)) * 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>0.2){


	


$sql1 = mysql_query($dbConn,"select * from trips_users where ID='$TU_ID'");
$row1 = mysql_fetch_array($sql1);
$lng1 = $row1['From_Longitude'];
$lat1 = $row1['From_Latitude'];
$lng2 = $row1['To_Longitude'];
$lat2 = $row1['To_Longitude'];











// Retrieve driver locations
$sql = "SELECT * FROM trips where ID!='$TTrip_ID' AND Trip_Type='Shared' AND Date='$Date' AND (Status='In process' AND Gender_Type='Mix') AND (Max_Limit_Riders>='$Total_Riders' AND Current_Total_Riders<'$Total_Riders')";
$result = $conn->query($sql);

$locations = array();

	function haversineDistance($lat1, $lon1, $lat2, $lon2) {
    $earthRadius = 6371000; // in meters
    $deltaLat = deg2rad($lat2 - $lat1);
    $deltaLon = deg2rad($lon2 - $lon1);

    $a = sin($deltaLat / 2) * sin($deltaLat / 2) +
         cos(deg2rad($lat1)) * cos(deg2rad($lat2)) *
         sin($deltaLon / 2) * sin($deltaLon / 2);
    $c = 2 * atan2(sqrt($a), sqrt(1 - $a));

    $distance = $earthRadius * $c;

    return $distance;
}


if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
		
		
	$Trip_ID=$row['ID'];
	$Driver_ID=$row['Driver_ID'];
	$Current_Total_Riders=$row['Current_Total_Riders'];
	$Max_Limit_Riders=$row['Max_Limit_Riders'];
		
	$sql1 = "SELECT * FROM drivers where ID='$Driver_ID'";
	$result1 = $conn->query($sql1);	
	$row1 = $result1->fetch_assoc();	
		
	$DID=$row1['ID'];
	$DName=$row1['Name'];
	$DLongitude=$row1['Longitude'];
	$DLatitude=$row1['Latitude'];
	$DCar_Plat_Number=$row1['Car_Plat_Number'];
	$DCar_Type=$row1['Car_Type'];
	$DCar_Color=$row1['Car_Color'];
		
		
		
	$sql2 = "SELECT * FROM trip_spots where Trip_ID='$Trip_ID'";
	$result2 = $conn->query($sql2);
    while ($row2 = $result2->fetch_assoc()) {
		
		
		
	$Spot_Number = $row2['Spot_Number'];
	$Spot_Longitude = $row2['Longitude'];
	$Spot_Latitude = $row2['Latitude'];	
		
	
	

  
// Define start and finish coordinates of the road map
$startLatitude = $lat1;
$startLongitude = $lng1;
$finishLatitude = $lat2;
$finishLongitude = $lng2;

// Driver's current coordinates
$driverLatitude = $DLatitude;
$driverLongitude = $DLongitude;

// Define the acceptable distance threshold in meters
$acceptableDistance = 2000; // 2 kilometers

// Calculate distances
$startDistance = haversineDistance($driverLatitude, $driverLongitude, $startLatitude, $startLongitude);
$finishDistance = haversineDistance($Spot_Latitude, $Spot_Longitude, $finishLatitude, $finishLongitude);



// Check if the driver's location is within the acceptable range
if ($startDistance <= $acceptableDistance && $finishDistance <= $acceptableDistance){




echo "<script language='JavaScript'>
			  alert ('Attention ! we find trips to your location nearest to you ! check those trips !');
      </script>";
	  
	
	
	echo '<script language="JavaScript">
	document.location="shared-mix-gender-44.php?TTrip_ID='.$TTrip_ID.'&Trip_ID='.$Trip_ID.'&TU_ID='.$TU_ID.'&lng1='.$lng1.'&lat1='.$lat1.'&lng2='.$lng2.'&lat2='.$lat2'.&From_Location='.$From_Location.'&To_Location='.$To_Location.'&Date='.$Date.'&Total_KM_Distance='.$Total_KM_Distance.'&Total_Price='.$Total_Price.'&Total_Price_All_Riders='.$Total_Price_All_Riders.'&Payment_Type='.$Payment_Type.'&Total_Riders='.$Total_Riders.'&Gender='.$Gender.'&U_ID='.$U_ID.'";
	</script>';



}else{


}	
		
		
	}
		
		
		
		
		
		
		
	

		
	
		
		
		
    }
}





	}else{
		
		
		
		
		
	}
























	  
?>