����JFIF��`�`�����Viewing File: /home/u820193700/domains/throtllr.com/public_html/backend/api/tours.php
<?php
// [FILE] backend/api/tours.php
require_once 'db.php';

$method = $_SERVER['REQUEST_METHOD'];

if ($method === 'GET') {
    if (isset($_GET['id'])) {
        if (!is_numeric($_GET['id'])) {
            send_json(['success' => false, 'message' => 'Invalid tour ID format'], 400);
        }
        $id = (int)$_GET['id'];
    }
    else {
        $id = null;
    }

    if ($id) {
        // Fetch specific tour details along with dealer info and available slots
        $stmt = $conn->prepare("SELECT t.*, d.showroom_name, d.location as dealer_location, d.upi_id, u.name as dealer_name, u.email as dealer_email,
                t.max_riders - (SELECT COUNT(*) FROM bookings b WHERE b.tour_id = t.id AND b.payment_status = 'completed') AS available_slots
                FROM tours t 
                JOIN dealers d ON t.dealer_id = d.id 
                JOIN users u ON d.user_id = u.id 
                WHERE t.id = ?"); // Removed status='approved' for single ID lookup to allow dealer previews
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $result = $stmt->get_result();

        if ($result && $row = $result->fetch_assoc()) {
            $stmt->close();
            // Fetch associated media
            $m_stmt = $conn->prepare("SELECT id, media_url, media_type, thumbnail_url, title FROM tour_media WHERE tour_id = ? ORDER BY created_at ASC");
            $m_stmt->bind_param("i", $id);
            $m_stmt->execute();
            $media_res = $m_stmt->get_result();
            $row['media'] = [];
            while ($m = $media_res->fetch_assoc()) {
                $row['media'][] = $m;
            }
            $m_stmt->close();

            // Fetch associated travel blogs (only approved)
            $b_stmt = $conn->prepare("SELECT id, title, excerpt, author_name, author_avatar, blog_url, media_type, media_url, published_date FROM tour_blogs WHERE tour_id = ? AND status = 'approved' ORDER BY published_date DESC");
            $b_stmt->bind_param("i", $id);
            $b_stmt->execute();
            $blog_res = $b_stmt->get_result();
            $row['blogs'] = [];
            while ($b = $blog_res->fetch_assoc()) {
                $row['blogs'][] = $b;
            }
            $b_stmt->close();

            // Fetch associated addons
            $a_stmt = $conn->prepare("SELECT id, label, description, price, icon FROM tour_addons WHERE tour_id = ? AND is_active = 1 ORDER BY created_at ASC");
            $a_stmt->bind_param("i", $id);
            $a_stmt->execute();
            $addon_res = $a_stmt->get_result();
            $row['addons'] = [];
            while ($a = $addon_res->fetch_assoc()) {
                $row['addons'][] = $a;
            }
            $a_stmt->close();

            send_json(['success' => true, 'data' => $row]);
        }
        else {
            if (isset($stmt)) $stmt->close();
            send_json(['success' => false, 'message' => 'Tour not found or not approved'], 404);
        }
    }
    else {
        // Fetch all approved tours with optional filtering and available slots
        $location = isset($_GET['location']) ? $_GET['location'] : '';
        $date = isset($_GET['date']) ? $_GET['date'] : '';

        $sql = "SELECT t.*, d.showroom_name, u.name as dealer_name,
                t.max_riders - (SELECT COUNT(*) FROM bookings b WHERE b.tour_id = t.id AND b.payment_status = 'completed') AS available_slots 
                FROM tours t 
                JOIN dealers d ON t.dealer_id = d.id 
                JOIN users u ON d.user_id = u.id
                WHERE t.status = 'approved'";

        $types = "";
        $params = [];

        if ($location) {
            $sql .= " AND (LOWER(t.start_location) LIKE ? OR LOWER(t.route_details) LIKE ? OR LOWER(d.location) LIKE ?)";
            $loc_param = "%" . strtolower($location) . "%";
            $params[] = $loc_param;
            $params[] = $loc_param;
            $params[] = $loc_param;
            $types .= "sss";
        }

        if ($date) {
            $sql .= " AND (t.start_date >= ?)";
            $params[] = $date;
            $types .= "s";
        }

        $sql .= " ORDER BY t.start_date ASC";

        $stmt = $conn->prepare($sql);
        if ($types) {
            $stmt->bind_param($types, ...$params);
        }
        $stmt->execute();
        $result = $stmt->get_result();
        
        $tours = [];
        while ($row = $result->fetch_assoc()) {
            $tours[] = $row;
        }
        $stmt->close();
        send_json(['success' => true, 'data' => $tours]);
    }
}
else {
    send_json(['success' => false, 'message' => 'Method not allowed'], 405);
}
?>
Back to Directory �������}�!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�������������������������������������������������������������������������������� ������w�!1AQaq"2�B���� #3R�br� $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz������������������������������������������������������������������������ ��?��_��+��?��(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(���(�����