<?php
require_once 'functions/init.php';
require_once 'functions/functions.php';
include_once 'functions/article_functions.php';

if (!empty($_POST)) {
    if (isset($_POST['article_id'])) {
        $tags = [];
        for ($i = 0; $i < count($_POST['tags']); $i++)
            $tags[] = $_POST['tags'][$i];

        update_article($_POST['article_id'], $_POST['title'], $_POST['excerpt'], $tags, $_POST['markdown']);
        header('Location: articles.php?article_id=' . $_POST['article_id']);
    } else {
        $id = create_article($_COOKIE['user_id'], $_POST['title'], $_POST['excerpt'], (isset($_POST['tags']) ? $_POST['tags'] : []), $_POST['markdown']);
        header('Location: articles.php?article_id=' . $id);
    }
}

if (!isset($_COOKIE['role_id']) || $_COOKIE['role_id'] == 4)
    header('Location: articles.php');

include_once 'components/head.php';

if (isset($_GET['article_id'])) {
    $article_id = $_GET['article_id'];

    $conn = get_connection();
    $stmt = $conn->prepare('SELECT * FROM article WHERE article_id = ?');
    $stmt->bind_param('i', $article_id);
    $stmt->execute();
    $article = $stmt->get_result()->fetch_assoc();

    $stmt = $conn->prepare('SELECT tag_id FROM article_tags WHERE article_id = ?');
    $stmt->bind_param('i', $article_id);
    $stmt->execute();
    $result = $stmt->get_result();
    $tags = [];
    while ($row = $result->fetch_assoc())
        $tags[] = $row['tag_id'];

    $title = $article['title'];
    $excerpt = $article['excerpt'];
    $existing_markdown = read_file_one_string($_ENV['ARTICLES_FQ_PATH'] . $article_id . '/article.md');
}

echo '
  <div class="grid-2x1">
';
if (!empty($existing_markdown)) {
    echo form($article_id, $title, $excerpt, $existing_markdown, $tags);
} else {
    echo form();
}

echo preview();

echo '
  </div>
';

include_once 'components/foot.php';

function form($article_id = null, $title = null, $excerpt = null, $existing_markdown = null, $tags = null)
{
    $raw_tags = article_tags();
    $tags_boxes = '
      <div class="checkboxes">
        <label>Tags</label>
    ';

    foreach ($raw_tags as $row) {
        $tags_boxes .= '
          <label class="form_checkbox_container">' . $row['tag'] . '
            <input name="tags[]" value="' . $row['tag_id'] . '" type="checkbox"' . (isset($tags) && in_array($row['tag_id'], $tags) ? ' checked' : '') . '>
            <span class="checkmark"></span>
          </label>
        ';
    }

    $tags_boxes .= '
      </div>
    ';

    $form = '
<div class="full-height">
  <div class="form_card" id="composer">
    <form method="post">
      ' . (isset($article_id) ? '<input type="hidden" name="article_id" value="' . $article_id . '">' : '') . '

      <label for="title">Title</label>
      <input id="title" name="title" placeholder="Article Title" required' . (isset($title) ? ' value="' . $title . '"' : '') . '>
      
      <label for="markdown">Markdown</label>
      <textarea id="markdown" name="markdown" required>' . (isset($existing_markdown) ? $existing_markdown : '') . '</textarea>

      <label for="excerpt">Excerpt</label>
      <input id="excerpt" name="excerpt" placeholder="Article Excerpt"' . (isset($excerpt) ? 'value="' . $excerpt . '"' : '') . '>
' . $tags_boxes . '
      <input class="button" type="submit" value="' . (isset($article_id) ? 'Update Article' : 'Create Article') . '">
    </form>
  </div>
</div>
';

    return $form;
}

function preview()
{
    $preview = '
    <div class="full-height">
        <div class="form_card" id="md_preview">
        </div>
    </div>
  ';

    return $preview;
}
