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

$query = sanitize($_GET['search'] ?? '');
$placeFilter = intval($_GET['place'] ?? 0);

$results = [];
if (!empty($query) || $placeFilter > 0) {
    $db = getDBConnection();
    
    if ($placeFilter > 0 && !empty($query)) {
        $searchTerm = '%' . $query . '%';
        $stmt = $db->prepare("
            SELECT p.*, pl.name as place_name, pl.slug as place_slug,
            (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 
            JOIN places pl ON p.place_id = pl.id 
            WHERE p.status = 'active' AND pl.status = 'active'
            AND p.place_id = ?
            AND (p.title LIKE ? OR p.description LIKE ? OR pl.name LIKE ?)
            ORDER BY p.is_featured DESC, p.created_at DESC
        ");
        $stmt->execute([$placeFilter, $searchTerm, $searchTerm, $searchTerm]);
    } elseif ($placeFilter > 0) {
        $stmt = $db->prepare("
            SELECT p.*, pl.name as place_name, pl.slug as place_slug,
            (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 
            JOIN places pl ON p.place_id = pl.id 
            WHERE p.status = 'active' AND pl.status = 'active' AND p.place_id = ?
            ORDER BY p.is_featured DESC, p.created_at DESC
        ");
        $stmt->execute([$placeFilter]);
    } else {
        $results = searchPackages($query);
    }
    
    if (isset($stmt)) {
        $results = $stmt->fetchAll();
    }
}
?>

<!-- Page Header -->
<div style="margin-top:75px;background:linear-gradient(135deg,var(--secondary),var(--secondary-dark));padding:60px 0;text-align:center;">
    <div class="container">
        <h1 style="color:#fff;font-size:2.5rem;margin-bottom:10px;">Search Results</h1>
        <p style="color:rgba(255,255,255,0.8);font-size:1.1rem;">
            <?php if ($query): ?>
                Showing results for "<strong><?= $query ?></strong>"
            <?php else: ?>
                Browse our tour packages
            <?php endif; ?>
        </p>
    </div>
</div>

<!-- Search Bar -->
<section class="search-section" style="margin-top:-30px;">
    <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..." value="<?= $query ?>">
        </div>
        <button type="submit" class="btn btn-primary">
            <i class="fas fa-search"></i> Search
        </button>
    </form>
</section>

<section style="padding:60px 0;">
    <div class="container">
        <p style="color:var(--gray);margin-bottom:25px;"><?= count($results) ?> package(s) found</p>
        
        <?php if (!empty($results)): ?>
            <div class="packages-grid">
                <?php foreach ($results 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">
                            <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'] ?></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-search" style="font-size:3rem;color:var(--gray-light);margin-bottom:15px;"></i>
                <h3 style="color:var(--gray);">No packages found</h3>
                <p style="color:var(--gray-light);margin-bottom:20px;">Try different keywords or browse all packages.</p>
                <a href="<?= BASE_URL ?>packages.php" class="btn btn-primary">Browse All Packages</a>
            </div>
        <?php endif; ?>
    </div>
</section>

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