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

if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    $stmt = $pdo->prepare("DELETE FROM articles WHERE id = :id");
    $stmt->execute(['id' => $id]);
    header("Location: articles.php");
    exit;
}

$stmt = $pdo->query("SELECT ar.*, a.name as asso_name FROM articles ar LEFT JOIN associations a ON ar.association_id = a.id ORDER BY ar.created_at DESC");
$articles = $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 Actualités - 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 -->
        <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="newspaper" class="w-8 h-8 text-[#7b2c2d]"></i>
                <h1 class="text-2xl font-bold text-gray-800">Gestion des Actualités</h1>
            </div>
            <a href="articles-edit.php" class="bg-[#7b2c2d] hover:bg-[#5c1a1b] text-white px-4 py-2 rounded-md font-medium transition flex items-center shadow-sm">
                <i data-lucide="plus" class="w-5 h-5 mr-2"></i> Nouvelle actualité
            </a>
        </header>
        
        <!-- Content Area -->
        <div class="flex-1 overflow-auto p-8">
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
                <table class="w-full text-left border-collapse">
                    <thead>
                        <tr class="bg-gray-50 border-b border-gray-200">
                            <th class="px-6 py-4 text-sm font-semibold text-gray-600 uppercase tracking-wider">Titre</th>
                            <th class="px-6 py-4 text-sm font-semibold text-gray-600 uppercase tracking-wider">Auteur</th>
                            <th class="px-6 py-4 text-sm font-semibold text-gray-600 uppercase tracking-wider">Catégorie</th>
                            <th class="px-6 py-4 text-sm font-semibold text-gray-600 uppercase tracking-wider">Date</th>
                            <th class="px-6 py-4 text-sm font-semibold text-gray-600 uppercase tracking-wider">Statut</th>
                            <th class="px-6 py-4 text-sm font-semibold text-gray-600 uppercase tracking-wider text-right">Actions</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100">
                        <?php foreach($articles as $article): ?>
                        <tr class="hover:bg-gray-50 transition">
                            <td class="px-6 py-4">
                                <span class="font-medium text-gray-800"><?php echo htmlspecialchars($article['title']); ?></span>
                            </td>
                            <td class="px-6 py-4">
                                <?php if($article['association_id']): ?>
                                    <span class="text-xs font-bold text-blue-600 bg-blue-50 px-2 py-1 rounded border border-blue-100 uppercase"><?php echo htmlspecialchars($article['asso_name']); ?></span>
                                <?php else: ?>
                                    <span class="text-xs font-bold text-gray-600 bg-gray-50 px-2 py-1 rounded border border-gray-200 uppercase">Mairie</span>
                                <?php endif; ?>
                            </td>
                            <td class="px-6 py-4">
                                <span class="text-sm text-gray-600 bg-gray-100 px-2 py-1 rounded"><?php echo htmlspecialchars($article['category']); ?></span>
                            </td>
                            <td class="px-6 py-4 text-sm text-gray-500">
                                <?php echo date('d/m/Y', strtotime($article['created_at'])); ?>
                            </td>
                            <td class="px-6 py-4">
                                <?php if($article['is_published'] == 1): ?>
                                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">
                                        Publié
                                    </span>
                                <?php else: ?>
                                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-yellow-100 text-yellow-800">
                                        Brouillon
                                    </span>
                                <?php endif; ?>
                            </td>
                            <td class="px-6 py-4 text-right space-x-2">
                                <a href="articles-edit.php?id=<?php echo $article['id']; ?>" class="inline-flex items-center p-2 bg-blue-50 text-blue-600 rounded-md hover:bg-blue-100 transition" title="Modifier">
                                    <i data-lucide="edit" class="w-4 h-4"></i>
                                </a>
                                <a href="articles.php?delete=<?php echo $article['id']; ?>" class="inline-flex items-center p-2 bg-red-50 text-red-600 rounded-md hover:bg-red-100 transition" title="Supprimer" onclick="return confirm('Êtes-vous sûr de vouloir supprimer cette actualité ?')">
                                    <i data-lucide="trash-2" class="w-4 h-4"></i>
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                
                <?php if(empty($articles)): ?>
                <div class="text-center py-12">
                    <i data-lucide="inbox" class="mx-auto h-12 w-12 text-gray-300"></i>
                    <h3 class="mt-2 text-sm font-medium text-gray-900">Aucune actualité</h3>
                    <p class="mt-1 text-sm text-gray-500">Commencez par créer une nouvelle actualité.</p>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </main>

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