<?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'];	
$Payment_Type = $_GET['Payment_Type'];	
$U_ID = $_GET['U_ID'];	


// Retrieve driver locations
$sql = "SELECT * FROM drivers where Avalability_Status='Online' AND Gender='$Gender' AND Accept_Private_Trips_Gender_Status='Mix gender' AND Accept_Private_Trips_Status='True'";
$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()) {
		
		
	
	$Driver_ID=$row['Driver_ID'];
	
		
	$sql1 = "SELECT * FROM trips where Driver_ID='$Driver_ID' AND Status!='Finished'";
	$result1 = $conn->query($sql1);
	if ($result1->num_rows > 0) {


}else{






	$DID=$row['ID'];
	$DName=$row['Name'];
	$DLongitude=$row['Longitude'];
	$DLatitude=$row['Latitude'];
	$DCar_Plat_Number=$row['Car_Plat_Number'];
	$DCar_Type=$row['Car_Type'];
	$DCar_Color=$row['Car_Color'];
		
		
	
	

$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);



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







$arr1 = array('Total_Riders' => $Total_Riders);
$arr2 = array('From_Longitude' => $lng1);
$arr3 = array('From_Latitude' => $lat1);
$arr4 = array('To_Longitude' => $lng2);
$arr5 = array('To_Latitude' => $lat2);
$arr6 = array('From_Location' => $From_Location);
$arr7 = array('To_Location' => $To_Location);
$arr8 = array('Total_KM_Distance' => $Total_KM_Distance);
$arr9 = array('Total_Price' => $Total_Price);
$arr10 = array('Payment_Type' => $Payment_Type);


$locations[] = $row+$arr1+$arr2+$arr3+$arr4+$arr5+$arr6+$arr7+$arr8+$arr9+$arr10;

}else{


}	
		
		
	}
		
		
		
		
		
		
		
	

		
	
		
		
		
    }
}

$conn->close();

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



echo json_encode($locations);


	  
?>