<?php
require_once 'includes/auth.php';
require_once 'includes/seo.php';

$search = trim($_GET['search'] ?? '');
$city = trim($_GET['city'] ?? '');
$sort = $_GET['sort'] ?? 'rating';

$where = ["status='active'"];
$params = [];

if ($search) {
    $where[] = "(name LIKE ? OR city LIKE ? OR district LIKE ?)";
    $params[] = "%$search%";
    $params[] = "%$search%";
    $params[] = "%$search%";
}
if ($city) {
    $where[] = "city=?";
    $params[] = $city;
}

$order = match($sort) {
    'price_asc' => 'min_price ASC',
    'price_desc' => 'min_price DESC',
    'name' => 'name ASC',
    default => 'rating DESC'
};

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

$total = db()->selectOne(
    "SELECT COUNT(*) as c FROM hotels WHERE ".implode(' AND ',$where),
    $params
)['c'] ?? 0;

$hotels = db()->select(
    "SELECT h.*, (SELECT MIN(price_per_night) FROM room_types WHERE hotel_id=h.id) as min_price
     FROM hotels h WHERE ".implode(' AND ',$where)."
     ORDER BY $order LIMIT $per_page OFFSET $offset",
    $params
) ?? [];

$total_pages = ceil($total / $per_page);

$page_title = 'Pet Otelleri - Türkiye Geneli';
$meta_description = 'Türkiye\'nin 81 ilinde 165+ güvenilir pet otel. Köpek oteli, kedi pansiyonu online rezervasyon.';
$canonical_url = 'https://petpansiyon.com.tr/oteller';

include 'includes/header.php';
?>


<!-- Hero -->
<div class="hotels-hero">
    <div class="container">
        <h1 class="hotels-title">🐾 Türkiye'nin Pet Otelleri</h1>
        <p style="font-size:1.1rem; opacity:0.85;">81 ilde 165+ güvenilir pet otel</p>
        
        <form class="search-bar" method="GET">
            <input type="text" name="search" placeholder="Otel veya şehir ara..." 
                   value="<?php echo htmlspecialchars($search); ?>">
            <button type="submit">🔍 Ara</button>
        </form>
    </div>
</div>

<!-- Filter -->
<div class="filter-bar">
    <div class="container">
        <div class="filter-row">
            <div class="filter-left">
                <i class="bi bi-building" style="color:#FF6B6B;"></i>
                <?php echo $total; ?> otel bulundu
            </div>
            
            <form class="filter-right" method="GET">
                <?php if ($search): ?><input type="hidden" name="search" value="<?php echo htmlspecialchars($search); ?>"><?php endif; ?>
                
                <select name="city" class="filter-select" onchange="this.form.submit()">
                    <option value="">Tüm Şehirler</option>
                    <?php
                    $cities = db()->select("SELECT DISTINCT city FROM hotels WHERE status='active' ORDER BY city");
                    foreach($cities as $c):
                    ?>
                    <option value="<?php echo htmlspecialchars($c['city']); ?>" <?php echo $city===$c['city']?'selected':''; ?>>
                        <?php echo htmlspecialchars($c['city']); ?>
                    </option>
                    <?php endforeach; ?>
                </select>
                
                <select name="sort" class="filter-select" onchange="this.form.submit()">
                    <option value="rating" <?php echo $sort==='rating'?'selected':''; ?>>⭐ Puana Göre</option>
                    <option value="price_asc" <?php echo $sort==='price_asc'?'selected':''; ?>>💰 Ucuzdan Pahalıya</option>
                    <option value="price_desc" <?php echo $sort==='price_desc'?'selected':''; ?>>💰 Pahalıdan Ucuza</option>
                    <option value="name" <?php echo $sort==='name'?'selected':''; ?>>🔤 İsme Göre</option>
                </select>
            </form>
        </div>
    </div>
</div>

<!-- Hotels Grid -->
<div class="container">
    <?php if (empty($hotels)): ?>
    <div class="empty-state">
        <div style="font-size:5rem; margin-bottom:1.5rem;">🔍</div>
        <h3 style="color:#343a40; font-weight:700;">Otel bulunamadı</h3>
        <p>Arama kriterlerinizi değiştirip tekrar deneyin.</p>
        <a href="/oteller" style="display:inline-block; margin-top:1rem; background:linear-gradient(135deg,#FF6B6B,#FF8E53); color:white; padding:0.75rem 2rem; border-radius:50px; font-weight:700; text-decoration:none;">
            Tüm Otelleri Gör
        </a>
    </div>
    <?php else: ?>
    <div class="hotels-grid">
        <?php foreach($hotels as $h): ?>
        <a href="/<?php echo $h['slug']; ?>" style="text-decoration:none; color:inherit;">
            <div class="hotel-card">
                <div class="hotel-img">
                    🏠
                    <span class="hotel-rating-badge">⭐ <?php echo number_format($h['rating'],1); ?></span>
                </div>
                <div class="hotel-body">
                    <h5 class="hotel-name"><?php echo htmlspecialchars($h['name']); ?></h5>
                    <div class="hotel-loc">
                        <i class="bi bi-geo-alt-fill" style="color:#FF6B6B;"></i>
                        <?php echo htmlspecialchars($h['city']); ?>
                    </div>
                    <div class="hotel-footer">
                        <?php if ($h['min_price']): ?>
                        <div class="hotel-price">
                            <?php echo number_format($h['min_price'],0); ?>₺
                            <small style="font-size:0.7rem; color:#868e96; font-weight:400;">/gece</small>
                        </div>
                        <?php endif; ?>
                        <span class="btn-detail">Detay →</span>
                    </div>
                </div>
            </div>
        </a>
        <?php endforeach; ?>
    </div>
    
    <!-- Pagination -->
    <?php if ($total_pages > 1): ?>
    <div class="pagination">
        <?php if ($page > 1): ?>
        <a href="?page=<?php echo $page-1; ?>&<?php echo http_build_query(['search'=>$search,'city'=>$city,'sort'=>$sort]); ?>" class="page-btn">‹</a>
        <?php endif; ?>
        
        <?php for($i=max(1,$page-2); $i<=min($total_pages,$page+2); $i++): ?>
        <a href="?page=<?php echo $i; ?>&<?php echo http_build_query(['search'=>$search,'city'=>$city,'sort'=>$sort]); ?>" 
           class="page-btn <?php echo $i===$page?'active':''; ?>">
            <?php echo $i; ?>
        </a>
        <?php endfor; ?>
        
        <?php if ($page < $total_pages): ?>
        <a href="?page=<?php echo $page+1; ?>&<?php echo http_build_query(['search'=>$search,'city'=>$city,'sort'=>$sort]); ?>" class="page-btn">›</a>
        <?php endif; ?>
    </div>
    <?php endif; ?>
    <?php endif; ?>
</div>

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