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

if($_SESSION['role'] < 1) {
    header("Location: login.php");
    http_response_code(404);
    die();
}

$filename = rand(100000, 999999);

while(file_exists($wwwroot . "/content/journal/" . $filename . ".md")) {
    $filename = rand(100000, 999999);
}

if(isset($_POST['article-content'])) {
    file_put_contents($wwwroot . "/content/journal/" . $filename . ".md", nl2br($_POST['article-content']));

    $bdd = connect($dbhost, $dbname, $dbuser, $dbpass);
    $upload = $bdd->prepare("INSERT INTO articles (ID, titre, auteur, resume, classification) VALUES (:filename, :titre, :auteur, :resume, :classification)");
    $upload->bindParam(':filename', $filename);
    $upload->bindParam(':titre', htmlspecialchars($_POST['article-titre']));
    $upload->bindParam(':auteur', $_SESSION['userid']);
    $upload->bindParam(':resume', htmlspecialchars($_POST['article-resume']));
    $upload->bindParam(':classification', $_POST['classification']);
    if($upload->execute()) {
        $status = "Article Publié";
    } else {
        $status = "Erreur SQL";
    }
    header("Location: editpage.php?article=" . $filename);
    exit();
}

?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta name="robots" content="noindex">
    <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>
    <style>
        textarea {
            width: 100%;
            min-height: 50em;
        }
    </style>
</head>
<body class="body">
    <header>
        <div class="pancontent">
            <div class="athena-container">
                <a href="<?=$root?>">
                    <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 class="status"><?=$status?></div>
                <form action="?article=<?=$filename?>" method="post">
                    <div>
                        <div><div><h1>Publier un article</h1></div>
                        <div><input type="text" name="article-titre" placeholder="Titre"></div>
                        <div><textarea name="article-resume" placeholder="Résumé de l'article (200 cacactères) ..." class="article-resume-input" maxlength="200"></textarea></div>
                        <div><textarea name="article-content" placeholder="Contenu de l'article (MarkDown) ..."></textarea></div>
                        <div>
                            <label for="classification">Classification : </label>
                            <select name="classification" required>
                                <?php foreach($classifications as $id => $nom) { echo '<option value="' . $id .'">' . $nom . '</option>'; }?>
                            </select>
                        </div>
                        <div><input type="submit" value="Publier"></div>
                    </div>
                </form>
            </div>
        </div>
    </main>
    <footer>
        <div class="content">
            <div>
                <div><?=$copyright?></div>
            </div>
        </div>
    </footer>
</body>
</html>