<?php
session_start();
include('../include/auth.php');
include('../include/lskfjwfe.php');

if ($_SERVER["REQUEST_METHOD"] === "POST") {
    if (!isset($_POST['fleet_id'])) {
        die(json_encode(["success" => false, "message" => "Missing fleet_id."]));
    }

    $fleet_id = intval($_POST['fleet_id']);

    // Delete the record from api_fleet
    $sql = "DELETE FROM api_fleet WHERE api_fleet_id = ?";
    $stmt = $db2->prepare($sql);

    if (!$stmt) {
        die(json_encode(["success" => false, "message" => "SQL Error: " . $db2->error]));
    }

    $stmt->bind_param("i", $fleet_id);

    if ($stmt->execute()) {
        // Log the deletion action
        $user_id = $_SESSION['user_id'];
        $action = "Delete Fleet";
        $details = "Deleted fleet record (Fleet ID: $fleet_id)";
        $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 deleting record."]);
    }
}
?>
