<?php
/**
 * ============================================================
 * WINGS TOUR & TRAVELS - DETAIL PAGE
 * ============================================================
 */
require_once __DIR__ . '/header.php';
require_once __DIR__ . '/db_config.php';

// URL se ID get karein
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

// Database se option ki details fetch karein taaki title aur description dynamic rahein
$query = "SELECT * FROM dropdown_options WHERE id = $id";
$result = mysqli_query($conn,$query);
$row = ($result && mysqli_num_rows($result) > 0) ? mysqli_fetch_assoc($result) : null;

$title =$row ? $row['option_name'] : 'Tour Details';$description = $row ? ($row['description'] ?? 'Experience the divine journey with Wings Tour & Travels.') : '';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo htmlspecialchars($title); ?> | Wings Tour & Travels</title>

    <!-- Font Awesome & Google Fonts & Bootstrap & Swiper CSS -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.css" />

    <style>
        :root {
            --saffron: #D4A359;
            --deep-saffron: #E65100;
            --forest-green: #1B5E20;
            --dark-green: #0F172A;
            --text-dark: #1E293B;
            --white: #FFFFFF;
            --soft-orange: #fff6ed;
            --border: rgba(15, 23, 42, 0.10);
        }

        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { background: #FAFAFA; color: var(--text-dark); font-family: 'Poppins', sans-serif; line-height: 1.6; overflow-x: hidden; }

        /* FULL WIDTH SLIDER SECTION */
        .full-width-slider-section {
            width: 100%;
            background: var(--white);
            box-shadow: 0 4px 15px rgba(0,0,0,0.05);
            margin-bottom: 25px;
        }
        .full-width-slider-section .swiper {
            width: 100%;
            height: 420px;
        }
        .full-width-slider-section .swiper-slide img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        /* MAIN WRAPPER CONTAINER FOR REST OF THE CONTENT */
        .page-content-wrapper {
            width: min(94%, 1420px);
            margin: 0 auto 40px auto;
        }

        /* DESTINATIONS GRID */
        .section-heading {
            font-size: 20px;
            font-weight: 700;
            color: var(--dark-green);
            margin-bottom: 15px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .destination-card {
            background: var(--white);
            border-radius: 12px;
            overflow: hidden;
            border: 1px solid var(--border);
            box-shadow: 0 4px 12px rgba(0,0,0,0.03);
            cursor: pointer;
            transition: transform 0.2s ease, box-shadow 0.2s ease;
            height: 100%;
            display: flex;
            flex-direction: column;
        }
        .destination-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 20px rgba(0,0,0,0.08);
        }
        .destination-img {
            width: 100%;
            height: 190px;
            object-fit: cover;
        }
        .destination-body {
            padding: 12px;
            text-align: center;
            flex-grow: 1;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }
        .destination-title {
            font-size: 15px;
            font-weight: 700;
            color: var(--dark-green);
            margin-bottom: 6px;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        /* MOBILE RESPONSIVE OPTIMIZATIONS (BEST UI MODEL) */
        @media (max-width: 768px) {
            .full-width-slider-section .swiper { height: 240px; }
            .destination-img { height: 135px; }
            .destination-body { padding: 8px 10px; }
            .destination-title { font-size: 13px; margin-bottom: 4px; }
            .section-heading { font-size: 17px; margin-bottom: 12px; }
            .page-content-wrapper { width: 96%; }
        }

        @media (max-width: 576px) {
            .full-width-slider-section .swiper { height: 185px; }
            .destination-img { height: 120px; }
            .destination-title { font-size: 12px; }
            .destination-body .btn-outline-success {
                font-size: 10px !important;
                padding: 3px 6px;
            }
        }
    </style>
</head>
<body>

<!-- FULL WIDTH ADMIN SLIDER SECTION -->
<?php
$slider_query = "SELECT * FROM dropdown_media WHERE dropdown_option_id = $id AND media_type = 'image' ORDER BY id DESC";
$slider_result = mysqli_query($conn,$slider_query);

if ($slider_result && mysqli_num_rows($slider_result) > 0) {
?>
<div class="full-width-slider-section">
    <div class="swiper mySwiper">
        <div class="swiper-wrapper">
            <?php while($slide = mysqli_fetch_assoc($slider_result)) { 
                $slide_img =$slide['file_path'];
                if (strpos($slide_img, 'uploads/') === false) {$s_url = "/Chardham Yatra/uploads/slider_media/" . $slide_img;
                } else {
                    $s_url = "/Chardham Yatra/" . ltrim($slide_img, '/');
                }
            ?>
            <div class="swiper-slide">
                <img src="<?php echo htmlspecialchars($s_url); ?>" alt="Slider Image">
            </div>
            <?php } ?>
        </div>
        <div class="swiper-pagination"></div>
        <div class="swiper-button-next"></div>
        <div class="swiper-button-prev"></div>
    </div>
</div>
<?php } ?>

<div class="page-content-wrapper">

    <!-- MAPPED DESTINATIONS IMAGES GRID -->
    <?php
    $dest_query = "SELECT * FROM destinations WHERE dropdown_option_id = $id ORDER BY id DESC";
    $dest_result = mysqli_query($conn,$dest_query);

    if ($dest_result && mysqli_num_rows($dest_result) > 0) {
    ?>
    <div class="destinations-section">
        <div class="section-heading">
            <i class="fas fa-images text-success"></i> <?php echo htmlspecialchars($title); ?> Destination
        </div>
        
        <div class="row g-2 g-md-3">
            <?php
            while($dest = mysqli_fetch_assoc($dest_result)) {
                $db_img =$dest['image'];
                if (strpos($db_img, 'uploads/') === false) {$img_url = "/Chardham Yatra/uploads/destinations/" . $db_img;
                } else {
                    $img_url = "/Chardham Yatra/" . ltrim($db_img, '/');
                }
            ?>
            <!-- col-6 mobile par 2 images per row dikhayega -->
            <div class="col-6 col-md-6 col-lg-4 mb-2 mb-md-3">
                <div class="destination-card" data-bs-toggle="modal" data-bs-target="#inquiryModal" data-destination="<?php echo htmlspecialchars($dest['title'] ?? 'Destination'); ?>">
                    <img src="<?php echo htmlspecialchars($img_url); ?>" class="destination-img" alt="Destination Image">
                    <div class="destination-body">
                        <div>
                            <div class="rating mb-1" style="color: #ffc107; font-size: 11px;">
                                <?php 
                                $rating = isset($dest['rating']) ? intval($dest['rating']) : 5;
                                for($i=0; $i < $rating; $i++) { ?>
                                    <span class="fa fa-star"></span>
                                <?php } ?>
                            </div>
                            <h5 class="destination-title"><?php echo htmlspecialchars($dest['title'] ?? 'Explore Destination'); ?></h5>
                        </div>
                        <span class="btn btn-sm btn-outline-success mt-1 w-100" style="font-size: 11px;"><i class="fab fa-whatsapp"></i> Book / Inquiry</span>
                    </div>
                </div>
            </div>
            <?php } ?>
        </div>
    </div>
    <?php } ?>
</div>

<!-- WhatsApp Inquiry Modal Popup -->
<div class="modal fade" id="inquiryModal" tabindex="-1" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header bg-success text-white py-2">
        <h5 class="modal-title fs-6"><i class="fab fa-whatsapp"></i> Inquiry for <span id="modalDestTitle"></span></h5>
        <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body p-3">
        <form id="whatsappInquiryForm">
            <input type="hidden" id="destInput" name="destination">
            
            <div class="mb-2">
                <label class="form-label fw-bold" style="font-size: 12px;">Your Full Name</label>
                <input type="text" id="visitorName" class="form-control form-control-sm" placeholder="Enter your name" required>
            </div>

            <div class="mb-2">
                <label class="form-label fw-bold" style="font-size: 12px;">Phone Number</label>
                <input type="tel" id="visitorPhone" class="form-control form-control-sm" placeholder="Enter phone number" required>
            </div>

            <div class="mb-2">
                <label class="form-label fw-bold" style="font-size: 12px;">Preferred Travel Date</label>
                <input type="date" id="travelDate" class="form-control form-control-sm" required>
            </div>

            <div class="mb-2">
                <label class="form-label fw-bold" style="font-size: 12px;">Number of Persons</label>
                <input type="number" id="personCount" class="form-control form-control-sm" min="1" value="2" required>
            </div>

            <div class="mb-3">
                <label class="form-label fw-bold" style="font-size: 12px;">Custom Requirements / Message</label>
                <textarea id="visitorMessage" class="form-control form-control-sm" placeholder="Mention any specific requirement..." style="height: 60px;"></textarea>
            </div>

            <button type="button" onclick="sendToWhatsApp()" class="btn btn-success w-100 py-2 fw-bold" style="font-size: 14px;">
                <i class="fab fa-whatsapp"></i> Send Inquiry on WhatsApp
            </button>
        </form>
      </div>
    </div>
  </div>
</div>

<!-- Bootstrap & Swiper JS Bundle -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.js"></script>
<script>
var swiper = new Swiper(".mySwiper", {
    loop: true,
    autoplay: {
        delay: 3000,
        disableOnInteraction: false,
    },
    pagination: {
        el: ".swiper-pagination",
        clickable: true,
    },
    navigation: {
        nextEl: ".swiper-button-next",
        prevEl: ".swiper-button-prev",
    },
});

var inquiryModal = document.getElementById('inquiryModal');
inquiryModal.addEventListener('show.bs.modal', function (event) {
  var button = event.relatedTarget;
  var destination = button.getAttribute('data-destination');
  document.getElementById('modalDestTitle').textContent = destination;
  document.getElementById('destInput').value = destination;
});

function sendToWhatsApp() {
    const name = document.getElementById('visitorName').value.trim();
    const phone = document.getElementById('visitorPhone').value.trim();
    const date = document.getElementById('travelDate').value;
    const persons = document.getElementById('personCount').value;
    const message = document.getElementById('visitorMessage').value.trim();
    const destinationName = document.getElementById('destInput').value;
    const packageTitle = "<?php echo addslashes($title); ?>";

    if (!name || !phone || !date) {
        alert('Please fill in all mandatory fields (Name, Phone, and Date).');
        return;
    }

    let text = `*New Destination Inquiry - Wings Tour & Travels*%0A`;
    text += `-----------------------------------%0A`;
    text += `*Package / Category:* ${packageTitle}%0A`;
    text += `*Destination:* ${destinationName}%0A`;
    text += `*Name:* ${name}%0A`;
    text += `*Phone:* ${phone}%0A`;
    text += `*Travel Date:* ${date}%0A`;
    text += `*Persons:* ${persons}%0A`;
    if(message) {
        text += `*Message:* ${message}%0A`;
    }
    text += `-----------------------------------`;

    const whatsappNumber = "917428465992";
    const whatsappUrl = `https://wa.me/${whatsappNumber}?text=${text}`;
    window.open(whatsappUrl, '_blank');
}
</script>

</body>
</html>
<?php
require_once __DIR__ . '/footer.php';
?>