<?php
include('../include/auth.php');
include('../include/lskfjwfe.php'); // Database connectivity file

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $veh_id = intval($_POST['veh_id']);
    $lic_num = mysqli_real_escape_string($db2, $_POST['lic_num']);
    $color = mysqli_real_escape_string($db2, $_POST['color']);
    $notes = mysqli_real_escape_string($db2, $_POST['notes']);
    $active = intval($_POST['active']);

    $insert_query = "INSERT INTO fleet (veh_id, lic_num, color, notes, active) 
                     VALUES ($veh_id, '$lic_num', '$color', '$notes', $active)";

    if (mysqli_query($db2, $insert_query)) {
        // Log the action
        $action = "Add Vehicle to Fleet";
        $details = "Added Vehicle (veh_id: $veh_id, License: $lic_num, Color: $color, Active: $active)";
        $log_query = "INSERT INTO action_logs (action, details, user, timestamp) 
                      VALUES ('$action', '$details', '$user', NOW())";
        mysqli_query($db2, $log_query);

        echo json_encode(['success' => true]);
    } else {
        echo json_encode(['success' => false, 'message' => 'Error adding vehicle: ' . mysqli_error($db2)]);
    }
}
?>
