<?php
$pageTitle = 'Home';
require_once 'includes/header.php';

$sliders = getActiveSliders();
$featuredPlaces = getFeaturedPlaces(10);
$featuredPackages = getFeaturedPackages(6);

// Count packages per place
$db = getDBConnection();
$placePackageCounts = [];
$stmt = $db->query("SELECT place_id, COUNT(*) as count FROM packages WHERE status = 'active' GROUP BY place_id");
while ($row = $stmt->fetch()) {
    $placePackageCounts[$row['place_id']] = $row['count'];
}
?>

<!-- Hero Slider -->
<section class="hero-slider">
    <?php if (!empty($sliders)): ?>
        <?php foreach ($sliders as $index => $slider): ?>
            <div class="hero-slide <?= $index === 0 ? 'active' : '' ?>">
                <img src="<?= getImageUrl($slider['image'], 'sliders') ?>" alt="<?= sanitize($slider['title']) ?>">
                <div class="hero-overlay"></div>
                <div class="hero-content">
                    <h1><?= sanitize($slider['title']) ?></h1>
                    <p><?= sanitize($slider['subtitle']) ?></p>
                    <a href="<?= $slider['link'] ?: BASE_URL . 'packages.php' ?>" class="btn btn-primary btn-lg">
                        <i class="fas fa-compass"></i> Explore Packages
                    </a>
                </div>
            </div>
        <?php endforeach; ?>
    <?php else: ?>
        <div class="hero-slide active">
            <img src="<?= getPlaceholderImage('slider-1') ?>" alt="Explore the World">
            <div class="hero-overlay"></div>
            <div class="hero-content">
                <h1>Explore the World</h1>
                <p>Discover amazing destinations and create unforgettable memories</p>
                <a href="<?= BASE_URL ?>packages.php" class="btn btn-primary btn-lg">
                    <i class="fas fa-compass"></i> Explore Packages
                </a>
            </div>
        </div>
    <?php endif; ?>
    
    <button class="slider-arrow prev"><i class="fas fa-chevron-left"></i></button>
    <button class="slider-arrow next"><i class="fas fa-chevron-right"></i></button>
    
    <div class="slider-controls">
        <?php for ($i = 0; $i < max(count($sliders), 1); $i++): ?>
            <button class="slider-dot <?= $i === 0 ? 'active' : '' ?>"></button>
        <?php endfor; ?>
    </div>
</section>

<!-- Search Section -->
<section class="search-section">
    <form class="search-box" action="<?= BASE_URL ?>search.php" method="GET">
        <div class="search-input-group">
            <i class="fas fa-search"></i>
            <input type="text" name="search" placeholder="Search destinations, packages..." autocomplete="off">
        </div>
        <div class="search-input-group">
            <i class="fas fa-map-marker-alt"></i>
            <select name="place">
                <option value="">All Destinations</option>
                <?php foreach ($featuredPlaces as $place): ?>
                    <option value="<?= $place['id'] ?>"><?= sanitize($place['name']) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <button type="submit" class="btn btn-primary">
            <i class="fas fa-search"></i> Search
        </button>
    </form>
</section>

<!-- Featured Places Section -->
<section class="places-section">
    <div class="container">
        <div class="section-title">
            <h2>Popular Destinations</h2>
            <p>Explore the most sought-after travel destinations handpicked for you</p>
        </div>
        
        <div class="places-grid">
            <?php foreach ($featuredPlaces as $place): ?>
                <a href="<?= BASE_URL ?>place.php?slug=<?= $place['slug'] ?>" class="place-card">
                    <img src="<?= getImageUrl($place['image'], 'places') ?>" alt="<?= sanitize($place['name']) ?>" loading="lazy">
                    <div class="place-overlay">
                        <div class="place-name"><?= sanitize($place['name']) ?></div>
                        <div class="place-packages">
                            <?= $placePackageCounts[$place['id']] ?? 0 ?> Tour Packages
                        </div>
                    </div>
                    <?php if ($place['is_featured']): ?>
                        <span class="place-badge"><i class="fas fa-star"></i> Featured</span>
                    <?php endif; ?>
                </a>
            <?php endforeach; ?>
        </div>
    </div>
</section>

<!-- Featured Packages Section -->
<section class="packages-section" id="packages">
    <div class="container">
        <div class="section-title">
            <h2>Featured Tour Packages</h2>
            <p>Handcrafted travel experiences designed to create lasting memories</p>
        </div>
        
        <div class="packages-grid">
            <?php foreach ($featuredPackages as $pkg): ?>
                <div class="package-card">
                    <a href="<?= BASE_URL ?>package.php?slug=<?= $pkg['slug'] ?>" class="card-image">
                        <img src="<?= getImageUrl($pkg['primary_image'], 'packages') ?>" alt="<?= sanitize($pkg['title']) ?>" loading="lazy">
                        <div class="card-badges">
                            <?php if ($pkg['is_featured']): ?>
                                <span class="badge badge-featured"><i class="fas fa-star"></i> Featured</span>
                            <?php endif; ?>
                            <?php if ($pkg['offer_price'] && $pkg['offer_price'] < $pkg['price_per_person']): ?>
                                <span class="badge badge-discount"><?= calculateDiscount($pkg['price_per_person'], $pkg['offer_price']) ?>% OFF</span>
                            <?php endif; ?>
                            <span class="badge badge-duration"><i class="fas fa-clock"></i> <?= $pkg['duration_days'] ?>D/<?= $pkg['duration_nights'] ?>N</span>
                        </div>
                    </a>
                    
                    <div class="card-body">
                        <div class="card-location">
                            <i class="fas fa-map-marker-alt"></i>
                            <?= sanitize($pkg['place_name']) ?>
                        </div>
                        <a href="<?= BASE_URL ?>package.php?slug=<?= $pkg['slug'] ?>" class="card-title">
                            <?= sanitize($pkg['title']) ?>
                        </a>
                        <div class="card-meta">
                            <span><i class="fas fa-calendar-alt"></i> <?= $pkg['duration_days'] ?> Days</span>
                            <span><i class="fas fa-users"></i> Min <?= $pkg['min_people'] ?> People</span>
                            <?php if ($pkg['start_date']): ?>
                                <span><i class="fas fa-plane-departure"></i> <?= formatDate($pkg['start_date'], 'M Y') ?></span>
                            <?php endif; ?>
                        </div>
                        <div class="card-footer">
                            <div class="card-price">
                                <?php if ($pkg['offer_price'] && $pkg['offer_price'] < $pkg['price_per_person']): ?>
                                    <div class="price-container">
                                        <span class="original-price"><?= formatCurrency($pkg['price_per_person']) ?></span>
                                        <span class="offer-price"><?= formatCurrency($pkg['offer_price']) ?> <small class="per-person">/ person</small></span>
                                        <span class="discount-badge"><?= calculateDiscount($pkg['price_per_person'], $pkg['offer_price']) ?>% OFF</span>
                                    </div>
                                <?php else: ?>
                                    <span class="current-price"><?= formatCurrency($pkg['price_per_person']) ?> <small class="per-person">/ person</small></span>
                                <?php endif; ?>
                            </div>
                            <a href="<?= BASE_URL ?>package.php?slug=<?= $pkg['slug'] ?>" class="btn btn-sm btn-primary">
                                View Details
                            </a>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
        
        <div class="text-center mt-4">
            <a href="<?= BASE_URL ?>packages.php" class="btn btn-outline btn-lg">
                <i class="fas fa-th-large"></i> View All Packages
            </a>
        </div>
    </div>
</section>

<!-- Why Choose Us Section -->
<section class="why-us-section">
    <div class="container">
        <div class="section-title">
            <h2 style="color:#fff;">Why Choose Us?</h2>
            <p style="color:rgba(255,255,255,0.8);">We go above and beyond to make your travel experience exceptional</p>
        </div>
        
        <div class="why-us-grid">
            <div class="why-us-card">
                <div class="icon-box">
                    <i class="fas fa-shield-alt"></i>
                </div>
                <h4>100% Trusted</h4>
                <p>We are a trusted travel partner with thousands of happy customers and verified reviews.</p>
            </div>
            <div class="why-us-card">
                <div class="icon-box">
                    <i class="fas fa-tags"></i>
                </div>
                <h4>Best Price Guarantee</h4>
                <p>Get the best deals and competitive prices on all our tour packages with no hidden charges.</p>
            </div>
            <div class="why-us-card">
                <div class="icon-box">
                    <i class="fas fa-headset"></i>
                </div>
                <h4>24/7 Support</h4>
                <p>Our dedicated support team is available round the clock to assist you before, during, and after your trip.</p>
            </div>
            <div class="why-us-card">
                <div class="icon-box">
                    <i class="fas fa-map-marked-alt"></i>
                </div>
                <h4>Handpicked Hotels</h4>
                <p>We carefully select the best hotels and accommodations to ensure your comfort and satisfaction.</p>
            </div>
        </div>
    </div>
</section>

<!-- Testimonials Section -->
<section class="testimonials-section">
    <div class="container">
        <div class="section-title">
            <h2>What Our Travelers Say</h2>
            <p>Real experiences from real travelers who explored the world with us</p>
        </div>
        
        <div class="testimonials-grid">
            <div class="testimonial-card">
                <div class="stars">
                    <i class="fas fa-star"></i>
                    <i class="fas fa-star"></i>
                    <i class="fas fa-star"></i>
                    <i class="fas fa-star"></i>
                    <i class="fas fa-star"></i>
                </div>
                <p class="quote">"The Goa package was absolutely amazing! Everything was well organized from hotel to sightseeing. The team was very responsive and helpful throughout the trip."</p>
                <div class="author">
                    <img src="https://ui-avatars.com/api/?name=Rahul+Sharma&background=FF6B35&color=fff" alt="Rahul Sharma">
                    <div class="author-info">
                        <div class="name">Rahul Sharma</div>
                        <div class="location">Bangalore, India</div>
                    </div>
                </div>
            </div>
            
            <div class="testimonial-card">
                <div class="stars">
                    <i class="fas fa-star"></i>
                    <i class="fas fa-star"></i>
                    <i class="fas fa-star"></i>
                    <i class="fas fa-star"></i>
                    <i class="fas fa-star"></i>
                </div>
                <p class="quote">"Kerala backwater experience was magical! The houseboat stay was the highlight of our trip. Highly recommend Traveler for anyone looking for a hassle-free vacation."</p>
                <div class="author">
                    <img src="https://ui-avatars.com/api/?name=Priya+Patel&background=004E89&color=fff" alt="Priya Patel">
                    <div class="author-info">
                        <div class="name">Priya Patel</div>
                        <div class="location">Kolkata, India</div>
                    </div>
                </div>
            </div>
            
            <div class="testimonial-card">
                <div class="stars">
                    <i class="fas fa-star"></i>
                    <i class="fas fa-star"></i>
                    <i class="fas fa-star"></i>
                    <i class="fas fa-star"></i>
                    <i class="fas fa-star-half-alt"></i>
                </div>
                <p class="quote">"The Ladakh expedition was a life-changing experience! The itinerary was perfectly planned considering altitude acclimatization. Will definitely book again!"</p>
                <div class="author">
                    <img src="https://ui-avatars.com/api/?name=Amit+Kumar&background=10B981&color=fff" alt="Amit Kumar">
                    <div class="author-info">
                        <div class="name">Amit Kumar</div>
                        <div class="location">New Delhi, India</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<!-- Newsletter Section -->
<section class="newsletter-section">
    <div class="container">
        <div class="newsletter-box">
            <h3><i class="fas fa-envelope-open-text text-primary"></i> Subscribe to Our Newsletter</h3>
            <p>Get the latest travel deals, destination guides, and exclusive offers delivered to your inbox.</p>
            <form class="newsletter-form" onsubmit="event.preventDefault(); showToast('Thank you for subscribing!', 'success');">
                <input type="email" placeholder="Enter your email address" required>
                <button type="submit" class="btn btn-primary">Subscribe</button>
            </form>
        </div>
    </div>
</section>

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