<?php
require_once 'config.php';
session_start();

// Sécurité : accès réservé à l'admin
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    header("Location: index.php");
    exit;
}

// Statistiques
$totalUsers = $pdo->query("SELECT COUNT(*) FROM utilisateurs")->fetchColumn();
$totalPosts = $pdo->query("SELECT COUNT(*) FROM posts")->fetchColumn();
$totalAdmins = $pdo->query("SELECT COUNT(*) FROM utilisateurs WHERE role = 'admin'")->fetchColumn();
$totalAuteurs = $pdo->query("SELECT COUNT(*) FROM utilisateurs WHERE role = 'auteur'")->fetchColumn();

// Récupération des utilisateurs et articles
$users = $pdo->query("SELECT id, username, role FROM utilisateurs ORDER BY id DESC")->fetchAll();
$articles = $pdo->query("SELECT p.id, p.title, u.username, p.date_creation
                         FROM posts p
                         LEFT JOIN utilisateurs u ON p.user_id = u.id
                         ORDER BY p.date_creation DESC")->fetchAll();

include 'header.php';
?>

<div class="dashboard-container">
    <h2>📊 Tableau de bord administrateur</h2>
    <p>Bienvenue <strong><?= htmlspecialchars($_SESSION['username']) ?></strong> !</p>
    <hr>

    <!-- Statistiques -->
    <div class="stats">
        <div class="card pink">
            <h3><?= $totalUsers ?></h3>
            <p>Utilisateurs</p>
        </div>
        <div class="card orange">
            <h3><?= $totalAuteurs ?></h3>
            <p>Auteurs</p>
        </div>
        <div class="card purple">
            <h3><?= $totalAdmins ?></h3>
            <p>Admins</p>
        </div>
        <div class="card blue">
            <h3><?= $totalPosts ?></h3>
            <p>Articles</p>
        </div>
    </div>

    <!-- Liste des utilisateurs -->
    <h3>👥 Liste des utilisateurs</h3>
    <table>
        <tr>
            <th>ID</th>
            <th>Nom d’utilisateur</th>
            <th>Rôle</th>
            <th>Actions</th>
        </tr>
        <?php foreach ($users as $user): ?>
            <tr>
                <td><?= $user['id'] ?></td>
                <td><?= htmlspecialchars($user['username']) ?></td>
                <td><?= htmlspecialchars($user['role']) ?></td>
                <td>
                    <a href="delete_user.php?id=<?= $user['id'] ?>" class="btn-delete" onclick="return confirm('Supprimer cet utilisateur ?')">🗑 Supprimer</a>
                </td>
            </tr>
        <?php endforeach; ?>
    </table>

    <br>

    <!-- Liste des articles -->
    <h3>📰 Articles publiés</h3>
    <table>
        <tr>
            <th>ID</th>
            <th>Titre</th>
            <th>Auteur</th>
            <th>Date</th>
            <th>Actions</th>
        </tr>
        <?php foreach ($articles as $article): ?>
            <tr>
                <td><?= $article['id'] ?></td>
                <td><?= htmlspecialchars($article['title']) ?></td>
                <td><?= htmlspecialchars($article['username']) ?></td>
                <td><?= htmlspecialchars($article['date_creation']) ?></td>
                <td>
                    <a href="edit_article.php?id=<?= $article['id'] ?>" class="btn-edit">✏️ Modifier</a>
                    <a href="delete_article.php?id=<?= $article['id'] ?>" class="btn-delete" onclick="return confirm('Supprimer cet article ?')">🗑 Supprimer</a>
                </td>
            </tr>
        <?php endforeach; ?>
    </table>
</div>

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

<style>
.dashboard-container {
    max-width: 1100px;
    margin: 40px auto;
    background: #fff;
    border-radius: 15px;
    padding: 30px;
    box-shadow: 0 4px 10px rgba(0,0,0,0.1);
}

.dashboard-container h2 {
    text-align: center;
    color: #ff6fa7;
}

.stats {
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
    margin: 20px 0 40px;
}

.card {
    width: 200px;
    text-align: center;
    border-radius: 12px;
    padding: 20px;
    color: white;
    font-weight: bold;
    box-shadow: 0 4px 10px rgba(0,0,0,0.1);
}
.card h3 {
    font-size: 32px;
    margin: 0;
}
.card p {
    margin: 5px 0 0;
}

.pink { background: #ff6fa7; }
.orange { background: #ffa45b; }
.purple { background: #b69cff; }
.blue { background: #6c63ff; }

table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 15px;
}

th, td {
    padding: 10px;
    border-bottom: 1px solid #ddd;
    text-align: center;
}

th {
    background: #fff5f8;
    color: #333;
}

.btn-edit, .btn-delete {
    padding: 6px 10px;
    border-radius: 6px;
    text-decoration: none;
    color: white;
    font-size: 14px;
}

.btn-edit {
    background: #6c63ff;
}

.btn-delete {
    background: #ff5c5c;
}

.btn-edit:hover {
    background: #5a52e0;
}
.btn-delete:hover {
    background: #e24a4a;
}
</style>
