<?php
require_once 'includes/functions.php';

$slug = $_GET['slug'] ?? '';
if (empty($slug)) redirect(BASE_URL . 'places.php');

$db = getDBConnection();
$stmt = $db->prepare("SELECT * FROM places WHERE slug = ? AND status = 'active'");
$stmt->execute([$slug]);
$place = $stmt->fetch();

if (!$place) redirect(BASE_URL . 'places.php');

$pageTitle = $place['name'];

// Get packages for this place
$stmt = $db->prepare("
    SELECT p.*, 
    (SELECT pi.image FROM package_images pi WHERE pi.package_id = p.id AND pi.is_primary = 1 LIMIT 1) as primary_image
    FROM packages p WHERE p.place_id = ? AND p.status = 'active' ORDER BY p.is_featured DESC, p.created_at DESC
");
$stmt->execute([$place['id']]);
$packages = $stmt->fetchAll();

require_once 'includes/header.php';
?>

<div class="place-hero" style="margin-top:75px;position:relative;height:400px;overflow:hidden;">
    <img src="<?= getImageUrl($place['image'], 'places') ?>" alt="<?= sanitize($place['name']) ?>" style="width:100%;height:100%;object-fit:cover;">
    <div style="position:absolute;top:0;left:0;width:100%;height:100%;background:linear-gradient(transparent 30%,rgba(0,0,0,0.7));"></div>
    <div style="position:absolute;bottom:40px;left:0;right:0;">
        <div class="container">
            <div class="breadcrumb" style="margin-bottom:10px;">
                <a href="<?= BASE_URL ?>" style="color:rgba(255,255,255,0.8);">Home</a>
                <span class="separator" style="color:rgba(255,255,255,0.5);"><i class="fas fa-chevron-right"></i></span>
                <a href="<?= BASE_URL ?>places.php" style="color:rgba(255,255,255,0.8);">Destinations</a>
                <span class="separator" style="color:rgba(255,255,255,0.5);"><i class="fas fa-chevron-right"></i></span>
                <span style="color:#fff;"><?= sanitize($place['name']) ?></span>
            </div>
            <h1 style="color:#fff;font-size:2.5rem;"><?= sanitize($place['name']) ?></h1>
            <p style="color:rgba(255,255,255,0.8);max-width:700px;margin-top:10px;"><?= sanitize($place['description']) ?></p>
        </div>
    </div>
</div>

<section style="padding:60px 0;">
    <div class="container">
        <h2 style="margin-bottom:30px;">Tour Packages in <?= sanitize($place['name']) ?> <span style="font-size:1rem;color:var(--gray);font-weight:400;">(<?= count($packages) ?> packages)</span></h2>
        
        <?php if (!empty($packages)): ?>
            <div class="packages-grid">
                <?php foreach ($packages 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['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">
                            <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'] ?></span>
                            </div>
                            <div class="card-footer">
                                <div class="card-price">
                                    <?php if ($pkg['offer_price'] && $pkg['offer_price'] < $pkg['price_per_person']): ?>
                                        <span class="original-price"><?= formatCurrency($pkg['price_per_person']) ?></span>
                                        <span class="current-price"><?= formatCurrency($pkg['offer_price']) ?> <small class="per-person">/ person</small></span>
                                    <?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>
        <?php else: ?>
            <div style="text-align:center;padding:60px 0;">
                <i class="fas fa-suitcase-rolling" style="font-size:3rem;color:var(--gray-light);margin-bottom:15px;"></i>
                <h3 style="color:var(--gray);">No packages available yet</h3>
                <p style="color:var(--gray-light);">Check back soon for exciting tour packages!</p>
            </div>
        <?php endif; ?>
    </div>
</section>

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