<?php
include 'db.php';

$current_lat = $_GET['current_lat'];
$current_lng = $_GET['current_lng'];

$sql = "SELECT id, driver_name, phone, vehicle_type, vehicle_current_lat, vehicle_current_lng,
        (6371 * acos(cos(radians($current_lat)) * cos(radians(vehicle_current_lat)) * cos(radians(vehicle_current_lng) - radians($current_lng)) + sin(radians($current_lat)) * sin(radians(vehicle_current_lat)))) AS distance
        FROM registered_vehicles
        HAVING distance < 10
        ORDER BY distance";

$result = $conn->query($sql);

$vehicles = array();
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $vehicles[] = $row;
    }
}

header('Content-Type: application/json');
echo json_encode($vehicles);

$conn->close();
?>
