<?php
include('../config/config.php'); // Ensure DB connection is established

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $action = isset($_POST['action']) ? $_POST['action'] : '';
    $id = isset($_POST['id']) ? $_POST['id'] : '';

    switch ($action) {
  
  
  case 'add':
                            if (!isset($_POST['lorry_name']) || !isset($_POST['reg_no']) || !isset($_POST['year_model']) || !isset($_POST['capacity'])) {
                    echo "Error: Required fields are missing.";
                    exit();
                }
            
                $lorry_name = $_POST['lorry_name'];
                $reg_no = $_POST['reg_no'];
                $year_model = $_POST['year_model'];
                $capacity = $_POST['capacity'];
            
                // Debug: Log received POST data
                file_put_contents("debug_log.txt", print_r($_POST, true), FILE_APPEND);
            
                // Prepare the SQL query
                $stmt = $conn->prepare("INSERT INTO vehicles (lorry_name, reg_no, year_model, capacity, status) VALUES (?, ?, ?, ?, 'active')");
                if (!$stmt) {
                    die("Prepare failed: " . $conn->error);
                }
            
                // Bind parameters and check for errors
                $stmt->bind_param("ssis", $lorry_name, $reg_no, $year_model, $capacity);
                
                if ($stmt->execute()) {
                    echo "Vehicle added successfully!";
                } else {
                    echo "SQL Error: " . $stmt->error;
                }
            
                $stmt->close();
                break;

        case 'edit':
            if (!isset($_POST['id']) || !isset($_POST['lorry_name']) || !isset($_POST['reg_no'])) {
                echo "Error: Required fields are missing.";
                exit();
            }

            $lorry_name = $_POST['lorry_name'];
            $reg_no = $_POST['reg_no'];
            $year_model = $_POST['year_model'];
            $capacity = $_POST['capacity'];

            $stmt = $conn->prepare("UPDATE vehicles SET lorry_name=?, reg_no=?, year_model=?, capacity=? WHERE id=?");
            $stmt->bind_param("ssisi", $lorry_name, $reg_no, $year_model, $capacity, $id);

            echo $stmt->execute() ? "Vehicle updated successfully!" : "Error: " . $stmt->error;
            $stmt->close();
            break;

        case 'delete':
            if (!isset($_POST['id'])) {
                echo "Error: Missing vehicle ID.";
                exit();
            }

            $stmt = $conn->prepare("UPDATE vehicles SET status='inactive' WHERE id=?");
            $stmt->bind_param("i", $id);

            echo $stmt->execute() ? "Vehicle deleted successfully!" : "Error: " . $stmt->error;
            $stmt->close();
            break;

        default:
            echo "Invalid action!";
            break;
    }
    $conn->close();
    exit();
}

if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET['id'])) {
    $id = $_GET['id'];
    $stmt = $conn->prepare("SELECT lorry_name, reg_no, year_model, capacity, status FROM vehicles WHERE id = ?");
    $stmt->bind_param("i", $id);
    $stmt->execute();

    // Bind the result instead of using get_result() for PHP 5.6 compatibility
    $stmt->bind_result($lorry_name, $reg_no, $year_model, $capacity, $status);
    if ($stmt->fetch()) {
        echo json_encode(array(
            "lorry_name" => $lorry_name,
            "reg_no" => $reg_no,
            "year_model" => $year_model,
            "capacity" => $capacity,
            "status" => $status
        ));
    } else {
        echo json_encode(array("error" => "No data found"));
    }
    $stmt->close();
    $conn->close();
}
?>
