<?php
require "../include/variables.php";
require "../include/functions.php";

?>
<!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="./src/css/style.css">
    <style>
    @media (prefers-color-scheme: dark) {
        .body {
            <?=$theme_dark?>
        }
    } 
    @media (prefers-color-scheme: light) {
        .body {
            <?=$theme_light?>
            }
        }
    </style>
    <link rel="icon" href="./src/img/favicon.ico">
    <title><?=$title?></title>
</head>
<body class="body">
    <header>
        <div class="pancontent">
            <div class="athena-container">
                <a href="<?=$root?>" class="athena-link">
                    <img src="./src/img/athena-mono.png" class="athena">
                </a>
            </div>
            <div class="content">
                <div>
                    <div class="main-title">
                        <div class="title"><?=$header_title?></div>
                        <div class="subtitle"><?=$header_subtitle?></div>
                    </div>
                </div>
            </div>
        </div>
    </header>
    <nav>
        <div class="pancontent">
            <?php nav($nav);?>
        </div>
    </nav>
    <main>
        <div class="content">
            <div>
                <div>
                    <?php
                        $markdownContent = file_get_contents('../content/index.md');

                        require_once '../include/parsedown.php';
                        $parsedown = new Parsedown();
                        echo $parsedown->text($markdownContent);
                    ?>
                </div>
                <div class="articles"><?php
                $bdd = connect($dbhost, $dbname, $dbuser, $dbpass);
                $search = isset($_GET['search']) ? "%" . htmlspecialchars($_GET['search']) . "%" : "%%";
                $req = $bdd->prepare("SELECT articles.ID, articles.titre, articles.date, articles.image, articles.resume, users.username, users.display_name FROM articles JOIN users ON articles.auteur = users.ID WHERE articles.classification <= :accreditation ORDER BY date DESC LIMIT 3");
                $req->bindParam(":accreditation", $_SESSION['accreditation']);
                $req->execute();
                $resultat = $req->fetchAll(PDO::FETCH_ASSOC);

                foreach($resultat as $row) {
                    $date = strtotime($row['date']);
                    echo '<div class="article-preview">';
                    echo '<div class="article-illustration">';
                    echo '<a href="news.php?article=' . $row['ID'] . '" class="article-link"><img src="' . $row['image'] . '" class="article-miniature"></a>';
                    echo '</div>';
                    echo '<div class="article-details">';
                    echo '<div class="article-data">n° ' . $row['ID'] . ' | ' . date('d/m/Y', $date) . ' | '. '<a href="user.php?user=' . $row['username'] . '">' . $row['display_name'] . '</a></div>';
                    echo '<a href="news.php?article=' . $row['ID'] . '" class="article-link">';
                    echo '<div class="article-titre">' . $row['titre'] . '</div>';
                    echo '</a>';
                    echo '<div class="article-resume">' . $row['resume'] . '</div>';
                    echo '</div>';
                    echo '</div> ';
                }
                ?>
                </div>
            </div>
        </div>
    </main>
    <footer>
        <div class="content">
            <div>
                <div><?=$copyright?></div>
            </div>
        </div>
    </footer>
</body>
</html>