<?php
session_start();
include 'layouts/dbconn.php';
include 'includes/menu_config.php';

// Set timezone
date_default_timezone_set('Africa/Nairobi');

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$user_id = $_SESSION['user_id'];
$vehicle_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

if (!$vehicle_id) {
    header('Location: vehicles.php');
    exit();
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $vehicle_number = trim($_POST['vehicle_number']);
    $vehicle_type = $_POST['vehicle_type'];
    $make = trim($_POST['make']);
    $model = trim($_POST['model']);
    $year = intval($_POST['year']);
    $capacity_weight = floatval($_POST['capacity_weight']);
    $capacity_volume = floatval($_POST['capacity_volume']);
    $fuel_type = $_POST['fuel_type'];
    $insurance_expiry = $_POST['insurance_expiry'] ?: null;
    $license_expiry = $_POST['license_expiry'] ?: null;
    $status = $_POST['status'];
    
    // Check for duplicate vehicle number (excluding current vehicle)
    $check_sql = "SELECT vehicle_id FROM vehicles WHERE vehicle_number = ? AND vehicle_id != ?";
    $stmt_check = $conn->prepare($check_sql);
    $stmt_check->bind_param("si", $vehicle_number, $vehicle_id);
    $stmt_check->execute();
    
    if ($stmt_check->get_result()->num_rows > 0) {
        $error_message = "Vehicle number already exists";
    } else {
        $update_sql = "UPDATE vehicles SET 
                       vehicle_number = ?, vehicle_type = ?, make = ?, model = ?, year = ?, 
                       capacity_weight = ?, capacity_volume = ?, fuel_type = ?, 
                       insurance_expiry = ?, license_expiry = ?, status = ?, 
                       updated_at = NOW() 
                       WHERE vehicle_id = ?";
        
        $stmt_update = $conn->prepare($update_sql);
        $stmt_update->bind_param("ssssiddssssi", 
            $vehicle_number, $vehicle_type, $make, $model, $year,
            $capacity_weight, $capacity_volume, $fuel_type,
            $insurance_expiry, $license_expiry, $status, $vehicle_id
        );
        
        if ($stmt_update->execute()) {
            // Log activity
            $log_sql = "INSERT INTO activity_log (user_id, action, module, record_id, old_values, new_values) 
                       VALUES (?, 'UPDATE', 'vehicles', ?, 'vehicle_updated', ?)";
            $stmt_log = $conn->prepare($log_sql);
            $details = json_encode(['vehicle_number' => $vehicle_number, 'updated_by' => $user_id]);
            $stmt_log->bind_param("iss", $user_id, $vehicle_id, $details);
            $stmt_log->execute();
            
            $_SESSION['success_message'] = "Vehicle updated successfully";
            header("Location: vehicle_details.php?id=$vehicle_id");
            exit();
        } else {
            $error_message = "Failed to update vehicle";
        }
    }
}

// Fetch vehicle details
$sql_vehicle = "SELECT * FROM vehicles WHERE vehicle_id = ?";
$stmt_vehicle = $conn->prepare($sql_vehicle);
$stmt_vehicle->bind_param("i", $vehicle_id);
$stmt_vehicle->execute();
$vehicle = $stmt_vehicle->get_result()->fetch_assoc();

if (!$vehicle) {
    header('Location: vehicles.php');
    exit();
}

$conn->close();
?>

<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Edit Vehicle - ' . $vehicle['vehicle_number'])); ?>
    <?php include 'layouts/head-css.php'; ?>
    
    <!-- SweetAlert2 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.css" rel="stylesheet">
    
    <!-- Remix Icons -->
    <link href="https://cdn.jsdelivr.net/npm/remixicon@3.5.0/fonts/remixicon.css" rel="stylesheet">
    
    <style>
        .edit-card {
            background: white;
            border-radius: 12px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            border: 1px solid #e3e6f0;
            margin-bottom: 1.5rem;
        }
        
        .form-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 12px 12px 0 0;
            padding: 1.5rem;
        }
        
        .form-section {
            background: #f8f9fa;
            border-radius: 8px;
            padding: 1.5rem;
            margin-bottom: 1.5rem;
            border: 1px solid #e9ecef;
        }
        
        .section-title {
            font-size: 1.1rem;
            font-weight: 600;
            color: #2c3e50;
            margin-bottom: 1rem;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }
        
        .form-floating {
            margin-bottom: 1rem;
        }
        
        .form-control:focus {
            border-color: #667eea;
            box-shadow: 0 0 0 0.2rem rgba(102, 126, 234, 0.25);
        }
        
        .form-select:focus {
            border-color: #667eea;
            box-shadow: 0 0 0 0.2rem rgba(102, 126, 234, 0.25);
        }
        
        .btn-update {
            background: linear-gradient(45deg, #28a745, #20c997);
            border: none;
            color: white;
            padding: 0.75rem 2rem;
            border-radius: 10px;
            font-weight: 600;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(40,167,69,0.3);
        }
        
        .btn-update:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(40,167,69,0.4);
            color: white;
        }
        
        .vehicle-preview {
            background: linear-gradient(45deg, #e3f2fd, #f3e5f5);
            border-radius: 12px;
            padding: 1.5rem;
            text-align: center;
        }
        
        .vehicle-icon {
            width: 80px;
            height: 80px;
            border-radius: 50%;
            background: linear-gradient(45deg, #667eea, #764ba2);
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 2rem;
            margin: 0 auto 1rem;
        }
        
        .preview-info {
            margin: 0.5rem 0;
        }
        
        .preview-label {
            font-weight: 600;
            color: #495057;
        }
        
        .preview-value {
            color: #6c757d;
        }
        
        .capacity-indicator {
            background: #e9ecef;
            border-radius: 6px;
            padding: 0.5rem;
            margin: 0.5rem 0;
        }
        
        .status-preview {
            display: inline-block;
            padding: 0.25rem 0.75rem;
            border-radius: 15px;
            font-size: 0.75rem;
            font-weight: 600;
            text-transform: uppercase;
        }
        
        .status-active { background: #d1e7dd; color: #0a3622; }
        .status-maintenance { background: #fff3cd; color: #664d03; }
        .status-inactive { background: #f8d7da; color: #58151c; }
        
        .alert-custom {
            border-radius: 8px;
            padding: 1rem;
            margin-bottom: 1.5rem;
            border: 1px solid;
        }
        
        .alert-error {
            background: #f8d7da;
            border-color: #f5c6cb;
            color: #721c24;
        }
        
        .alert-success {
            background: #d1e7dd;
            border-color: #badbcc;
            color: #0f5132;
        }
        
        @media (max-width: 768px) {
            .form-header {
                padding: 1rem;
            }
            
            .form-section {
                padding: 1rem;
            }
            
            .btn-update {
                width: 100%;
            }
        }
    </style>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    
                    <!-- Page Header -->
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">
                                    <i class="ri-edit-line me-2"></i>Edit Vehicle - <?php echo $vehicle['vehicle_number']; ?>
                                </h4>
                                <div class="page-title-right">
                                    <ol class="breadcrumb m-0">
                                        <li class="breadcrumb-item"><a href="index.php">Dashboard</a></li>
                                        <li class="breadcrumb-item"><a href="vehicles.php">Vehicles</a></li>
                                        <li class="breadcrumb-item"><a href="vehicle_details.php?id=<?php echo $vehicle_id; ?>"><?php echo $vehicle['vehicle_number']; ?></a></li>
                                        <li class="breadcrumb-item active">Edit</li>
                                    </ol>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Error/Success Messages -->
                    <?php if (isset($error_message)): ?>
                    <div class="row">
                        <div class="col-12">
                            <div class="alert-custom alert-error">
                                <div class="d-flex align-items-center">
                                    <i class="ri-error-warning-line fs-4 me-3"></i>
                                    <div>
                                        <strong>Error!</strong>
                                        <p class="mb-0"><?php echo $error_message; ?></p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php endif; ?>

                    <!-- Main Form -->
                    <form method="POST" id="editVehicleForm">
                        <div class="row">
                            <!-- Form Fields -->
                            <div class="col-xl-8">
                                <div class="edit-card">
                                    <div class="form-header">
                                        <h5 class="mb-0">
                                            <i class="ri-edit-line me-2"></i>Vehicle Information
                                        </h5>
                                    </div>
                                    <div class="card-body">
                                        
                                        <!-- Basic Information Section -->
                                        <div class="form-section">
                                            <div class="section-title">
                                                <i class="ri-information-line"></i>
                                                Basic Information
                                            </div>
                                            
                                            <div class="row">
                                                <div class="col-md-6">
                                                    <div class="form-floating">
                                                        <input type="text" class="form-control" id="vehicle_number" name="vehicle_number" 
                                                               value="<?php echo htmlspecialchars($vehicle['vehicle_number']); ?>" required>
                                                        <label for="vehicle_number">Vehicle Number *</label>
                                                    </div>
                                                </div>
                                                
                                                <div class="col-md-6">
                                                    <div class="form-floating">
                                                        <select class="form-select" id="vehicle_type" name="vehicle_type" required>
                                                            <option value="">Select Type</option>
                                                            <option value="TRUCK" <?php echo $vehicle['vehicle_type'] == 'TRUCK' ? 'selected' : ''; ?>>Truck</option>
                                                            <option value="VAN" <?php echo $vehicle['vehicle_type'] == 'VAN' ? 'selected' : ''; ?>>Van</option>
                                                            <option value="MOTORCYCLE" <?php echo $vehicle['vehicle_type'] == 'MOTORCYCLE' ? 'selected' : ''; ?>>Motorcycle</option>
                                                            <option value="CAR" <?php echo $vehicle['vehicle_type'] == 'CAR' ? 'selected' : ''; ?>>Car</option>
                                                            <option value="OTHER" <?php echo $vehicle['vehicle_type'] == 'OTHER' ? 'selected' : ''; ?>>Other</option>
                                                        </select>
                                                        <label for="vehicle_type">Vehicle Type *</label>
                                                    </div>
                                                </div>
                                            </div>
                                            
                                            <div class="row">
                                                <div class="col-md-4">
                                                    <div class="form-floating">
                                                        <input type="text" class="form-control" id="make" name="make" 
                                                               value="<?php echo htmlspecialchars($vehicle['make']); ?>">
                                                        <label for="make">Make</label>
                                                    </div>
                                                </div>
                                                
                                                <div class="col-md-4">
                                                    <div class="form-floating">
                                                        <input type="text" class="form-control" id="model" name="model" 
                                                               value="<?php echo htmlspecialchars($vehicle['model']); ?>">
                                                        <label for="model">Model</label>
                                                    </div>
                                                </div>
                                                
                                                <div class="col-md-4">
                                                    <div class="form-floating">
                                                        <input type="number" class="form-control" id="year" name="year" 
                                                               min="1990" max="<?php echo date('Y') + 1; ?>" 
                                                               value="<?php echo $vehicle['year']; ?>">
                                                        <label for="year">Year</label>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                        <!-- Capacity Section -->
                                        <div class="form-section">
                                            <div class="section-title">
                                                <i class="ri-scales-3-line"></i>
                                                Capacity Information
                                            </div>
                                            
                                            <div class="row">
                                                <div class="col-md-6">
                                                    <div class="form-floating">
                                                        <input type="number" class="form-control" id="capacity_weight" name="capacity_weight" 
                                                               step="0.1" min="0" value="<?php echo $vehicle['capacity_weight']; ?>">
                                                        <label for="capacity_weight">Weight Capacity (tons)</label>
                                                    </div>
                                                </div>
                                                
                                                <div class="col-md-6">
                                                    <div class="form-floating">
                                                        <input type="number" class="form-control" id="capacity_volume" name="capacity_volume" 
                                                               step="0.1" min="0" value="<?php echo $vehicle['capacity_volume']; ?>">
                                                        <label for="capacity_volume">Volume Capacity (m³)</label>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                        <!-- Technical Details Section -->
                                        <div class="form-section">
                                            <div class="section-title">
                                                <i class="ri-settings-3-line"></i>
                                                Technical Details
                                            </div>
                                            
                                            <div class="row">
                                                <div class="col-md-6">
                                                    <div class="form-floating">
                                                        <select class="form-select" id="fuel_type" name="fuel_type">
                                                            <option value="">Select Fuel Type</option>
                                                            <option value="PETROL" <?php echo $vehicle['fuel_type'] == 'PETROL' ? 'selected' : ''; ?>>Petrol</option>
                                                            <option value="DIESEL" <?php echo $vehicle['fuel_type'] == 'DIESEL' ? 'selected' : ''; ?>>Diesel</option>
                                                            <option value="ELECTRIC" <?php echo $vehicle['fuel_type'] == 'ELECTRIC' ? 'selected' : ''; ?>>Electric</option>
                                                            <option value="HYBRID" <?php echo $vehicle['fuel_type'] == 'HYBRID' ? 'selected' : ''; ?>>Hybrid</option>
                                                        </select>
                                                        <label for="fuel_type">Fuel Type</label>
                                                    </div>
                                                </div>
                                                
                                                <div class="col-md-6">
                                                    <div class="form-floating">
                                                        <select class="form-select" id="status" name="status" required>
                                                            <option value="ACTIVE" <?php echo $vehicle['status'] == 'ACTIVE' ? 'selected' : ''; ?>>Active</option>
                                                            <option value="MAINTENANCE" <?php echo $vehicle['status'] == 'MAINTENANCE' ? 'selected' : ''; ?>>Maintenance</option>
                                                            <option value="INACTIVE" <?php echo $vehicle['status'] == 'INACTIVE' ? 'selected' : ''; ?>>Inactive</option>
                                                        </select>
                                                        <label for="status">Status</label>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                        <!-- Document Expiry Section -->
                                        <div class="form-section">
                                            <div class="section-title">
                                                <i class="ri-file-list-3-line"></i>
                                                Document Expiry Dates
                                            </div>
                                            
                                            <div class="row">
                                                <div class="col-md-6">
                                                    <div class="form-floating">
                                                        <input type="date" class="form-control" id="insurance_expiry" name="insurance_expiry" 
                                                               value="<?php echo $vehicle['insurance_expiry']; ?>">
                                                        <label for="insurance_expiry">Insurance Expiry Date</label>
                                                    </div>
                                                </div>
                                                
                                                <div class="col-md-6">
                                                    <div class="form-floating">
                                                        <input type="date" class="form-control" id="license_expiry" name="license_expiry" 
                                                               value="<?php echo $vehicle['license_expiry']; ?>">
                                                        <label for="license_expiry">License Expiry Date</label>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                        <!-- Form Actions -->
                                        <div class="text-end">
                                            <a href="vehicle_details.php?id=<?php echo $vehicle_id; ?>" class="btn btn-outline-secondary me-2">
                                                <i class="ri-arrow-left-line me-1"></i>Cancel
                                            </a>
                                            <button type="submit" class="btn btn-update">
                                                <i class="ri-save-line me-1"></i>Update Vehicle
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <!-- Vehicle Preview -->
                            <div class="col-xl-4">
                                <div class="edit-card">
                                    <div class="card-header bg-transparent border-bottom">
                                        <h5 class="card-title mb-0">
                                            <i class="ri-eye-line me-2"></i>Vehicle Preview
                                        </h5>
                                    </div>
                                    <div class="card-body">
                                        <div class="vehicle-preview">
                                            <div class="vehicle-icon" id="previewIcon">
                                                <i class="ri-truck-line"></i>
                                            </div>
                                            
                                            <div class="preview-info">
                                                <div class="preview-label">Vehicle Number</div>
                                                <div class="preview-value" id="previewNumber"><?php echo $vehicle['vehicle_number']; ?></div>
                                            </div>
                                            
                                            <div class="preview-info">
                                                <div class="preview-label">Type</div>
                                                <div class="preview-value" id="previewType"><?php echo $vehicle['vehicle_type']; ?></div>
                                            </div>
                                            
                                            <div class="preview-info">
                                                <div class="preview-label">Make & Model</div>
                                                <div class="preview-value" id="previewMakeModel">
                                                    <?php echo $vehicle['make'] . ' ' . $vehicle['model']; ?>
                                                </div>
                                            </div>
                                            
                                            <div class="preview-info">
                                                <div class="preview-label">Year</div>
                                                <div class="preview-value" id="previewYear"><?php echo $vehicle['year']; ?></div>
                                            </div>
                                            
                                            <div class="capacity-indicator">
                                                <div class="preview-label">Capacity</div>
                                                <div class="preview-value">
                                                    <span id="previewWeight"><?php echo $vehicle['capacity_weight']; ?></span> tons • 
                                                    <span id="previewVolume"><?php echo $vehicle['capacity_volume']; ?></span> m³
                                                </div>
                                            </div>
                                            
                                            <div class="preview-info">
                                                <div class="preview-label">Fuel Type</div>
                                                <div class="preview-value" id="previewFuel"><?php echo $vehicle['fuel_type']; ?></div>
                                            </div>
                                            
                                            <div class="preview-info">
                                                <div class="preview-label">Status</div>
                                                <div class="preview-value">
                                                    <span class="status-preview status-<?php echo strtolower($vehicle['status']); ?>" id="previewStatus">
                                                        <?php echo $vehicle['status']; ?>
                                                    </span>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <!-- Additional Information -->
                                <div class="edit-card">
                                    <div class="card-header bg-transparent border-bottom">
                                        <h5 class="card-title mb-0">
                                            <i class="ri-information-line me-2"></i>Additional Information
                                        </h5>
                                    </div>
                                    <div class="card-body">
                                        <div class="info-item mb-3">
                                            <small class="text-muted">Created Date</small>
                                            <div class="fw-bold"><?php echo date('M j, Y H:i', strtotime($vehicle['created_at'])); ?></div>
                                        </div>
                                        
                                        <div class="info-item mb-3">
                                            <small class="text-muted">Last Updated</small>
                                            <div class="fw-bold"><?php echo date('M j, Y H:i', strtotime($vehicle['updated_at'])); ?></div>
                                        </div>
                                        
                                        <div class="info-item mb-3">
                                            <small class="text-muted">Vehicle Age</small>
                                            <div class="fw-bold"><?php echo (date('Y') - $vehicle['year']); ?> years</div>
                                        </div>
                                        
                                        <?php if ($vehicle['insurance_expiry']): ?>
                                        <div class="info-item mb-3">
                                            <small class="text-muted">Insurance Status</small>
                                            <?php 
                                            $insurance_days = (strtotime($vehicle['insurance_expiry']) - time()) / 86400;
                                            ?>
                                            <div class="fw-bold <?php echo $insurance_days <= 0 ? 'text-danger' : ($insurance_days <= 30 ? 'text-warning' : 'text-success'); ?>">
                                                <?php if ($insurance_days <= 0): ?>
                                                Expired
                                                <?php elseif ($insurance_days <= 30): ?>
                                                Expires in <?php echo ceil($insurance_days); ?> days
                                                <?php else: ?>
                                                Valid
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                        <?php endif; ?>
                                        
                                        <?php if ($vehicle['license_expiry']): ?>
                                        <div class="info-item">
                                            <small class="text-muted">License Status</small>
                                            <?php 
                                            $license_days = (strtotime($vehicle['license_expiry']) - time()) / 86400;
                                            ?>
                                            <div class="fw-bold <?php echo $license_days <= 0 ? 'text-danger' : ($license_days <= 30 ? 'text-warning' : 'text-success'); ?>">
                                                <?php if ($license_days <= 0): ?>
                                                Expired
                                                <?php elseif ($license_days <= 30): ?>
                                                Expires in <?php echo ceil($license_days); ?> days
                                                <?php else: ?>
                                                Valid
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>

    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    
    <!-- SweetAlert2 JS -->
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.js"></script>
    
    <script>
$(document).ready(function() {
    // Real-time preview updates
    updatePreview();
    
    // Bind events for real-time preview
    $('#vehicle_number').on('input', updatePreview);
    $('#vehicle_type').on('change', updatePreview);
    $('#make, #model').on('input', updatePreview);
    $('#year').on('input', updatePreview);
    $('#capacity_weight, #capacity_volume').on('input', updatePreview);
    $('#fuel_type').on('change', updatePreview);
    $('#status').on('change', updatePreview);

    // Form submission with validation
    $('#editVehicleForm').on('submit', function(e) {
        e.preventDefault();
        
        // Basic validation
        if (!$('#vehicle_number').val().trim()) {
            showAlert('error', 'Please enter a vehicle number');
            return;
        }
        
        if (!$('#vehicle_type').val()) {
            showAlert('error', 'Please select a vehicle type');
            return;
        }
        
        // Show confirmation
        Swal.fire({
            title: 'Update Vehicle',
            text: 'Are you sure you want to update this vehicle information?',
            icon: 'question',
            showCancelButton: true,
            confirmButtonText: 'Yes, Update',
            cancelButtonText: 'Cancel',
            confirmButtonColor: '#28a745'
        }).then((result) => {
            if (result.isConfirmed) {
                // Show loading
                Swal.fire({
                    title: 'Updating Vehicle...',
                    text: 'Please wait while we update the vehicle information.',
                    icon: 'info',
                    allowOutsideClick: false,
                    showConfirmButton: false,
                    didOpen: () => {
                        Swal.showLoading();
                    }
                });
                
                // Submit form
                this.submit();
            }
        });
    });
});

function updatePreview() {
    // Update vehicle number
    const vehicleNumber = $('#vehicle_number').val() || 'Vehicle Number';
    $('#previewNumber').text(vehicleNumber);
    
    // Update vehicle type and icon
    const vehicleType = $('#vehicle_type').val() || 'Not Selected';
    $('#previewType').text(vehicleType);
    
    // Update icon based on type
    const iconMap = {
        'TRUCK': 'ri-truck-line',
        'VAN': 'ri-car-line',
        'MOTORCYCLE': 'ri-motorbike-line',
        'CAR': 'ri-car-line',
        'OTHER': 'ri-car-line'
    };
    const iconClass = iconMap[vehicleType] || 'ri-truck-line';
    $('#previewIcon i').attr('class', iconClass);
    
    // Update make and model
    const make = $('#make').val() || '';
    const model = $('#model').val() || '';
    const makeModel = (make + ' ' + model).trim() || 'Not Specified';
    $('#previewMakeModel').text(makeModel);
    
    // Update year
    const year = $('#year').val() || 'Not Set';
    $('#previewYear').text(year);
    
    // Update capacity
    const weight = $('#capacity_weight').val() || '0';
    const volume = $('#capacity_volume').val() || '0';
    $('#previewWeight').text(weight);
    $('#previewVolume').text(volume);
    
    // Update fuel type
    const fuelType = $('#fuel_type').val() || 'Not Selected';
    $('#previewFuel').text(fuelType);
    
    // Update status
    const status = $('#status').val() || 'ACTIVE';
    $('#previewStatus').text(status).attr('class', 'status-preview status-' + status.toLowerCase());
}

function showAlert(type, message) {
    Swal.fire({
        title: type === 'error' ? 'Error' : 'Success',
        text: message,
        icon: type,
        timer: 3000,
        showConfirmButton: false
    });
}

// Auto-save functionality (optional)
let autoSaveTimer;
function scheduleAutoSave() {
    clearTimeout(autoSaveTimer);
    autoSaveTimer = setTimeout(() => {
        // Could implement auto-save to localStorage here
        console.log('Auto-save triggered');
    }, 5000);
}

// Bind auto-save to form changes
$('#editVehicleForm input, #editVehicleForm select').on('change input', scheduleAutoSave);
</script>

    <!-- App js -->
    <script src="assets/js/app.js"></script>
</body>
</html>