<!DOCTYPE html>
<html>

<head>
  <%- include("../../partials/header.ejs") %>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"
      integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
    <script src="https://kit.fontawesome.com/c65a03b059.js" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>

<body>

  <%- include("../../partials/privateNav.ejs") %>

    <div class="columns">
      <div class="column is-3"></div>
      <div class="column is-6">
        <div class="section">
          <div class="block">
            <h1 class="title">Create a Blog Post</h1>
          </div>
          <div class="box" style="padding: 2rem">

            <div class="field">
              <label class="label">Post Title</label>
              <div class="control">
                <input type="text" class="input" placeholder="Enter the blog post's name." id="userPostName">
              </div>
            </div>

            <div class="field">
              <label class="label">Post Image</label>
              <div class="file">
                <label class="file-label">
                  <input class="file-input" type="file" id="userPostImage">
                  <span class="file-cta">
                    <span class="file-icon">
                      <i class="fas fa-upload"></i>
                    </span>
                    <span class="file-label">
                      Choose a fileâ€¦
                    </span>
                  </span>
                </label>
              </div>
            </div>

            <div class="field">
              <label class="label">Alternative Text for Image</label>
              <div class="control">
                <input id="userPostImageAlt" type="text" class="input"
                  placeholder="If the image is unable to load, or if the user requires accessibility assistance, this text will be shown.">
              </div>
            </div>

            <div class="field">
              <label class="label">Post Content (in raw Markdown)</label>
              <textarea class="textarea" placeholder="Your blog post's content!" id="userPostContent"
                rows="12"></textarea>
            </div>

            <button class="button is-dark" id="createPostButton" onclick="attemptPostCreate()"
              style="margin-top: 1rem">Create Post</button>

          </div>
        </div>
      </div>
      <div class="column is-3"></div>
    </div>

</body>

<script>
  async function attemptPostCreate() {

    let userPostName = $('#userPostName').val();
    let userPostContent = $('#userPostContent').val();
    let userPostImageAlt = $('#userPostImageAlt').val();
    let postImageLink;
    const fileInput = document.getElementById('userPostImage').files[0];
    //const userProvidedFile = fileInput.files;

    if (!fileInput) {
      console.log("No files provided!");
      postImageLink = "https://interlakedebateblogimages.s3-us-west-2.amazonaws.com/interlake+entrance.jpeg";
      //$('#createPostButton').after('<article id="creationError" class="message is-danger is-normal post-error" style="margin-top: 2rem"> <div class="message-header"><p>User Form Error</p></div><div class="message-body">Don\'t forget to upload an image!.</div></article>');
    }
    console.log("alt text: " + userPostImageAlt);

    axios.post('/private/blog/create', {
      postTitle: userPostName,
      postContent: userPostContent,
      postDate: new Date().toISOString,
      postImageAlt: userPostImageAlt,
      postImage: postImageLink
    }).then(response => {

      $('#createPostButton').after('<article id="creationSuccess" class="message is-success is-normal" style="margin-top: 2rem"> <div class="message-header"><p>Post Successfully Created</p></div><div class="message-body">It\'s now visible on the public Interlake Speech and Debate site!</div></article>');

      $('#createPostButton').prop("disabled", true);
      $('.post-error').remove();

    }).catch(error => {

      $('#createPostButton').after('<article id="creationError" class="message is-danger is-normal post-error" style="margin-top: 2rem"> <div class="message-header"><p>Server Error</p></div><div class="message-body">Our servers had some unknown issues. Please try again in a few minutes. If the issue persists, contact support.</div></article>');

    });

  }
  
</script>

<%- include("../../partials/footer.ejs") %>

</html>