<?php
header('Access-Control-Allow-Origin: *');
header("Content-Type: application/json; charset=UTF-8");
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");
require_once 'db.php';
header('Content-Type: application/json');

$response = array();
$action = $_GET['action'] ?? '';

switch ($action) {
    case 'fetch':
        $sql = "SELECT * FROM registered_vehicles order by id DESC";
        $result = $conn->query($sql);
        $drivers = array();

        while ($row = $result->fetch_assoc()) {
            $drivers[] = $row;
        }

        $response = $drivers;
        break;

    case 'create':
        $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'];
        $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
        $approval = $_POST['approval'];

        // Handle file uploads
        $license_image = null;
        $nic_image_front = null;
        $nic_image_back = null;
        $vehicle_book_image = null;
        $vehicle_license_image = null;
        $profile_picture = null;

        if (isset($_FILES['license_image'])) {
            $license_image = basename($_FILES['license_image']['name']);
            move_uploaded_file($_FILES['license_image']['tmp_name'], "uploads/$license_image");
        }
        if (isset($_FILES['nic_image_front'])) {
            $nic_image_front = basename($_FILES['nic_image_front']['name']);
            move_uploaded_file($_FILES['nic_image_front']['tmp_name'], "uploads/$nic_image_front");
        }
        if (isset($_FILES['nic_image_back'])) {
            $nic_image_back = basename($_FILES['nic_image_back']['name']);
            move_uploaded_file($_FILES['nic_image_back']['tmp_name'], "uploads/$nic_image_back");
        }
        if (isset($_FILES['vehicle_book_image'])) {
            $vehicle_book_image = basename($_FILES['vehicle_book_image']['name']);
            move_uploaded_file($_FILES['vehicle_book_image']['tmp_name'], "uploads/$vehicle_book_image");
        }
        if (isset($_FILES['vehicle_license_image'])) {
            $vehicle_license_image = basename($_FILES['vehicle_license_image']['name']);
            move_uploaded_file($_FILES['vehicle_license_image']['tmp_name'], "uploads/$vehicle_license_image");
        }
        if (isset($_FILES['profile_picture'])) {
            $profile_picture = basename($_FILES['profile_picture']['name']);
            move_uploaded_file($_FILES['profile_picture']['tmp_name'], "uploads/$profile_picture");
        }

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

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

    case 'update':
        $id = $_POST['id'];
        $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 = $_POST['approval'];

        // Handle file uploads
        $license_image = null;
        $nic_image_front = null;
        $nic_image_back = null;
        $vehicle_book_image = null;
        $vehicle_license_image = null;
        $profile_picture = null;

        if (isset($_FILES['license_image'])) {
            $license_image = basename($_FILES['license_image']['name']);
            move_uploaded_file($_FILES['license_image']['tmp_name'], "uploads/$license_image");
        }
        if (isset($_FILES['nic_image_front'])) {
            $nic_image_front = basename($_FILES['nic_image_front']['name']);
            move_uploaded_file($_FILES['nic_image_front']['tmp_name'], "uploads/$nic_image_front");
        }
        if (isset($_FILES['nic_image_back'])) {
            $nic_image_back = basename($_FILES['nic_image_back']['name']);
            move_uploaded_file($_FILES['nic_image_back']['tmp_name'], "uploads/$nic_image_back");
        }
        if (isset($_FILES['vehicle_book_image'])) {
            $vehicle_book_image = basename($_FILES['vehicle_book_image']['name']);
            move_uploaded_file($_FILES['vehicle_book_image']['tmp_name'], "uploads/$vehicle_book_image");
        }
        if (isset($_FILES['vehicle_license_image'])) {
            $vehicle_license_image = basename($_FILES['vehicle_license_image']['name']);
            move_uploaded_file($_FILES['vehicle_license_image']['tmp_name'], "uploads/$vehicle_license_image");
        }
        if (isset($_FILES['profile_picture'])) {
            $profile_picture = basename($_FILES['profile_picture']['name']);
            move_uploaded_file($_FILES['profile_picture']['tmp_name'], "uploads/$profile_picture");
        }

        

        $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 = '$license_image', nic_image_front = '$nic_image_front', nic_image_back = '$nic_image_back', vehicle_book_image = '$vehicle_book_image', vehicle_license_image = '$vehicle_license_image', profile_picture = '$profile_picture'
                WHERE id = $id";

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

    case 'delete':
        $id = $_GET['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: ' . $conn->error;
        }
        break;

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

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