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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $fleet_id = intval($_POST['fleet_id']);
    $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']);

    $update_query = "UPDATE fleet 
                     SET veh_id = $veh_id, lic_num = '$lic_num', color = '$color', notes = '$notes', active = $active 
                     WHERE fleet_id = $fleet_id";

    if (mysqli_query($db2, $update_query)) {
        // Log the action
        $action = "Edit Vehicle in Fleet";
        $details = "Updated Vehicle (fleet_id: $fleet_id, 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 updating vehicle: ' . mysqli_error($db2)]);
    }
}
?>
