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

// Fetch documents from database
$stmt = $pdo->query("SELECT * FROM documents ORDER BY date_document DESC");
$documents = $stmt->fetchAll();

// Group documents by type
$grouped_docs = [
    'bulletin' => [],
    'compte_rendu' => [],
    'arrete_municipal' => [],
    'arrete_prefectoral' => []
];

foreach ($documents as $doc) {
    if (isset($grouped_docs[$doc['type']])) {
        $grouped_docs[$doc['type']][] = $doc;
    }
}

include 'includes/header.php'; 
?>

<main class="min-h-screen py-16 bg-gray-50">
    <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">
            Publications Officielles
        </h2>
        
        <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
            
            <!-- Bulletins Communaux -->
            <div class="bg-white rounded-xl shadow-md p-6 border border-gray-100">
                <h3 class="text-xl font-bold text-gray-800 mb-4 flex items-center">
                    <i data-lucide="book-open" class="w-6 h-6 text-alsace-red mr-2"></i> Bulletins Communaux
                </h3>
                <ul class="space-y-3">
                    <?php if (empty($grouped_docs['bulletin'])): ?>
                        <li class="text-gray-500 text-sm">Aucun bulletin pour le moment.</li>
                    <?php else: ?>
                        <?php foreach($grouped_docs['bulletin'] as $doc): ?>
                        <li>
                            <a href="uploads/<?php echo htmlspecialchars($doc['file_path']); ?>" target="_blank" class="flex items-center justify-between p-3 rounded-lg hover:bg-gray-50 transition border border-transparent hover:border-gray-200 group">
                                <span class="font-medium text-gray-700 group-hover:text-alsace-red transition"><?php echo htmlspecialchars($doc['title']); ?></span>
                                <div class="flex items-center text-sm text-gray-500">
                                    <span class="mr-4"><?php echo date('d/m/Y', strtotime($doc['date_document'])); ?></span>
                                    <i data-lucide="download" class="w-4 h-4 text-alsace-red"></i>
                                </div>
                            </a>
                        </li>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </ul>
            </div>

            <!-- Comptes Rendus -->
            <div class="bg-white rounded-xl shadow-md p-6 border border-gray-100">
                <h3 class="text-xl font-bold text-gray-800 mb-4 flex items-center">
                    <i data-lucide="users" class="w-6 h-6 text-alsace-red mr-2"></i> Comptes Rendus
                </h3>
                <ul class="space-y-3">
                    <?php if (empty($grouped_docs['compte_rendu'])): ?>
                        <li class="text-gray-500 text-sm">Aucun compte rendu pour le moment.</li>
                    <?php else: ?>
                        <?php foreach($grouped_docs['compte_rendu'] as $doc): ?>
                        <li>
                            <a href="uploads/<?php echo htmlspecialchars($doc['file_path']); ?>" target="_blank" class="flex items-center justify-between p-3 rounded-lg hover:bg-gray-50 transition border border-transparent hover:border-gray-200 group">
                                <span class="font-medium text-gray-700 group-hover:text-alsace-red transition"><?php echo htmlspecialchars($doc['title']); ?></span>
                                <div class="flex items-center text-sm text-gray-500">
                                    <span class="mr-4"><?php echo date('d/m/Y', strtotime($doc['date_document'])); ?></span>
                                    <i data-lucide="download" class="w-4 h-4 text-alsace-red"></i>
                                </div>
                            </a>
                        </li>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </ul>
            </div>

            <!-- Arrêtés Municipaux -->
            <div class="bg-white rounded-xl shadow-md p-6 border border-gray-100">
                <h3 class="text-xl font-bold text-gray-800 mb-4 flex items-center">
                    <i data-lucide="file-text" class="w-6 h-6 text-alsace-red mr-2"></i> Arrêtés Municipaux
                </h3>
                <ul class="space-y-3">
                    <?php if (empty($grouped_docs['arrete_municipal'])): ?>
                        <li class="text-gray-500 text-sm">Aucun arrêté pour le moment.</li>
                    <?php else: ?>
                        <?php foreach($grouped_docs['arrete_municipal'] as $doc): ?>
                        <li>
                            <a href="uploads/<?php echo htmlspecialchars($doc['file_path']); ?>" target="_blank" class="flex items-center justify-between p-3 rounded-lg hover:bg-gray-50 transition border border-transparent hover:border-gray-200 group">
                                <span class="font-medium text-gray-700 group-hover:text-alsace-red transition"><?php echo htmlspecialchars($doc['title']); ?></span>
                                <div class="flex items-center text-sm text-gray-500">
                                    <span class="mr-4"><?php echo date('d/m/Y', strtotime($doc['date_document'])); ?></span>
                                    <i data-lucide="download" class="w-4 h-4 text-alsace-red"></i>
                                </div>
                            </a>
                        </li>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </ul>
            </div>

            <!-- Arrêtés Préfectoraux -->
            <div class="bg-white rounded-xl shadow-md p-6 border border-gray-100">
                <h3 class="text-xl font-bold text-gray-800 mb-4 flex items-center">
                    <i data-lucide="alert-triangle" class="w-6 h-6 text-alsace-red mr-2"></i> Arrêtés Préfectoraux
                </h3>
                <ul class="space-y-3">
                    <?php if (empty($grouped_docs['arrete_prefectoral'])): ?>
                        <li class="text-gray-500 text-sm">Aucun arrêté pour le moment.</li>
                    <?php else: ?>
                        <?php foreach($grouped_docs['arrete_prefectoral'] as $doc): ?>
                        <li>
                            <a href="uploads/<?php echo htmlspecialchars($doc['file_path']); ?>" target="_blank" class="flex items-center justify-between p-3 rounded-lg hover:bg-gray-50 transition border border-transparent hover:border-gray-200 group">
                                <span class="font-medium text-gray-700 group-hover:text-alsace-red transition"><?php echo htmlspecialchars($doc['title']); ?></span>
                                <div class="flex items-center text-sm text-gray-500">
                                    <span class="mr-4"><?php echo date('d/m/Y', strtotime($doc['date_document'])); ?></span>
                                    <i data-lucide="download" class="w-4 h-4 text-alsace-red"></i>
                                </div>
                            </a>
                        </li>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </ul>
            </div>

        </div>
    </div>
</main>

<script>
    if(typeof lucide !== 'undefined') {
        lucide.createIcons();
    }
</script>

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