<?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 = uploadFile('license_image');
        $nic_image_front = uploadFile('nic_image_front');
        $nic_image_back = uploadFile('nic_image_back');
        $vehicle_book_image = uploadFile('vehicle_book_image');
        $vehicle_license_image = uploadFile('vehicle_license_image');

        $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 = $_PUT['approval'];

        // File upload handling
        $license_image = uploadFile('license_image');
        $nic_image_front = uploadFile('nic_image_front');
        $nic_image_back = uploadFile('nic_image_back');
        $vehicle_book_image = uploadFile('vehicle_book_image');
        $vehicle_license_image = uploadFile('vehicle_license_image');

        $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,
                    license_image = IFNULL('$license_image', license_image), nic_image_front = IFNULL('$nic_image_front', nic_image_front), 
                    nic_image_back = IFNULL('$nic_image_back', nic_image_back), vehicle_book_image = IFNULL('$vehicle_book_image', vehicle_book_image),
                    vehicle_license_image = IFNULL('$vehicle_license_image', vehicle_license_image)
                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($inputName) {
    if (!isset($_FILES[$inputName])) {
        return null;
    }

    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES[$inputName]["name"]);
    if (move_uploaded_file($_FILES[$inputName]["tmp_name"], $target_file)) {
        return basename($_FILES[$inputName]["name"]);
    }

    return null;
}
?>
