<?php
include 'confing.php';

$response = array();

$vehicle_type = $_POST['vehicle_type'];
$vehicle_brand = $_POST['vehicle_brand'];
$vehicle_model = $_POST['vehicle_model'];
$registration_no = $_POST['registration_no'];
$alternate_no = $_POST['alternate_no'];
$block = $_POST['block'];
$share_location = $_POST['share_location'];
$apartment = $_POST['apartment'];
$user_id = $_POST['user_id'];
$vehicle_image = $_POST['image']; 
$type = $_POST['type']; 
$date_time = date("d-m-Y h:i A"); 

$image_name = uniqid() . ".jpg";
$image_path = "upload/" . $image_name;

if (!file_exists("upload")) {
    mkdir("upload", 0777, true);
}

if (file_put_contents($image_path, base64_decode($vehicle_image))) {

    $sql = "INSERT INTO add_vehicle (
        vehicle_type, vehicle_brand, vehicle_model, registration_no, alternate_no,
        block, share_location, apartment, user_id, image, date_time,type)
        VALUES ('$vehicle_type', '$vehicle_brand', '$vehicle_model', '$registration_no', '$alternate_no',
        '$block', '$share_location', '$apartment', '$user_id', '$image_path', '$date_time', '$type')";

    if (mysqli_query($conn, $sql)) {
        $response['status'] = "success";
        $response['message'] = "Vehicle inserted successfully with image and time";
    } else {
        $response['status'] = "error";
        $response['message'] = "Database Error: " . mysqli_error($conn);
    }

} else {
    $response['status'] = "error";
    $response['message'] = "Image upload failed";
}

echo json_encode($response);
?>
