<?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'];	


// Retrieve driver locations
$sql = "SELECT * FROM trips where 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'];	
		
	
	

$lng1 = $_GET['lng1'];
$lat1 = $_GET['lat1'];
$lng2 = $_GET['lng2'];
$lat2 = $_GET['lat2'];
  
// 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){



$sql3 = "SELECT * FROM trips_users where Trip_ID='$Trip_ID' AND Gender='Male'";
$result3 = $conn->query($sql3);
$Total_Male = $result3->num_rows;


$sql4 = "SELECT * FROM trips_users where Trip_ID='$Trip_ID' AND Gender='Female'";
$result4 = $conn->query($sql4);
$Total_Female = $result4->num_rows;









$arr = array('Trip_ID' => $Trip_ID);
$arr2 = array('Current_Total_Riders' => $Current_Total_Riders);
$arr3 = array('Total_Riders' => $Total_Riders);
$arr4 = array('From_Longitude' => $lng1);
$arr5 = array('From_Latitude' => $lat1);
$arr6 = array('To_Longitude' => $lng2);
$arr7 = array('To_Latitude' => $lat2);
$arr8 = array('From_Location' => $From_Location);
$arr9 = array('To_Location' => $To_Location);
$arr10 = array('Total_KM_Distance' => $Total_KM_Distance);
$arr11 = array('Total_Price' => $Total_Price);
$arr12 = array('Total_Price_All_Riders' => $Total_Price_All_Riders);
$arr13 = array('Payment_Type' => $Payment_Type);
$arr14 = array('Spot_Number' => $Spot_Number);
$arr15 = array('Max_Limit_Riders' => $Max_Limit_Riders);
$arr16 = array('Total_Male' => $Total_Male);
$arr17 = array('Total_Female' => $Total_Female);


$locations[] = $row1+$arr+$arr2+$arr3+$arr4+$arr5+$arr6+$arr7+$arr8+$arr9+$arr10+$arr11+$arr12+$arr13+$arr14+$arr15+$arr16+$arr17;

}else{


}	
		
		
	}
		
		
		
		
		
		
		
	

		
	
		
		
		
    }
}

$conn->close();

header("Content-Type: application/json");



echo json_encode($locations);


	  
?>