false, 'message' => 'Invalid vehicle ID']); exit; } try { $stmt = $pdo->prepare(" SELECT vi.*, d.first_name, d.last_name FROM vehicle_inspections vi LEFT JOIN drivers d ON vi.inspector_cid COLLATE utf8mb4_unicode_ci = d.hr_cid COLLATE utf8mb4_unicode_ci WHERE vi.vehicle_id = ? ORDER BY vi.inspection_date DESC, vi.inspection_time DESC LIMIT 50 "); $stmt->execute([$vehicle_id]); $history = $stmt->fetchAll(PDO::FETCH_ASSOC); // Decode JSON items for each record foreach ($history as &$record) { $record['items'] = json_decode($record['items_json'], true); unset($record['items_json']); } echo json_encode(['success' => true, 'data' => $history]); } catch (PDOException $e) { echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]); }