<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

include_once '../config/database.php';
include_once '../objects/vehicle.php';

session_start();
date_default_timezone_set('Asia/Rangoon');

$database = new Database();
$db = $database->getConnection();

$vehicle = new Vehicle($db);
$data = json_decode(file_get_contents("php://input"));

$vehicle->id = $data->id ?? null;
$vehicle->license_plate = $data->license_plate ?? null;
$vehicle->vehicle_category = $data->vehicle_category ?? null;
$vehicle->transmission = $data->transmission ?? null;
$vehicle->seats = $data->seats ?? 0;
$vehicle->remark = $data->remark ?? null;
$vehicle->entry_by = $_SESSION['userid'] ?? "";
$vehicle->update_by = $_SESSION['userid'] ?? "";

if ($vehicle->id) {
    echo json_encode(array("message" => $vehicle->update() ? "updated" : "error"));
} else {
    if ($vehicle->exist()) {
        echo json_encode(array("message" => "duplicate"));
    } else {
        echo json_encode(array("message" => $vehicle->create() ? "created" : "error"));
    }
}
?>
