<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{layout/default_layout}">
<body>

<section layout:fragment="contentFragment">
  <script th:src="@{/plugins/vs/loader.js}"></script>
  <script>
    require.config({paths: {'vs': '[[@{/plugins/vs}]]'}});
  </script>
  <div class="container-fluid mt-2">
    <div class="toast" role="alert" aria-live="assertive" aria-atomic="true" style="position: absolute; top: 0; left: 0;">
      <div class="toast-header">
        <strong class="mr-auto">알림</strong>
        <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
      </div>
      <div class="toast-body">
      </div>
    </div>
    <form name="queueHandlerForm" id="queueHandlerForm"
          th:action="${queueHandler.queueName == null}?@{/mgmt/queues/register.do}:@{/mgmt/queues/update.do}"
          th:object='${queueHandler}' method="post">
      <div class="card col-md">
        <div class="card-header">
          <h3 th:if="${queueHandler.queueName == null}">Queue Handler 등록</h3>
          <h3 th:if="${queueHandler.queueName != null}">Queue Handler 수정</h3>
        </div>
        <div class="card-body">
          <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}">

          <!-- Queue Name field -->
          <div class="form-floating mt-2">
            <input type="text" class="form-control"
                   th:classappend="${#fields.hasErrors('queueName')}? 'is-invalid'"
                   th:field="*{queueName}" required/>
            <label for="queueName" class="form-label must">Queue Name</label>
            <th:block th:each="err : ${#fields.errors('queueName')}">
              <div th:text="${err}" class="invalid-feedback"></div>
            </th:block>
          </div>

          <!-- Handler Script -->
          <div class="card mt-3">
            <div class="card-header">Handler Script</div>
            <div class="card-body">
              <div id="handler_script_editor" style="width:100%;height:400px;border:1px solid grey;"></div>
              <textarea class="form-control" style="display: none;"
                        th:field="*{handlerScript}" name="handlerScript"></textarea>
            </div>
          </div>

        </div>
        <div class="card-footer">
          <button type="button" class="btn btn-primary btn_list">
            <i class="fa-solid fa-list"></i> 목록
          </button>
          <button type="button" class="btn btn-primary btn_register">
            <i class="fa-sharp fa-solid fa-floppy-disk"></i> 저장
          </button>
        </div>
      </div>
    </form>
  </div>
</section>
</body>

<th:block layout:fragment="contentScript">
  <script>
    $(function () {
      let handlerScriptEditor;

      // Initialize Monaco Editor
      require(['vs/editor/editor.main'], function () {
        handlerScriptEditor = monaco.editor.create(document.getElementById('handler_script_editor'), {
          value: document.querySelector('textarea[name="handlerScript"]').value || '',
          language: 'javascript',
          theme: 'vs-light',
          automaticLayout: true
        });

        handlerScriptEditor.onDidChangeModelContent(function (e) {
          document.querySelector('textarea[name="handlerScript"]').value = handlerScriptEditor.getValue();
        });
      });

      let fnRegister = document.querySelector(".btn_register");

      fnRegister.addEventListener("click", function (event) {
        let form = document.getElementById("queueHandlerForm");
        // Update the hidden textarea with current editor content
        document.querySelector('textarea[name="handlerScript"]').value = handlerScriptEditor.getValue();

        if (!form.checkValidity()) {
          event.preventDefault()
          event.stopPropagation()
        } else {
          form.submit();
        }
        form.classList.add('was-validated');
      });

      let fnList = document.querySelector(".btn_list");

      fnList.addEventListener("click", function () {
        location.href = "[[@{/mgmt/queues/list_view.do}]]";
      });
    });
  </script>
</th:block>
</html>
