<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles/style.css">
    <title>Accueil</title>
</head>
<body>

<?php
if (isset($_SESSION["login"]))
{ 
echo "<h1>Bonjour Administrateur <br></h1>";
}
?>

<a href='index.php?action=deconn' class='logout-button'>Se déconnecter</a>
<a href="index.php?action=admin" class='login-button'>Administration</a>

<br><br>


<?php
echo"<?php
<div class='blog-post'>
<h2>Nouveau Post</h2>
<form action='index.php?action=add_billet' method='POST'>
<label for='titre'>Entrez titre</label>
<input type='text' name='titre'>
<label for='contenu'>Entrez contenu</label>
<input type='text' name='contenu'>
<input type='submit' value='Ajouter un post'>
</form>
</div>";


if (isset($_GET["archive"])) {
    $billets = $entityManager->getRepository(Billets::class)->findAll();
} else {
    $billets = $entityManager->getRepository(Billets::class)->findBy([], null, 3);
}

echo '<div class="blog-container">';
foreach ($billets as $billet) {
    $contenu = substr($billet->getContenu(), 0, 300) . (strlen($billet->getContenu()) > 300 ? '...' : '');
    echo "<div class='blog-post'>
            <h2>{$billet->getTitre()}</h2>
            <h3>Auteur : {$billet->getAuteur()->getLogin()}</h3>
            <p>{$contenu}</p>
            <a href='index.php?action=billet_affiche&id_billet={$billet->getId()}' class='read-more'>Lire la suite</a>
            <p class='date'>{$billet->getDatePost()->format('Y-m-d H:i:s')}</p>
      </div>";
}
?>

<a href="index.php?action=accueil&archive=on" class="login-button">Afficher le reste</a>

<?php
if (isset($_GET ["archive"])){
    echo"<a href='index.php?action=accueil' class='login-button'>Masquer</a>";
}
?>
</body>
</html>


