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

if (!empty($_POST)) {
  $id = create_article($_COOKIE['user_id'], $_POST['title'], $_POST['excerpt'], $_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';

echo form();
include_once 'components/foot.php';

function form()
{
  $raw_tags = article_tags();
  $tags = '
      <div class="checkboxes">
        <label>Tags</label>
    ';

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

  $tags .= '
      </div>
    ';

  $form = '
  <div class="form_card">
    <form method="post">
      <label for="title">Title</label>
      <input id="title" name="title" placeholder="Article Title" required>
      
      <label for="markdown">Markdown</label>
      <textarea id="markdown" name="markdown" required>
## Heading
vintagecoding.net uses markdown for articles. This means:
- Simple syntax for formatting
- Pre-built styling
- Markdown can be parsed into HTML.
      </textarea>

      <label for="excerpt">Excerpt</label>
      <input id="excerpt" name="excerpt" placeholder="Article Excerpt">
' . $tags . '
      <input class="button" type="submit" value="Create Article">
    </form>
  </div>
';

  return $form;
}
