<?php 
$pageTitle = "Our Services by City | Finggu Infotech";
$pageDescription = "Find Finggu Infotech services in your city. We provide web design, development, SEO, and digital marketing services across major Indian cities.";
$pageKeywords = "web design services, web development, SEO services, digital marketing, mumbai, delhi, bangalore, pune, chennai";

include 'includes/header.php'; 
require_once __DIR__ . '/includes/pdo.php';
include 'includes/url-helper.php';

// Get all cities and services
$cities = $pdo->query("SELECT * FROM cities ORDER BY name")->fetchAll();
$services = $pdo->query("SELECT * FROM services ORDER BY name")->fetchAll();

// Get selected filters
$selectedCity = $_GET['city'] ?? '';
$selectedService = $_GET['service'] ?? '';
?>

<!-- Hero Section -->
<section class="relative pt-32 pb-20 bg-gradient-to-r from-blue-600 to-blue-700 text-white overflow-hidden">
    <div class="container mx-auto px-4 relative z-10">
        <div class="text-center max-w-4xl mx-auto">
            <h1 class="text-4xl md:text-5xl lg:text-6xl font-bold leading-tight mb-6">
                Our Services by City
            </h1>
            <p class="text-xl text-blue-100 mb-8">
                Find professional digital services in your city. We serve clients across major Indian cities with expert solutions.
            </p>
        </div>
    </div>
</section>

<!-- Filter Section -->
<section class="py-8 bg-gray-50">
    <div class="container mx-auto px-4">
        <div class="max-w-4xl mx-auto">
            <form method="GET" class="bg-white rounded-lg shadow-md p-6" id="serviceSearchForm">
                <div class="grid md:grid-cols-3 gap-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Select City</label>
                        <select name="city" id="citySelect" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
                            <option value="">All Cities</option>
                            <?php foreach ($cities as $city): ?>
                                <option value="<?= $city['slug'] ?>" <?= $selectedCity === $city['slug'] ? 'selected' : '' ?>>
                                    <?= htmlspecialchars($city['name']) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Select Service</label>
                        <select name="service" id="serviceSelect" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
                            <option value="">All Services</option>
                            <?php foreach ($services as $service): ?>
                                <option value="<?= $service['slug'] ?>" <?= $selectedService === $service['slug'] ? 'selected' : '' ?>>
                                    <?= htmlspecialchars($service['name']) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="flex items-end">
                        <button type="submit" id="searchBtn" class="w-full bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-md transition-colors">
                            <i class="fas fa-search mr-2"></i>Find Services
                        </button>
                    </div>
                </div>
                
                <!-- Info message -->
                <div id="searchInfo" class="mt-4 p-3 bg-blue-50 border border-blue-200 rounded-md text-sm text-blue-700" style="display: none;">
                    <i class="fas fa-info-circle mr-2"></i>
                    <span id="searchInfoText"></span>
                </div>
            </form>
            
            <script>
            document.getElementById('serviceSearchForm').addEventListener('submit', function(e) {
                const city = document.getElementById('citySelect').value;
                const service = document.getElementById('serviceSelect').value;
                const btn = document.getElementById('searchBtn');
                const info = document.getElementById('searchInfo');
                const infoText = document.getElementById('searchInfoText');
                
                // Show loading state
                btn.innerHTML = '<i class="fas fa-spinner fa-spin mr-2"></i>Searching...';
                btn.disabled = true;
                
                // Show appropriate message
                if (city && service) {
                    infoText.textContent = 'Redirecting to specific service page...';
                    info.style.display = 'block';
                } else if (city) {
                    infoText.textContent = 'Showing all services in selected city...';
                    info.style.display = 'block';
                } else if (service) {
                    infoText.textContent = 'Showing selected service in all cities...';
                    info.style.display = 'block';
                }
                
                // Allow form to submit normally
                return true;
            });
            </script>
        </div>
    </div>
</section>

<!-- Services Grid -->
<section class="py-16 bg-white">
    <div class="container mx-auto px-4">
        <div class="max-w-6xl mx-auto">
            <?php if ($selectedCity && $selectedService): ?>
                <!-- Single Service in Single City -->
                <?php
                $stmt = $pdo->prepare("
                    SELECT cs.*, c.name as city_name, s.name as service_name
                    FROM city_service cs
                    JOIN cities c ON cs.city_id = c.id
                    JOIN services s ON cs.service_id = s.id
                    WHERE c.slug = ? AND s.slug = ?
                ");
                $stmt->execute([$selectedCity, $selectedService]);
                $result = $stmt->fetch();
                
                if ($result): ?>
                    <div class="text-center mb-8">
                        <h2 class="text-3xl font-bold mb-4"><?= htmlspecialchars($result['service_name']) ?> in <?= htmlspecialchars($result['city_name']) ?></h2>
                    </div>
                    <div class="bg-gray-50 rounded-xl p-8 text-center">
                        <div class="max-w-3xl mx-auto">
                            <p class="text-lg text-gray-700 mb-6"><?= nl2br(htmlspecialchars($result['content'])) ?></p>
                            <a href="<?= getCityServiceUrl($selectedCity, $selectedService) ?>" 
                               class="bg-blue-600 hover:bg-blue-700 text-white px-8 py-3 rounded-md font-semibold transition-colors inline-block">
                                View Full Details
                            </a>
                        </div>
                    </div>
                <?php endif; ?>
                
            <?php elseif ($selectedCity): ?>
                <!-- All Services in Selected City -->
                <?php
                $stmt = $pdo->prepare("
                    SELECT cs.*, c.name as city_name, s.name as service_name, s.slug as service_slug
                    FROM city_service cs
                    JOIN cities c ON cs.city_id = c.id
                    JOIN services s ON cs.service_id = s.id
                    WHERE c.slug = ?
                    ORDER BY s.name
                ");
                $stmt->execute([$selectedCity]);
                $results = $stmt->fetchAll();
                ?>
                
                <div class="text-center mb-12">
                    <h2 class="text-3xl font-bold mb-4">Our Services in <?= htmlspecialchars($results[0]['city_name'] ?? 'Selected City') ?></h2>
                    <p class="text-lg text-gray-600">Professional digital solutions available in your city</p>
                </div>
                
                <div class="grid md:grid-cols-2 lg:grid-cols-3 gap-8">
                    <?php foreach ($results as $result): ?>
                        <div class="bg-white rounded-xl shadow-md hover:shadow-lg transition-shadow p-6 border">
                            <h3 class="text-xl font-bold mb-3"><?= htmlspecialchars($result['service_name']) ?></h3>
                            <p class="text-gray-600 mb-4"><?= substr(strip_tags($result['content']), 0, 150) ?>...</p>
                            <a href="<?= getCityServiceUrl($selectedCity, $result['service_slug']) ?>" 
                               class="text-blue-600 hover:text-blue-800 font-medium inline-flex items-center">
                                Learn More <i class="fas fa-arrow-right ml-2"></i>
                            </a>
                        </div>
                    <?php endforeach; ?>
                </div>
                
            <?php elseif ($selectedService): ?>
                <!-- Selected Service in All Cities -->
                <?php
                $stmt = $pdo->prepare("
                    SELECT cs.*, c.name as city_name, c.slug as city_slug, s.name as service_name
                    FROM city_service cs
                    JOIN cities c ON cs.city_id = c.id
                    JOIN services s ON cs.service_id = s.id
                    WHERE s.slug = ?
                    ORDER BY c.name
                ");
                $stmt->execute([$selectedService]);
                $results = $stmt->fetchAll();
                ?>
                
                <div class="text-center mb-12">
                    <h2 class="text-3xl font-bold mb-4"><?= htmlspecialchars($results[0]['service_name'] ?? 'Selected Service') ?> Services</h2>
                    <p class="text-lg text-gray-600">Available in multiple cities across India</p>
                </div>
                
                <div class="grid md:grid-cols-2 lg:grid-cols-3 gap-6">
                    <?php foreach ($results as $result): ?>
                        <a href="<?= getCityServiceUrl($result['city_slug'], $selectedService) ?>" 
                           class="block bg-white rounded-lg shadow-md hover:shadow-lg transition-shadow p-6 border-l-4 border-blue-500">
                            <h3 class="text-lg font-semibold mb-2"><?= htmlspecialchars($result['service_name']) ?> in <?= htmlspecialchars($result['city_name']) ?></h3>
                            <p class="text-gray-600 text-sm"><?= substr(strip_tags($result['content']), 0, 100) ?>...</p>
                        </a>
                    <?php endforeach; ?>
                </div>
                
            <?php else: ?>
                <!-- Show All Cities and Services -->
                <div class="text-center mb-12">
                    <h2 class="text-3xl font-bold mb-4">Choose Your City and Service</h2>
                    <p class="text-lg text-gray-600">We provide professional digital services across major Indian cities</p>
                </div>
                
                <!-- Cities Grid -->
                <div class="mb-16">
                    <h3 class="text-2xl font-bold mb-8 text-center">Our Service Locations</h3>
                    <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-5 gap-4">
                        <?php foreach ($cities as $city): ?>
                            <a href="<?= getCityServicesUrl($city['slug']) ?>" 
                               class="block bg-blue-50 hover:bg-blue-100 text-blue-800 p-4 rounded-lg text-center font-medium transition-colors">
                                <i class="fas fa-map-marker-alt mb-2 text-lg"></i>
                                <div><?= htmlspecialchars($city['name']) ?></div>
                            </a>
                        <?php endforeach; ?>
                    </div>
                </div>
                
                <!-- Services Grid -->
                <div>
                    <h3 class="text-2xl font-bold mb-8 text-center">Our Services</h3>
                    <div class="grid md:grid-cols-2 lg:grid-cols-3 gap-6">
                        <?php foreach ($services as $service): ?>
                            <a href="<?= getServiceCitiesUrl($service['slug']) ?>" 
                               class="block bg-white rounded-xl shadow-md hover:shadow-lg transition-shadow p-6 border">
                                <h4 class="text-xl font-bold mb-3"><?= htmlspecialchars($service['name']) ?></h4>
                                <p class="text-gray-600 mb-4">Professional <?= htmlspecialchars($service['name']) ?> services available in all our service locations.</p>
                                <div class="text-blue-600 font-medium">
                                    View Cities <i class="fas fa-arrow-right ml-2"></i>
                                </div>
                            </a>
                        <?php endforeach; ?>
                    </div>
                </div>
            <?php endif; ?>
        </div>
    </div>
</section>

<!-- CTA Section -->
<section class="py-16 bg-gradient-to-r from-blue-600 to-blue-700 text-white">
    <div class="container mx-auto px-4 text-center">
        <div class="max-w-4xl mx-auto">
            <h2 class="text-3xl md:text-4xl font-bold mb-6">Ready to Get Started?</h2>
            <p class="text-xl mb-8">Contact us today to discuss your project requirements and get a free consultation.</p>
            <div class="flex flex-col sm:flex-row justify-center gap-4">
                <a href="<?= getBasePath() ?>/contact.php" class="bg-white hover:bg-gray-100 text-blue-600 font-semibold py-3 px-8 rounded-md transition-colors">
                    Get Free Consultation
                </a>
                <a href="tel:+918655442429" class="bg-transparent hover:bg-blue-700 text-white border-2 border-white font-semibold py-3 px-8 rounded-md transition-colors">
                    <i class="fas fa-phone-alt mr-2"></i> +91 86554 42429
                </a>
            </div>
        </div>
    </div>
</section>

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