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

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

$page_title = 'Arama';
$results = [];

if (!empty($query)) {
    $searchTerm = "%$query%";
    
    // Firmalarda ara
    $stmt = $pdo->prepare("
        SELECT 'company' as type, id, company_name as title, about as description, '' 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 'service' as type, 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 'blog' as type, 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();
    
    $results = array_merge($companies, $services, $blogPosts);
}

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 if (empty($results)): ?>
            <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 <?= count($results) ?> sonuç bulundu</p>
            </div>
            
            <div class="row">
                <?php foreach ($results as $result): ?>
                    <div class="col-md-6 mb-4">
                        <div class="card h-100">
                            <?php if ($result['image']): ?>
                                <img src="<?= e($result['image']) ?>" class="card-img-top" alt="<?= e($result['title']) ?>" style="height: 200px; object-fit: cover;">
                            <?php endif; ?>
                            <div class="card-body">
                                <span class="badge bg-<?= $result['type'] === 'company' ? 'primary' : ($result['type'] === 'service' ? 'success' : 'info') ?> mb-2">
                                    <?= $result['type'] === 'company' ? 'Firma' : ($result['type'] === 'service' ? 'Hizmet' : 'Blog') ?>
                                </span>
                                <h5 class="card-title"><?= e($result['title']) ?></h5>
                                <p class="card-text"><?= excerpt($result['description'], 100) ?></p>
                                <small class="text-muted">
                                    <i class="fas fa-eye me-1"></i><?= $result['views'] ?> görüntülenme
                                </small>
                            </div>
                            <div class="card-footer">
                                <?php if ($result['type'] === 'company'): ?>
                                    <a href="<?= SITE_URL ?>/firma/<?= $result['id'] ?>" class="btn btn-primary">Detaylar</a>
                                <?php elseif ($result['type'] === 'service'): ?>
                                    <a href="<?= SITE_URL ?>/hizmet/<?= $result['id'] ?>" class="btn btn-primary">Detaylar</a>
                                <?php else: ?>
                                    <a href="<?= SITE_URL ?>/blog/<?= $result['slug'] ?? '' ?>" class="btn btn-primary">Devamını Oku</a>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    <?php endif; ?>
</div>

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

