<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
    <link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
    <script src="https://cdn.quilljs.com/1.3.6/quill.js"></script>
  </head>
  <body>
    <h1><%= title %></h1>
    <p>Welcome to <%= title %></p>

    <form action="/submit" method="POST" enctype="multipart/form-data">
      <input type="hidden" name="content" id="content">
      <div id="editor"></div>
      <input type="file" name="image" id="image">
      <button type="submit">Submit</button>
    </form>

    <script>
      var toolbarOptions = [
        ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
        ['blockquote', 'code-block'],

        [{ 'header': 1 }, { 'header': 2 }],               // custom button values
        [{ 'list': 'ordered'}, { 'list': 'bullet' }],
        [{ 'script': 'sub'}, { 'script': 'super' }],      // superscript/subscript
        [{ 'indent': '-1'}, { 'indent': '+1' }],          // outdent/indent
        [{ 'direction': 'rtl' }],                         // text direction

        [{ 'size': ['small', false, 'large', 'huge'] }],  // custom dropdown
        [{ 'header': [1, 2, 3, 4, 5, 6, false] }],

        [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
        [{ 'font': [] }],
        [{ 'align': [] }],
        ['link', 'image', 'video'],
        ['clean'],
                                 // remove formatting button
      ];

      var quill = new Quill('#editor', {
        modules: {
          toolbar: toolbarOptions,
          imageResize: {
            displaySize: true
          },
          imageDrop: true
        },
        theme: 'snow'
      });

      var form = document.querySelector('form');
      var contentInput = document.querySelector('#content');
      var imageInput = document.querySelector('#image');

      form.onsubmit = function() {
        contentInput.value = quill.root.innerHTML;
      };

      imageInput.addEventListener('change', function() {
        var file = imageInput.files[0];
        var formData = new FormData();
        formData.append('image', file);

        fetch('/upload', {
          method: 'POST',
          body: formData
        })
        .then(response => response.json())
        .then(data => {
          var range = quill.getSelection();
          quill.insertEmbed(range.index, 'image', data.url);
        })
        .catch(error => console.error(error));
      });
    </script>

    
        

      var form = document.querySelector('form');
      var contentInput = document.querySelector('#content');

      form.onsubmit = function() {
        contentInput.value = quill.root.innerHTML;
      };
    </script>

    
  </body>
</html>
