<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Embed collection editor</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
    <link rel="stylesheet" href=
        "https://use.fontawesome.com/releases/v5.6.3/css/all.css"
        integrity=
            "sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/"
            crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">
  </head>
  <body>
    <div class="container">
      <h1>Embed editor</h1>
      <div id='editor_holder'></div>
      <div class="btn-group mt-2">
        <button class="btn btn-success" id='submit'>Submit</button>
        <a class="btn btn-secondary" href="/embeds">Cancel</a>
      </div>
    </div>
    <script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@json-editor/json-editor@latest/dist/jsoneditor.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
    <script>
      fetch("/static/embed.schema.json")
        .then(res => res.json())
        .then(schema => {
          console.log(schema)
          fetch("/static/media/video-embed.json")
            .then(res => res.json())
            .then(embeds => {
              // Initialize the editor with a JSON schema
              var editor = new JSONEditor(
                document.getElementById('editor_holder'), {
                  theme: "bootstrap5",
                  iconlib: "fontawesome5",
                  ajax: true,
                  schema: schema,
                  startval: embeds
                });
              // Hook up the submit button to log to the console
              document.getElementById('submit').addEventListener('click',() => {
                // Get the value from the editor
                fetch("/update-embeds", {
                  method: "POST",
                  body: JSON.stringify(editor.getValue()),
                  headers: {
                      "Content-type": "application/json; charset=UTF-8"
                  }
                }).then((response) => { location.replace('/embeds') });
              });
            });
          });
    </script>
  </body>
</html>