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

$page = max(1, intval($_GET['page'] ?? 1));
$perPage = 9;
$offset = ($page - 1) * $perPage;

$db = getDBConnection();

// Get total count
$stmt = $db->query("SELECT COUNT(*) as total FROM packages p JOIN places pl ON p.place_id = pl.id WHERE p.status = 'active' AND pl.status = 'active'");
$total = $stmt->fetch()['total'];
$totalPages = ceil($total / $perPage);

$packages = getActivePackages($perPage, $offset);
$places = getFeaturedPlaces(10);
?>

<!-- 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;">Tour Packages</h1>
        <div class="breadcrumb" style="justify-content:center;">
            <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>
            <span style="color:#fff;">Tour Packages</span>
        </div>
    </div>
</div>

<section style="padding:60px 0;">
    <div class="container">
        <!-- Filter Bar -->
        <div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:30px;flex-wrap:wrap;gap:15px;">
            <p style="color:var(--gray);">Showing <?= count($packages) ?> of <?= $total ?> packages</p>
            <div style="display:flex;gap:10px;flex-wrap:wrap;">
                <a href="<?= BASE_URL ?>packages.php" class="btn btn-sm <?= !isset($_GET['place']) ? 'btn-primary' : 'btn-outline' ?>">All</a>
                <?php foreach ($places as $place): ?>
                    <a href="<?= BASE_URL ?>packages.php?place=<?= $place['id'] ?>" class="btn btn-sm <?= ($_GET['place'] ?? '') == $place['id'] ? 'btn-primary' : 'btn-outline' ?>">
                        <?= sanitize($place['name']) ?>
                    </a>
                <?php endforeach; ?>
            </div>
        </div>

        <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['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'] ?></span>
                        </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>

        <?php if (empty($packages)): ?>
            <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 found</h3>
                <p style="color:var(--gray-light);">Try adjusting your filters or check back later.</p>
            </div>
        <?php endif; ?>

        <!-- Pagination -->
        <?php if ($totalPages > 1): ?>
            <div class="pagination">
                <?php if ($page > 1): ?>
                    <a href="?page=<?= $page - 1 ?>"><i class="fas fa-chevron-left"></i></a>
                <?php endif; ?>
                
                <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                    <?php if ($i == $page): ?>
                        <span class="active"><?= $i ?></span>
                    <?php else: ?>
                        <a href="?page=<?= $i ?>"><?= $i ?></a>
                    <?php endif; ?>
                <?php endfor; ?>
                
                <?php if ($page < $totalPages): ?>
                    <a href="?page=<?= $page + 1 ?>"><i class="fas fa-chevron-right"></i></a>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    </div>
</section>

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