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

// Récupérer les vraies statistiques depuis la BDD
try {
    // Nombre de pages
    $stmt = $pdo->query("SELECT COUNT(*) FROM pages");
    $page_count = $stmt->fetchColumn();

    // Nombre d'articles
    $stmt = $pdo->query("SELECT COUNT(*) FROM articles");
    $article_count = $stmt->fetchColumn();

    // Nombre de documents
    $stmt = $pdo->query("SELECT COUNT(*) FROM documents");
    $doc_count = $stmt->fetchColumn();
} catch (PDOException $e) {
    // En cas d'erreur, on met des valeurs par défaut
    $page_count = 0;
    $article_count = 0;
    $doc_count = 0;
}

?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard - Administration Kutzenhausen</title>
    <script src="../js/tailwind.min.js"></script>
    <script src="../js/lucide.min.js"></script>
</head>
<body class="bg-gray-100 flex h-screen font-sans">

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

    <!-- Main Content -->
    <main class="flex-1 flex flex-col h-screen overflow-hidden">
        
        <!-- Headerbar -->
        <header class="h-20 bg-white shadow-sm flex items-center justify-between px-8">
            <h1 class="text-2xl font-bold text-gray-800">Vue d'ensemble</h1>
            <div class="flex items-center space-x-4">
                <span class="text-sm text-gray-500">Connecté en tant que <strong><?php echo htmlspecialchars($_SESSION['username']); ?></strong></span>
                <a href="../index.php" target="_blank" class="bg-gray-100 hover:bg-gray-200 text-gray-700 px-4 py-2 rounded-md text-sm font-medium transition flex items-center">
                    <i data-lucide="external-link" class="w-4 h-4 mr-2"></i> Voir le site
                </a>
            </div>
        </header>
        
        <!-- Content Area -->
        <div class="flex-1 overflow-auto p-8">
            
            <!-- Cards -->
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
                
                <div class="bg-white rounded-xl shadow-sm p-6 border-l-4 border-blue-500">
                    <div class="flex items-center">
                        <div class="p-3 rounded-full bg-blue-100 text-blue-500 mr-4">
                            <i data-lucide="file-text" class="w-6 h-6"></i>
                        </div>
                        <div>
                            <p class="text-sm font-medium text-gray-500">Pages publiées</p>
                            <p class="text-2xl font-bold text-gray-800"><?php echo $page_count; ?></p>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-sm p-6 border-l-4 border-green-500">
                    <div class="flex items-center">
                        <div class="p-3 rounded-full bg-green-100 text-green-500 mr-4">
                            <i data-lucide="newspaper" class="w-6 h-6"></i>
                        </div>
                        <div>
                            <p class="text-sm font-medium text-gray-500">Actualités</p>
                            <p class="text-2xl font-bold text-gray-800"><?php echo $article_count; ?></p>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-sm p-6 border-l-4 border-yellow-500">
                    <div class="flex items-center">
                        <div class="p-3 rounded-full bg-yellow-100 text-yellow-500 mr-4">
                            <i data-lucide="file-archive" class="w-6 h-6"></i>
                        </div>
                        <div>
                            <p class="text-sm font-medium text-gray-500">Documents / Arrêtés</p>
                            <p class="text-2xl font-bold text-gray-800"><?php echo $doc_count; ?></p>
                        </div>
                    </div>
                </div>
                
            </div>
            
            <!-- Actions Rapides -->
            <h2 class="text-xl font-bold text-gray-800 mb-4">Actions rapides</h2>
            <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                    <a href="articles-edit.php" class="flex items-center p-4 border border-gray-200 rounded-lg hover:border-[#7b2c2d] hover:bg-red-50 transition group cursor-pointer">
                        <div class="p-3 rounded-full bg-red-100 text-[#7b2c2d] mr-4 group-hover:scale-110 transition">
                            <i data-lucide="plus" class="w-5 h-5"></i>
                        </div>
                        <div>
                            <h3 class="font-bold text-gray-800 group-hover:text-[#7b2c2d]">Créer une actualité</h3>
                            <p class="text-sm text-gray-500">Publier une annonce</p>
                        </div>
                    </a>
                    
                    <a href="associations.php" class="flex items-center p-4 border border-gray-200 rounded-lg hover:border-[#7b2c2d] hover:bg-red-50 transition group cursor-pointer">
                        <div class="p-3 rounded-full bg-red-100 text-[#7b2c2d] mr-4 group-hover:scale-110 transition">
                            <i data-lucide="user-plus" class="w-5 h-5"></i>
                        </div>
                        <div>
                            <h3 class="font-bold text-gray-800 group-hover:text-[#7b2c2d]">Ajouter une association</h3>
                            <p class="text-sm text-gray-500">Nouveau compte asso</p>
                        </div>
                    </a>

                    <a href="agenda.php" class="flex items-center p-4 border border-gray-200 rounded-lg hover:border-[#7b2c2d] hover:bg-red-50 transition group cursor-pointer">
                        <div class="p-3 rounded-full bg-red-100 text-[#7b2c2d] mr-4 group-hover:scale-110 transition">
                            <i data-lucide="calendar-plus" class="w-5 h-5"></i>
                        </div>
                        <div>
                            <h3 class="font-bold text-gray-800 group-hover:text-[#7b2c2d]">Gérer l'agenda</h3>
                            <p class="text-sm text-gray-500">Ajouter une manifestation</p>
                        </div>
                    </a>

                    <a href="reservations.php" class="flex items-center p-4 border border-gray-200 rounded-lg hover:border-[#7b2c2d] hover:bg-red-50 transition group cursor-pointer">
                        <div class="p-3 rounded-full bg-red-100 text-[#7b2c2d] mr-4 group-hover:scale-110 transition">
                            <i data-lucide="key" class="w-5 h-5"></i>
                        </div>
                        <div>
                            <h3 class="font-bold text-gray-800 group-hover:text-[#7b2c2d]">Réservations Salles</h3>
                            <p class="text-sm text-gray-500">Voir les demandes</p>
                        </div>
                    </a>

                    <a href="pages.php" class="flex items-center p-4 border border-gray-200 rounded-lg hover:border-[#7b2c2d] hover:bg-red-50 transition group cursor-pointer">
                        <div class="p-3 rounded-full bg-red-100 text-[#7b2c2d] mr-4 group-hover:scale-110 transition">
                            <i data-lucide="edit-3" class="w-5 h-5"></i>
                        </div>
                        <div>
                            <h3 class="font-bold text-gray-800 group-hover:text-[#7b2c2d]">Éditer les pages</h3>
                            <p class="text-sm text-gray-500">Modifier le contenu du site</p>
                        </div>
                    </a>

                    <a href="backup.php" class="flex items-center p-4 border border-gray-200 rounded-lg hover:border-blue-600 hover:bg-blue-50 transition group cursor-pointer">
                        <div class="p-3 rounded-full bg-blue-100 text-blue-600 mr-4 group-hover:scale-110 transition">
                            <i data-lucide="save" class="w-5 h-5"></i>
                        </div>
                        <div>
                            <h3 class="font-bold text-gray-800 group-hover:text-blue-600">Sauvegarde Site/BDD</h3>
                            <p class="text-sm text-gray-500">Exporter les données</p>
                        </div>
                    </a>
                </div>
            </div>
            
        </div>
    </main>

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