<?php
require_once 'config/config.php';

$query = trim($_GET['q'] ?? '');

$page_title = 'Arama';
$companies = [];
$services = [];
$blogPosts = [];

if (!empty($query)) {
    $searchTerm = "%$query%";
    
    // Firmalarda ara
    $stmt = $pdo->prepare("
        SELECT id, company_name as title, about as description, logo as image, views
        FROM companies
        WHERE (company_name LIKE ? OR about LIKE ?) AND status = 'active'
        ORDER BY views DESC
        LIMIT 10
    ");
    $stmt->execute([$searchTerm, $searchTerm]);
    $companies = $stmt->fetchAll();
    
    // Hizmetlerde ara
    $stmt = $pdo->prepare("
        SELECT s.id, s.title, s.description, s.image, s.views
        FROM services s
        INNER JOIN companies c ON s.company_id = c.id
        WHERE (s.title LIKE ? OR s.description LIKE ?) AND s.status = 'active' AND c.status = 'active'
        ORDER BY s.views DESC
        LIMIT 10
    ");
    $stmt->execute([$searchTerm, $searchTerm]);
    $services = $stmt->fetchAll();
    
    // Blog yazılarında ara
    $stmt = $pdo->prepare("
        SELECT id, title, excerpt as description, image, views, slug
        FROM blog_posts
        WHERE (title LIKE ? OR content LIKE ? OR excerpt LIKE ?) AND status = 'published'
        ORDER BY created_at DESC
        LIMIT 10
    ");
    $stmt->execute([$searchTerm, $searchTerm, $searchTerm]);
    $blogPosts = $stmt->fetchAll();
}

include 'includes/header.php';
?>

<div class="container mt-4">
    <div class="row">
        <div class="col-12 mb-4">
            <h1>Arama</h1>
            <form method="GET" class="mb-4">
                <div class="input-group">
                    <input type="text" class="form-control form-control-lg" name="q" value="<?= e($query) ?>" placeholder="Arama yapın...">
                    <button class="btn btn-primary btn-lg" type="submit">
                        <i class="fas fa-search me-2"></i>Ara
                    </button>
                </div>
            </form>
        </div>
    </div>
    
    <?php if (!empty($query)): ?>
        <?php 
        $hasResults = !empty($companies) || !empty($services) || !empty($blogPosts);
        if (!$hasResults): ?>
            <div class="alert alert-info">
                <i class="fas fa-info-circle me-2"></i>"<?= e($query) ?>" için sonuç bulunamadı.
            </div>
        <?php else: ?>
            <div class="mb-3">
                <p class="text-muted">"<?= e($query) ?>" için 
                    <?php 
                    $totalCount = count($companies) + count($services) + count($blogPosts);
                    echo $totalCount;
                    ?> sonuç bulundu
                </p>
            </div>
            
            <!-- Firmalar -->
            <?php if (!empty($companies)): ?>
                <section class="mb-5">
                    <h3 class="mb-4">
                        <i class="fas fa-building me-2 text-primary"></i>Firmalar (<?= count($companies) ?>)
                    </h3>
                    <div class="row">
                        <?php foreach ($companies as $company): ?>
                            <div class="col-md-6 mb-4">
                                <div class="card h-100">
                                    <?php if ($company['image']): ?>
                                        <img src="<?= e(getImageUrl($company['image'])) ?>" class="card-img-top" alt="<?= e($company['title']) ?>" style="height: 200px; object-fit: cover;">
                                    <?php endif; ?>
                                    <div class="card-body">
                                        <span class="badge bg-primary mb-2">Firma</span>
                                        <h5 class="card-title"><?= e($company['title']) ?></h5>
                                        <p class="card-text"><?= excerpt($company['description'], 100) ?></p>
                                        <small class="text-muted">
                                            <i class="fas fa-eye me-1"></i><?= $company['views'] ?> görüntülenme
                                        </small>
                                    </div>
                                    <div class="card-footer">
                                        <a href="<?= SITE_URL ?>/firma/<?= $company['id'] ?>" class="btn btn-primary">Detaylar</a>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </section>
            <?php endif; ?>
            
            <!-- Hizmetler -->
            <?php if (!empty($services)): ?>
                <section class="mb-5">
                    <h3 class="mb-4">
                        <i class="fas fa-concierge-bell me-2 text-success"></i>Hizmetler (<?= count($services) ?>)
                    </h3>
                    <div class="row">
                        <?php foreach ($services as $service): ?>
                            <div class="col-md-6 mb-4">
                                <div class="card h-100">
                                    <?php if ($service['image']): ?>
                                        <img src="<?= e(getImageUrl($service['image'])) ?>" class="card-img-top" alt="<?= e($service['title']) ?>" style="height: 200px; object-fit: cover;">
                                    <?php endif; ?>
                                    <div class="card-body">
                                        <span class="badge bg-success mb-2">Hizmet</span>
                                        <h5 class="card-title"><?= e($service['title']) ?></h5>
                                        <p class="card-text"><?= excerpt($service['description'], 100) ?></p>
                                        <small class="text-muted">
                                            <i class="fas fa-eye me-1"></i><?= $service['views'] ?> görüntülenme
                                        </small>
                                    </div>
                                    <div class="card-footer">
                                        <a href="<?= SITE_URL ?>/hizmet/<?= $service['id'] ?>" class="btn btn-primary">Detaylar</a>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </section>
            <?php endif; ?>
            
            <!-- Blog Yazıları -->
            <?php if (!empty($blogPosts)): ?>
                <section class="mb-5">
                    <h3 class="mb-4">
                        <i class="fas fa-blog me-2 text-info"></i>Blog Yazıları (<?= count($blogPosts) ?>)
                    </h3>
                    <div class="row">
                        <?php foreach ($blogPosts as $post): ?>
                            <div class="col-md-6 mb-4">
                                <div class="card h-100">
                                    <?php if ($post['image']): ?>
                                        <img src="<?= e(getImageUrl($post['image'])) ?>" class="card-img-top" alt="<?= e($post['title']) ?>" style="height: 200px; object-fit: cover;">
                                    <?php endif; ?>
                                    <div class="card-body">
                                        <span class="badge bg-info mb-2">Blog</span>
                                        <h5 class="card-title"><?= e($post['title']) ?></h5>
                                        <p class="card-text"><?= excerpt($post['description'], 100) ?></p>
                                        <small class="text-muted">
                                            <i class="fas fa-eye me-1"></i><?= $post['views'] ?> görüntülenme
                                        </small>
                                    </div>
                                    <div class="card-footer">
                                        <a href="<?= SITE_URL ?>/blog/<?= $post['slug'] ?? '' ?>" class="btn btn-primary">Devamını Oku</a>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </section>
            <?php endif; ?>
        <?php endif; ?>
    <?php endif; ?>
</div>

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

