<?php
require_once 'db.php';
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, access');
header("Access-Control-Allow-Credentials: true");

$response = array();
$method = $_SERVER['REQUEST_METHOD'];

switch ($method) {
    case 'POST':
        // Add a new driver
        $driver_name = $_POST['driver_name'];
        $phone = $_POST['phone'];
        $vehicle_number = $_POST['vehicle_number'];
        $vehicle_type = $_POST['vehicle_type'];
        $vehicle_model = $_POST['vehicle_model'];
        $driver_address = $_POST['driver_address'];
        $driver_email = $_POST['driver_email'];
        $approval = isset($_POST['approval']) ? intval($_POST['approval']) : 0;

        // File upload handling
        $license_image = isset($_FILES['license_image']) ? uploadFile($_FILES['license_image']) : null;
        $nic_image_front = isset($_FILES['nic_image_front']) ? uploadFile($_FILES['nic_image_front']) : null;
        $nic_image_back = isset($_FILES['nic_image_back']) ? uploadFile($_FILES['nic_image_back']) : null;
        $vehicle_book_image = isset($_FILES['vehicle_book_image']) ? uploadFile($_FILES['vehicle_book_image']) : null;
        $vehicle_license_image = isset($_FILES['vehicle_license_image']) ? uploadFile($_FILES['vehicle_license_image']) : null;

        $sql = "INSERT INTO registered_vehicles (driver_name, phone, vehicle_number, vehicle_type, vehicle_model, driver_address, driver_email, approval, license_image, nic_image_front, nic_image_back, vehicle_book_image, vehicle_license_image) 
                VALUES ('$driver_name', '$phone', '$vehicle_number', '$vehicle_type', '$vehicle_model', '$driver_address', '$driver_email', $approval, '$license_image', '$nic_image_front', '$nic_image_back', '$vehicle_book_image', '$vehicle_license_image')";

        if ($conn->query($sql) === TRUE) {
            $response['message'] = 'Driver added successfully';
        } else {
            $response['error'] = 'Error adding driver';
        }
        break;

    case 'PUT':
        // Update driver
        parse_str(file_get_contents("php://input"), $_PUT);
        $id = $_PUT['id'];
        $driver_name = $_PUT['driver_name'];
        $phone = $_PUT['phone'];
        $vehicle_number = $_PUT['vehicle_number'];
        $vehicle_type = $_PUT['vehicle_type'];
        $vehicle_model = $_PUT['vehicle_model'];
        $driver_address = $_PUT['driver_address'];
        $driver_email = $_PUT['driver_email'];
        $approval = isset($_PUT['approval']) ? intval($_PUT['approval']) : 0;

        $sql = "UPDATE registered_vehicles SET driver_name = '$driver_name', phone = '$phone', vehicle_number = '$vehicle_number', vehicle_type = '$vehicle_type', vehicle_model = '$vehicle_model', driver_address = '$driver_address', driver_email = '$driver_email', approval = $approval WHERE id = $id";

        if ($conn->query($sql) === TRUE) {
            $response['message'] = 'Driver updated successfully';
        } else {
            $response['error'] = 'Error updating driver';
        }
        break;

    case 'DELETE':
        // Delete a driver
        parse_str(file_get_contents("php://input"), $_DELETE);
        $id = $_DELETE['id'];

        $sql = "DELETE FROM registered_vehicles WHERE id = $id";

        if ($conn->query($sql) === TRUE) {
            $response['message'] = 'Driver deleted successfully';
        } else {
            $response['error'] = 'Error deleting driver';
        }
        break;

    default:
        $response['error'] = 'Invalid request method';
        break;
}

echo json_encode($response);
$conn->close();

function uploadFile($file) {
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($file["name"]);
    if (move_uploaded_file($file["tmp_name"], $target_file)) {
        return basename($file["name"]);
    }
    return null;
}
?>
