����JFIF��`�`�����Viewing File: /home/u820193700/domains/throtllr.in/public_html/api/get_bikes.php
<?php
ob_start();
require_once __DIR__ . '/../includes/bike_models.php';

$fallbackBikes = [
    [
        'id' => 1,
        'reference_id' => 'RE-001',
        'model_name' => 'Royal Enfield Classic 350',
        'price' => 185000.00,
        'year' => 2021,
        'registration_year' => 2021,
        'km_driven' => 12500,
        'ownership' => '1st',
        'insurance_status' => 'Active',
        'location' => 'Bangalore',
        'colour' => 'Stealth Black',
        'engine_capacity' => '350',
        'description' => 'Meticulously maintained Royal Enfield Classic 350 in Stealth Black color. Single owner, regular dealership service, and absolute zero issues.',
        'image_url' => 'https://images.unsplash.com/photo-1558981806-ec527fa84c39?q=80&w=800'
    ],
    [
        'id' => 2,
        'reference_id' => 'RE-002',
        'model_name' => 'Royal Enfield Himalayan 411',
        'price' => 215000.00,
        'year' => 2022,
        'registration_year' => 2022,
        'km_driven' => 8200,
        'ownership' => '1st',
        'insurance_status' => 'Active',
        'location' => 'Bangalore',
        'colour' => 'Gravel Grey',
        'engine_capacity' => '411',
        'description' => 'Ready for adventure. Highly capable tourer with pannier stays and crash guards already installed.',
        'image_url' => 'https://images.unsplash.com/photo-1558981420-c5f9899a28bc?q=80&w=800'
    ],
    [
        'id' => 3,
        'reference_id' => 'RE-003',
        'model_name' => 'Royal Enfield Interceptor 650',
        'price' => 295000.00,
        'year' => 2023,
        'registration_year' => 2023,
        'km_driven' => 4500,
        'ownership' => '1st',
        'insurance_status' => 'Comprehensive',
        'location' => 'Bangalore',
        'colour' => 'Canyon Red',
        'engine_capacity' => '650',
        'description' => 'Smooth parallel-twin interceptor in pristine condition. Custom exhaust fitted for a rumble.',
        'image_url' => 'https://images.unsplash.com/photo-1605810731210-909289260c6c?q=80&w=800'
    ],
    [
        'id' => 4,
        'reference_id' => 'RE-004',
        'model_name' => 'Royal Enfield Meteor 350',
        'price' => 195000.00,
        'year' => 2022,
        'registration_year' => 2022,
        'km_driven' => 9800,
        'ownership' => '1st',
        'insurance_status' => 'Active',
        'location' => 'Bangalore',
        'colour' => 'Fireball Yellow',
        'engine_capacity' => '350',
        'description' => 'Comfortable cruiser with cruising windshield and backrest. Perfect for city commutes.',
        'image_url' => 'https://images.unsplash.com/photo-155898120-c5f9899a28bc?q=80&w=800'
    ],
    [
        'id' => 5,
        'reference_id' => 'RE-005',
        'model_name' => 'Royal Enfield Bullet 350',
        'price' => 95000.00,
        'year' => 2018,
        'registration_year' => 2018,
        'km_driven' => 28000,
        'ownership' => '2nd',
        'insurance_status' => 'Active',
        'location' => 'Chennai',
        'colour' => 'Standard Black',
        'engine_capacity' => '350',
        'description' => 'The legend itself. Heavy flywheel and original thump sound. Kept in vintage condition.',
        'image_url' => 'https://images.unsplash.com/photo-1558980394-4c7c9299fe96?q=80&w=800'
    ],
    [
        'id' => 6,
        'reference_id' => 'RE-006',
        'model_name' => 'Royal Enfield Thunderbird 350',
        'price' => 98000.00,
        'year' => 2017,
        'registration_year' => 2017,
        'km_driven' => 32000,
        'ownership' => '2nd',
        'insurance_status' => 'Active',
        'location' => 'Chennai',
        'colour' => 'Lightning Grey',
        'engine_capacity' => '350',
        'description' => 'Ultimate tourer Thunderbird with projector headlights and massive fuel tank.',
        'image_url' => 'https://images.unsplash.com/photo-1558980663-3685c65f80b8?q=80&w=800'
    ],
    [
        'id' => 7,
        'reference_id' => 'RE-007',
        'model_name' => 'Royal Enfield Continental GT 650',
        'price' => 325000.00,
        'year' => 2023,
        'registration_year' => 2023,
        'km_driven' => 3200,
        'ownership' => '1st',
        'insurance_status' => 'Comprehensive',
        'location' => 'Bangalore',
        'colour' => 'British Racing Green',
        'engine_capacity' => '650',
        'description' => 'A true cafe racer. Clip-on handlebars, single-seat cowl, and British Racing Green body color.',
        'image_url' => 'https://images.unsplash.com/photo-1558981806-ec527fa84c39?q=80&w=800'
    ],
    [
        'id' => 8,
        'reference_id' => 'RE-008',
        'model_name' => 'Royal Enfield Scram 411',
        'price' => 175000.00,
        'year' => 2023,
        'registration_year' => 2023,
        'km_driven' => 6100,
        'ownership' => '1st',
        'insurance_status' => 'Active',
        'location' => 'Bangalore',
        'colour' => 'Skyline Blue',
        'engine_capacity' => '411',
        'description' => 'Urban scrambler based on the Himalayan platform. Extremely nimble and fun to ride in traffic.',
        'image_url' => 'https://images.unsplash.com/photo-1558981420-c5f9899a28bc?q=80&w=800'
    ],
    [
        'id' => 9,
        'reference_id' => 'RE-009',
        'model_name' => 'Royal Enfield Bullet 500',
        'price' => 135000.00,
        'year' => 2019,
        'registration_year' => 2019,
        'km_driven' => 18500,
        'ownership' => '1st',
        'insurance_status' => 'Active',
        'location' => 'Chennai',
        'colour' => 'Forest Green',
        'engine_capacity' => '500',
        'description' => 'Powerful 500cc Bullet with fuel injection. Original thump, chrome finishes, and massive torque.',
        'image_url' => 'https://images.unsplash.com/photo-1558980394-4c7c9299fe96?q=80&w=800'
    ],
    [
        'id' => 10,
        'reference_id' => 'RE-010',
        'model_name' => 'Royal Enfield Classic 500',
        'price' => 145000.00,
        'year' => 2018,
        'registration_year' => 2018,
        'km_driven' => 22000,
        'ownership' => '2nd',
        'insurance_status' => 'Active',
        'location' => 'Bangalore',
        'colour' => 'Desert Storm',
        'engine_capacity' => '500',
        'description' => 'Royal Enfield Classic 500 in beautiful Desert Storm matte color. Runs smoothly and pulls like a train.',
        'image_url' => 'https://images.unsplash.com/photo-1558981806-ec527fa84c39?q=80&w=800'
    ],
    [
        'id' => 11,
        'reference_id' => 'RE-011',
        'model_name' => 'Royal Enfield Himalayan 450',
        'price' => 265000.00,
        'year' => 2024,
        'registration_year' => 2024,
        'km_driven' => 2500,
        'ownership' => '1st',
        'insurance_status' => 'Comprehensive',
        'location' => 'Bangalore',
        'colour' => 'Hanle Black',
        'engine_capacity' => '450',
        'description' => 'The brand-new Sherpa 450 liquid-cooled adventure bike. Riding modes, tubeless spoke wheels, and color TFT screen.',
        'image_url' => 'https://images.unsplash.com/photo-1558981420-c5f9899a28bc?q=80&w=800'
    ],
    [
        'id' => 12,
        'reference_id' => 'RE-012',
        'model_name' => 'Royal Enfield Continental GT 650',
        'price' => 290000.00,
        'year' => 2022,
        'registration_year' => 2022,
        'km_driven' => 14000,
        'ownership' => '2nd',
        'insurance_status' => 'Active',
        'location' => 'Bangalore',
        'colour' => 'GT Red',
        'engine_capacity' => '650',
        'description' => 'Authentic parallel-twin cafe racer Continental GT 650. Beautiful GT Red, fitted with premium tyres and brakes.',
        'image_url' => 'https://images.unsplash.com/photo-1558981420-c5f9899a28bc?q=80&w=800'
    ],
    [
        'id' => 13,
        'reference_id' => 'RE-013',
        'model_name' => 'Royal Enfield Super Meteor 650',
        'price' => 345000.00,
        'year' => 2023,
        'registration_year' => 2023,
        'km_driven' => 5200,
        'ownership' => '1st',
        'insurance_status' => 'Comprehensive',
        'location' => 'Bangalore',
        'colour' => 'Interstellar Green',
        'engine_capacity' => '650',
        'description' => 'Premium flagship cruiser from Royal Enfield. USD forks, LED headlights, alloy wheels, and show-stopping cruiser ergonomics.',
        'image_url' => 'https://images.unsplash.com/photo-1558980394-4c7c9299fe96?q=80&w=800'
    ]
];

$useFallback = false;
$bikes = [];

try {
    require_once __DIR__ . '/../includes/db.php';
    if (!isset($pdo)) {
        $useFallback = true;
    }
} catch (Exception $e) {
    $useFallback = true;
}

// Request parameters
$cc = isset($_GET['cc']) ? trim($_GET['cc']) : '';
$segment = isset($_GET['segment']) ? trim($_GET['segment']) : '';
$budget = isset($_GET['budget']) ? trim($_GET['budget']) : '';

// Segment to models mapping
$segmentModels = $bike_categories;

if (!$useFallback) {
    try {
        $query = "SELECT b.*, bi.image_url 
                  FROM bikes b 
                  LEFT JOIN bike_images bi ON b.id = bi.bike_id AND bi.is_main = 1 
                  WHERE b.status = 'available'";
        $params = [];

        if ($cc !== '') {
            $query .= " AND b.engine_capacity = ?";
            $params[] = $cc;
        }

        if ($segment !== '' && isset($segmentModels[$segment])) {
            $placeholders = implode(',', array_fill(0, count($segmentModels[$segment]), '?'));
            $query .= " AND b.model_name IN ($placeholders)";
            foreach ($segmentModels[$segment] as $m) {
                $params[] = $m;
            }
        }

        if ($budget !== '') {
            if ($budget === 'below-1') {
                $query .= " AND b.price < 100000";
            } elseif ($budget === '1-2') {
                $query .= " AND b.price >= 100000 AND b.price <= 200000";
            } elseif ($budget === '2-3') {
                $query .= " AND b.price > 200000 AND b.price <= 300000";
            } elseif ($budget === 'above-3') {
                $query .= " AND b.price > 300000";
            }
        }

        $stmt = $pdo->prepare($query);
        $stmt->execute($params);
        $bikes = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        // Check if database is empty and if we did not filter anything, 
        // to automatically populate from fallback list
        if (empty($bikes) && $cc === '' && $segment === '' && $budget === '') {
            $useFallback = true;
        }
    } catch (PDOException $e) {
        $useFallback = true;
    }
}

if ($useFallback) {
    // Perform filtering on the fallback array
    $bikes = array_filter($fallbackBikes, function($bike) use ($cc, $segment, $budget, $segmentModels) {
        // Filter by CC
        if ($cc !== '' && $bike['engine_capacity'] !== $cc) {
            return false;
        }
        
        // Filter by Segment
        if ($segment !== '' && isset($segmentModels[$segment])) {
            if (!in_array($bike['model_name'], $segmentModels[$segment])) {
                return false;
            }
        }
        
        // Filter by Budget
        if ($budget !== '') {
            $price = $bike['price'];
            if ($budget === 'below-1' && $price >= 100000) {
                return false;
            } elseif ($budget === '1-2' && ($price < 100000 || $price > 200000)) {
                return false;
            } elseif ($budget === '2-3' && ($price <= 200000 || $price > 300000)) {
                return false;
            } elseif ($budget === 'above-3' && $price <= 300000) {
                return false;
            }
        }
        
        return true;
    });
    
    // Reset array keys
    $bikes = array_values($bikes);
}

// Clean buffer and send JSON
ob_clean();
header('Content-Type: application/json');
echo json_encode(['success' => true, 'data' => $bikes]);
exit;
Back to Directory �������}�!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�������������������������������������������������������������������������������� ������w�!1AQaq"2�B���� #3R�br� $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz������������������������������������������������������������������������ ��?��_��+��?��(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(�����