<?php

try {
    $error = false;
    $dbh = new PDO('mysql:host=saeweb2022.souidi.fr;dbname=saeweb2022', "saeweb2022");

    $url = parse_url($_SERVER["REQUEST_URI"])["path"];

    if (str_ends_with($url, "/")) {
        $url = substr($url, 0, -1);
    }

    $url = explode("/", $url);

    if (end($url) == "blog") {
        $mode = "list";
        $articles = array();

        foreach($dbh->query('SELECT article.title, article.created_at, article.slug, article.excerpt, employee.name AS author, employee.slug AS authorUrl FROM article JOIN employee ON article.author=employee.id ORDER BY created_at DESC;') as $row) {
            $articles[] = $row;
        }
    } else {
        $mode = "view";
        $sth = $dbh->prepare("SELECT article.title, article.created_at, article.modified_at, article.slug, article.content, article.excerpt, employee.name AS author, employee.slug AS authorUrl FROM article JOIN employee ON article.author=employee.id AND article.slug = ?;");
        $sth->execute([end($url)]);
        $article = $sth->fetch(PDO::FETCH_ASSOC);

        if (empty($article)) {
            http_response_code(404);
            $error = "notfound";
        }

        $sth = null;
    }

    $dbh = null;
} catch (PDOException $e) {
    http_response_code(503);
    $error = "db";
    die($e->getMessage());
}
?>

<!DOCTYPE html>
<html lang="fr">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="icon" href="../assets/img/favicon_dark.ico" media="(prefers-color-scheme: light)">
        <link rel="icon" href="../assets/img/favicon_light.ico" media="(prefers-color-scheme: dark)">
        <?php
            if ($mode == 'list') {
                echo '<title>Blog - Elite Solar</title>';
                echo '<meta name="description" content="Le blog d\'Elite Solar">';
                echo '<meta property="og:title" content="Blog - Elite Solar">';
                echo '<meta property="og:description" content="Le blog d\'Elite Solar">';
                echo '<meta property="og:type" content="website">';
            }
            elseif ($mode == 'view') {
                echo '<title>' . $article['title'] . ' - Elite Solar</title>';
                echo '<meta name="description" content="' . $article['excerpt'] . '">';
                echo '<meta property="og:title" content="' . $article['title'] . ' - Elite Solar">';
                echo '<meta property="og:description" content="' . $article['excerpt'] . '">';
                echo '<meta name="author" content="' . $article['author'] . '">';
                echo '<meta property="og:author" content="' . $article['author'] . '">';
                echo '<meta property="article:author" content="' . $article['author'] . '">';
                echo '<meta property="og:type" content="article">';
                echo '<meta property="article:published_time" content="' . $article['created_at'] . '">';
                echo '<meta property="article:modified_time" content="' . $article['modified_at'] . '">';

            }
        ?>
        <meta property="og:site_name" content="Elite Solar">
        <meta property="og:locale" content="fr_FR">
        <link rel="stylesheet" href="../assets/css/style.css">
        <link rel="stylesheet" href="../assets/css/header.css">
        <link rel="stylesheet" href="../assets/css/footer.css">
        <link rel="stylesheet" href="../assets/css/blog.css">
        <script src="../assets/js/header.js"></script>
    </head>
    <body>
    <header>
        <div>
            <a href="../"><img id="logo" src="../assets/img/logo_noir.png" height="60" alt="Elite Solar"></a>
            <img id="burger-menu" alt="Menu" onclick="burgerMenu()" src="../assets/img/open-menu.svg">
            <nav>
                <ul id="menu" class="invisible">
                    <li><a href="../">Accueil</a></li>
                    <li><a href="../about">Qui sommes-nous ?</a></li>
                    <li><a href="../product">Produit</a></li>
                    <li><a href="../contact">Contact</a></li>
                    <li><a href="../faq">FAQ</a></li>
                </ul>
            </nav>
        </div>
    </header>
    <main>
<?php
if ($error == "db") {
    echo '<nav class="crumbs">
                        <ol>
                            <li class="crumb"><a href="../">Accueil</a></li>
                            <li class="crumb">Blog</li>
                        </ol>
                    </nav>
                    <p>Une erreur est survenue lors du chargement de la page.</p>';
} elseif ($error == "notfound") {
    echo '<nav class="crumbs">
            <ol>
                <li class="crumb"><a href="../">Accueil</a></li>
                <li class="crumb"><a href="./">Blog</a></li>
                <li class="crumb">Article</li>
            </ol>
        </nav>' .
        "<h1>Article introuvable</h1>
          <p>L'article que vous cherchez à atteindre ne peut pas être trouvée.
          Il est possible qu'il ait été supprimé ou déplacé. Si vous avez saisi manuellement son adresse,
          veuillez vérifier une éventuelle erreur de saisie.</p><br>
          <p><a href='../../'>Retour à la page d'accueil</a></p>";
} else {
    if ($mode == 'list') {
        echo '<nav class="crumbs">
                <ol>
                    <li class="crumb"><a href="../">Accueil</a></li>
                    <li class="crumb">Blog</li>
                </ol>
            </nav>' .
            "<h1>Blog</h1><br><div>";
        echo '<ul id="articles">';
        foreach ($articles as $article) {
            $date = new DateTime($article['created_at']);
            echo '<li><a href="../blog/' . $article["slug"] . '">' . $article["title"] . '</a><br>
                              Publié le ' . $date->format('d/m/Y') . ' par <a href="../team/' . $article["authorUrl"] . '">' . $article["author"] . '</a><br>
                              Extrait : ' . $article["excerpt"] . '</li>';
        }
        echo '</ul></div>';
    } elseif ($mode == 'view') {
        $date = new DateTime($article['created_at']);
        echo '<nav class="crumbs">
                <ol>
                    <li class="crumb"><a href="../">Accueil</a></li>
                    <li class="crumb"><a href="./">Blog</a></li>
                    <li class="crumb">' . $article["title"] . '</li>
                </ol>
            </nav>' .
            "<h1>" . $article["title"] . "</h1><br>
            <p>Publié le " . $date->format('d/m/Y') . " par <a href='../team/" . $article["authorUrl"] . "'>" . $article["author"] . "</a></p><br>" . $article["content"];
    }
}
?>
        </main>
        <button onclick="topFunction()" id="backToTop" title="Revenir au début de la page">⬆️</button>
        <footer>
            <div class="content">
                <div class="text">
                    © <?= date("Y") ?> Elite Solar
                    <a href="legal">Mentions légales</a>
                    <a href="sitemap">Plan du site</a>
                </div>
                <div class="social">
                    <a target="_blank" href="https://linkedin.com"><img src="../assets/img/linkedin.svg" width="25" height="25" alt="LinkedIn"></a>
                    <a target="_blank" href="https://instagram.com/elitesolar77"><img src="../assets/img/instagram.svg" width="25" height="25" alt="Instagram"></a>
                    <a target="_blank" href="https://www.facebook.com/profile.php?id=100089332237449"><img src="../assets/img/facebook.svg" width="25" height="25" alt="Facebook"></a>
                    <a target="_blank" href="https://twitter.com/EliteSolar77"><img src="../assets/img/twitter.svg" width="25" height="25" alt="Twitter"></a>
                </div>
            </div>
        </footer>
        <script src="../assets/js/script.js"></script>
    </body>
</html>