<?php
$pageTitle = 'Browse Cities - Weather Portal';
$pageDescription = 'Explore weather forecasts for 59+ cities across 13 countries. Find detailed weather information for any location.';

$citiesFile = __DIR__ . '/data/cities.json';
$cities = file_exists($citiesFile) ? json_decode(file_get_contents($citiesFile), true) : [];

$countries = [];
if ($cities) {
    foreach ($cities as $city) {
        $country = $city['country'] ?? 'Other';
        if (!isset($countries[$country])) {
            $countries[$country] = [];
        }
        $countries[$country][] = $city;
    }
    ksort($countries);
}

include 'header.php';
?>

<div class="page-header">
    <h1 class="page-title">Browse Cities</h1>
    <p class="page-subtitle">Weather forecasts for 59+ cities across 13 countries</p>
</div>

<div class="cities-search-section">
    <div class="cities-search-container">
        <input type="text" id="citySearchInput" class="cities-search-input" placeholder="Search for a city...">
        <span class="search-icon">&#128269;</span>
    </div>
</div>

<div class="cities-page-content">
    <?php foreach ($countries as $country => $countryCities): ?>
    <div class="country-section" data-country="<?php echo htmlspecialchars($country); ?>">
        <h2 class="country-title">
            <span class="country-flag"><?php echo getCountryFlag($country); ?></span>
            <?php echo htmlspecialchars($country); ?>
            <span class="city-count">(<?php echo count($countryCities); ?> cities)</span>
        </h2>
        <div class="cities-grid">
            <?php foreach ($countryCities as $city): ?>
            <?php $citySlug = strtolower(str_replace([' ', "'"], ['-', ''], $city['city'])); ?>
            <a href="/weather/<?php echo urlencode($citySlug); ?>" class="city-card" data-city="<?php echo htmlspecialchars(strtolower($city['city'])); ?>">
                <div class="city-name"><?php echo htmlspecialchars($city['city']); ?></div>
                <div class="city-country"><?php echo htmlspecialchars($city['country']); ?></div>
            </a>
            <?php endforeach; ?>
        </div>
    </div>
    <?php endforeach; ?>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const searchInput = document.getElementById('citySearchInput');
    const cityCards = document.querySelectorAll('.city-card');
    const countrySections = document.querySelectorAll('.country-section');
    
    searchInput.addEventListener('input', function() {
        const query = this.value.toLowerCase().trim();
        
        cityCards.forEach(card => {
            const cityName = card.dataset.city;
            if (query === '' || cityName.includes(query)) {
                card.style.display = '';
            } else {
                card.style.display = 'none';
            }
        });
        
        countrySections.forEach(section => {
            const visibleCards = section.querySelectorAll('.city-card:not([style*="display: none"])');
            if (visibleCards.length === 0) {
                section.style.display = 'none';
            } else {
                section.style.display = '';
            }
        });
    });
});
</script>

<?php
function getCountryFlag($country) {
    $flags = [
        'Pakistan' => '🇵🇰',
        'UAE' => '🇦🇪',
        'Saudi Arabia' => '🇸🇦',
        'Turkey' => '🇹🇷',
        'Egypt' => '🇪🇬',
        'Indonesia' => '🇮🇩',
        'Malaysia' => '🇲🇾',
        'Bangladesh' => '🇧🇩',
        'USA' => '🇺🇸',
        'UK' => '🇬🇧',
        'France' => '🇫🇷',
        'India' => '🇮🇳',
        'China' => '🇨🇳',
        'Italy' => '🇮🇹',
        'Japan' => '🇯🇵',
        'Germany' => '🇩🇪',
        'Spain' => '🇪🇸',
        'Australia' => '🇦🇺',
        'Canada' => '🇨🇦',
        'Brazil' => '🇧🇷',
    ];
    return $flags[$country] ?? '🌍';
}

include 'footer.php';
?>
