<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
include 'includes/header.php'; 
include 'includes/navbar.php'; 
?>

<!-- Premium Gallery Hero -->
<div class="gallery-hero" style="min-height: 60vh; background: linear-gradient(rgba(0,0,0,0.6), rgba(0,0,0,0.6)), url('https://images.pexels.com/photos/1450353/pexels-photo-1450353.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1'); background-size: cover; background-position: center; display: flex; align-items: center; justify-content: center; text-align: center; color: #fff; padding-top: 100px; padding-bottom: 150px;">
    <div class="container">
        <h6 class="reveal-up gallery-subtitle" style="letter-spacing: 5px; text-transform: uppercase; color: var(--primary); font-size: 18px;">Memories</h6>
        <h1 class="fw-bold reveal-up gallery-title" style="letter-spacing: -2px; font-size: 80px;">TRAVEL GALLERY</h1>
        <p class="lead reveal-up gallery-text" style="max-width: 600px; margin: 20px auto 0;">Glimpses of unforgettable journeys across the globe. Witness the beauty of nature and the richness of culture.</p>
    </div>
</div>

<section class="py-5" style="background: #fdfcfb;">
    <div class="container">
        <!-- Gallery Grid -->
        <div class="row g-4" id="gallery-grid">
            <?php
            $gallery_res = $conn->query("SELECT * FROM gallery ORDER BY id DESC");
            $has_db_images = ($gallery_res && $gallery_res->num_rows > 0);
            
            if ($has_db_images):
                while($img = $gallery_res->fetch_assoc()):
            ?>
            <div class="col-md-6 reveal-up">
                <div class="gallery-item" onclick="openLightbox('uploads/<?= $img['image'] ?>')">
                    <img src="uploads/<?= $img['image'] ?>" alt="<?= htmlspecialchars($img['title']) ?>">
                    <div class="gallery-overlay">
                        <span class="gallery-cat">Gallery</span>
                        <h4 class="gallery-item-title"><?= htmlspecialchars($img['title'] ?: 'Travel Moment') ?></h4>
                        <div class="gallery-icon">
                            <i class="fas fa-search-plus"></i>
                        </div>
                    </div>
                </div>
            </div>
            <?php 
                endwhile;
            endif;

            // Always show some fallback/additional images if requested or if DB is empty
            if (!$has_db_images):
                $gallery_images = [
                    ['url' => 'https://images.pexels.com/photos/2161449/pexels-photo-2161449.jpeg', 'title' => 'Golden Temple, India', 'cat' => 'Cultural'],
                    ['url' => 'https://images.pexels.com/photos/358457/pexels-photo-358457.jpeg', 'title' => 'Tropical Paradise', 'cat' => 'Nature'],
                    ['url' => 'https://images.pexels.com/photos/163168/berlin-brandenburg-gate-brandenburger-tor-germany-163168.jpeg', 'title' => 'Berlin Nights', 'cat' => 'City'],
                    ['url' => 'https://images.pexels.com/photos/338504/pexels-photo-338504.jpeg', 'title' => 'Coastal Escape', 'cat' => 'Nature'],
                    ['url' => 'https://images.pexels.com/photos/2082103/pexels-photo-2082103.jpeg', 'title' => 'Ancient Wonders', 'cat' => 'History'],
                    ['url' => 'https://images.pexels.com/photos/1450353/pexels-photo-1450353.jpeg', 'title' => 'Maldives Blue', 'cat' => 'Nature'],
                ];

                foreach($gallery_images as $img):
            ?>
            <div class="col-md-6 reveal-up">
                <div class="gallery-item" onclick="openLightbox('<?= $img['url'] ?>')">
                    <img src="<?= $img['url'] ?>?auto=compress&cs=tinysrgb&w=600" alt="<?= $img['title'] ?>">
                    <div class="gallery-overlay">
                        <span class="gallery-cat"><?= $img['cat'] ?></span>
                        <h4 class="gallery-item-title"><?= $img['title'] ?></h4>
                        <div class="gallery-icon">
                            <i class="fas fa-search-plus"></i>
                        </div>
                    </div>
                </div>
            </div>
            <?php 
                endforeach;
            endif;
            ?>
        </div>
    </div>
</section>

<!-- Lightbox Modal -->
<div id="gallery-lightbox" class="lightbox" onclick="this.style.display='none'">
    <span class="close-lightbox">&times;</span>
    <img id="lightbox-img" src="" alt="Zoomed Image">
</div>

<style>
.gallery-title { font-size: 80px; }
.gallery-item {
    position: relative;
    border-radius: 30px;
    overflow: hidden;
    height: 500px;
    cursor: pointer;
    box-shadow: 0 15px 35px rgba(0,0,0,0.1);
    transition: 0.5s;
}
.gallery-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: 0.8s;
}
.gallery-item:hover img {
    transform: scale(1.1);
}
.gallery-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(to top, rgba(0,0,0,0.8), transparent);
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    padding: 40px;
    opacity: 0;
    transition: 0.4s;
}
.gallery-item:hover .gallery-overlay {
    opacity: 1;
}
.gallery-cat {
    color: var(--primary);
    text-transform: uppercase;
    font-weight: 700;
    font-size: 12px;
    letter-spacing: 2px;
    margin-bottom: 10px;
}
.gallery-item-title {
    color: #fff;
    font-weight: 700;
    margin: 0;
}
.gallery-icon {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0.5);
    width: 60px;
    height: 60px;
    background: var(--primary);
    color: #fff;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    opacity: 0;
    transition: 0.4s;
}
.gallery-item:hover .gallery-icon {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
}

/* Lightbox Styles */
.lightbox {
    display: none;
    position: fixed;
    z-index: 9999;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.9);
    align-items: center;
    justify-content: center;
    padding: 40px;
}
.lightbox img {
    max-width: 90%;
    max-height: 90%;
    border-radius: 10px;
    box-shadow: 0 0 50px rgba(0,0,0,0.5);
}
.close-lightbox {
    position: absolute;
    top: 30px;
    right: 50px;
    color: #fff;
    font-size: 60px;
    cursor: pointer;
}

@media (max-width: 768px) {
    .gallery-hero {
        margin-top: 180px !important;
        padding-top: 50px !important;
        padding-bottom: 80px !important;
        min-height: auto !important;
    }
    .gallery-title { 
        font-size: 36px !important; 
        line-height: 1.2;
    }
    .gallery-subtitle { 
        font-size: 14px !important; 
        letter-spacing: 3px !important;
    }
    .gallery-text {
        font-size: 16px !important;
    }
    .gallery-item { height: 300px; }
}

/* Visibility Fix */
.reveal-up {
    opacity: 1 !important;
    transform: none !important;
    visibility: visible !important;
}
</style>

<script>
function openLightbox(url) {
    document.getElementById('lightbox-img').src = url;
    document.getElementById('gallery-lightbox').style.display = 'flex';
}
</script>

<?php include 'includes/footer.php'; ?>
