<?php
require_once 'auth.php';
require_once '../config/database.php';
check_admin_login();

// Récupérer les pages depuis la base de données, en excluant celles gérées ailleurs
$stmt = $pdo->query("SELECT * FROM pages WHERE slug NOT IN ('salles', 'conseil-municipal', 'personnel', 'associations', 'agenda', 'cultes', 'maison-rurale', 'enfance', 'plan') ORDER BY title ASC");
$pages = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gestion des Pages - Admin Kutzenhausen</title>
    <script src="../js/tailwind.min.js"></script>
    <script src="../js/lucide.min.js"></script>
</head>
<body class="bg-gray-50 flex h-screen font-sans text-gray-800">

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

    <!-- Main Content -->
    <main class="flex-1 flex flex-col h-screen overflow-hidden">
        
        <header class="h-20 bg-white shadow-sm flex items-center justify-between px-8 flex-shrink-0">
            <div class="flex items-center space-x-4">
                <i data-lucide="file-text" class="w-8 h-8 text-[#7b2c2d]"></i>
                <h1 class="text-2xl font-bold text-gray-800">Gestion des Pages</h1>
            </div>
            <!-- Pas de bouton "Nouvelle page" car les pages de menu sont généralement fixes -->
            <button class="text-gray-500 hover:text-[#7b2c2d] px-4 py-2 text-sm font-medium transition flex items-center" title="Les pages sont liées au menu principal">
                <i data-lucide="info" class="w-4 h-4 mr-2"></i> Information
            </button>
        </header>
        
        <div class="flex-1 overflow-auto p-8">
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden max-w-5xl mx-auto">
                <div class="p-6 border-b border-gray-200 bg-gray-50">
                    <h2 class="font-bold text-gray-800 text-lg">Contenu du Menu Principal</h2>
                    <p class="text-sm text-gray-500 mt-1">Ces pages correspondent aux onglets du site (Mot du Maire, Conseil Municipal, Démarches...). Vous pouvez modifier leur texte et leurs images ici.</p>
                </div>
                
                <ul class="divide-y divide-gray-100">
                    <?php foreach($pages as $page): ?>
                    <li class="p-6 hover:bg-gray-50 transition flex items-center justify-between group">
                        <div class="flex items-center space-x-4">
                            <div class="p-3 bg-red-50 text-[#7b2c2d] rounded-lg group-hover:scale-110 transition">
                                <i data-lucide="file-edit" class="w-6 h-6"></i>
                            </div>
                            <div>
                                <h3 class="text-lg font-bold text-gray-800"><?php echo $page['title']; ?></h3>
                                <p class="text-sm text-gray-500 font-mono mt-1">/<?php echo $page['slug']; ?>.php</p>
                            </div>
                        </div>
                        
                        <div class="flex flex-col items-end space-y-3">
                            <span class="text-xs text-gray-400">Modifiée le <?php echo $page['updated_at'] ? date('d/m/Y', strtotime($page['updated_at'])) : 'Non défini'; ?></span>
                            <a href="pages-edit.php?id=<?php echo $page['id']; ?>" class="bg-white border border-[#7b2c2d] text-[#7b2c2d] hover:bg-[#7b2c2d] hover:text-white px-4 py-2 rounded-md text-sm font-bold transition flex items-center shadow-sm">
                                <i data-lucide="edit" class="w-4 h-4 mr-2"></i> Éditer le contenu
                            </a>
                        </div>
                    </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>
    </main>

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