<?php 
require_once 'config/database.php';
include 'includes/header.php'; 

$q = isset($_GET['q']) ? trim($_GET['q']) : '';
$results = [];

if ($q !== '') {
    $searchTerm = "%$q%";
    
    // Rechercher dans les pages
    $stmt = $pdo->prepare("SELECT 'page' as type, title, slug, LEFT(content, 200) as excerpt FROM pages WHERE title LIKE :q OR content LIKE :q");
    $stmt->execute(['q' => $searchTerm]);
    $results = array_merge($results, $stmt->fetchAll());
    
    // Rechercher dans les articles
    $stmt = $pdo->prepare("SELECT 'article' as type, title, slug, excerpt FROM articles WHERE title LIKE :q OR content LIKE :q OR excerpt LIKE :q");
    $stmt->execute(['q' => $searchTerm]);
    $results = array_merge($results, $stmt->fetchAll());
    
    // Rechercher dans les documents
    $stmt = $pdo->prepare("SELECT 'document' as type, title, file_path as slug FROM documents WHERE title LIKE :q");
    $stmt->execute(['q' => $searchTerm]);
    $results = array_merge($results, $stmt->fetchAll());
}
?>

<main class="min-h-screen py-16 bg-alsace-cream">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <h2 class="font-serif text-3xl md:text-4xl font-bold text-alsace-red mb-8 border-b-2 border-alsace-red pb-4">
            Résultats de recherche pour : "<?php echo htmlspecialchars($q); ?>"
        </h2>
        
        <?php if (empty($results)): ?>
            <div class="bg-white rounded-xl shadow-md p-8 border border-gray-100">
                <p class="text-gray-600 text-lg">Aucun résultat trouvé pour votre recherche.</p>
                <div class="mt-6">
                    <a href="index.php" class="text-alsace-red font-bold hover:underline flex items-center">
                        <i data-lucide="arrow-left" class="w-4 h-4 mr-2"></i> Retour à l'accueil
                    </a>
                </div>
            </div>
        <?php else: ?>
            <div class="grid grid-cols-1 gap-6">
                <?php foreach ($results as $item): ?>
                    <div class="bg-white rounded-xl shadow-md p-6 border border-gray-100 hover:shadow-lg transition">
                        <div class="flex items-start justify-between">
                            <div>
                                <span class="inline-block px-2 py-1 text-xs font-bold uppercase tracking-wider rounded-md mb-2 
                                    <?php 
                                        if ($item['type'] == 'page') echo 'bg-blue-100 text-blue-800';
                                        elseif ($item['type'] == 'article') echo 'bg-green-100 text-green-800';
                                        else echo 'bg-yellow-100 text-yellow-800';
                                    ?>">
                                    <?php echo $item['type']; ?>
                                </span>
                                <h3 class="text-xl font-bold text-gray-800 mb-2">
                                    <?php if ($item['type'] == 'page'): ?>
                                        <a href="<?php echo $item['slug']; ?>.php" class="hover:text-alsace-red"><?php echo htmlspecialchars($item['title']); ?></a>
                                    <?php elseif ($item['type'] == 'article'): ?>
                                        <a href="article.php?slug=<?php echo $item['slug']; ?>" class="hover:text-alsace-red"><?php echo htmlspecialchars($item['title']); ?></a>
                                    <?php else: ?>
                                        <a href="uploads/<?php echo $item['slug']; ?>" target="_blank" class="hover:text-alsace-red"><?php echo htmlspecialchars($item['title']); ?></a>
                                    <?php endif; ?>
                                </h3>
                                <p class="text-gray-600 text-sm line-clamp-2">
                                    <?php echo strip_tags($item['excerpt'] ?? ''); ?>
                                </p>
                            </div>
                            <div class="flex-shrink-0 ml-4">
                                <?php if ($item['type'] == 'page'): ?>
                                    <i data-lucide="file-text" class="w-6 h-6 text-blue-500"></i>
                                <?php elseif ($item['type'] == 'article'): ?>
                                    <i data-lucide="newspaper" class="w-6 h-6 text-green-500"></i>
                                <?php else: ?>
                                    <i data-lucide="file-archive" class="w-6 h-6 text-yellow-500"></i>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</main>

<script>
    lucide.createIcons();
</script>

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