<div id="admin-token-modal" class="modal-overlay" role="dialog" aria-labelledby="admin-modal-title" aria-modal="true" hidden>
  <div class="modal-backdrop" onclick="closeAdminTokenModal()"></div>
  <div class="modal-content">
    <h3 id="admin-modal-title">관리자 모드</h3>
    <p class="modal-desc">강의 등록을 위해 관리자 토큰을 입력한 뒤 활성화해주세요.</p>
    <p id="admin-token-error" class="admin-error" style="display:none; margin-bottom:12px">입력한 토큰이 올바르지 않습니다. 다시 확인해주세요.</p>
    <form id="admin-token-form" class="admin-token-form">
      <input type="password" id="admin-token-input" name="token" placeholder="관리자 토큰" required autocomplete="off" />
      <div class="modal-actions">
        <button type="submit" id="admin-token-submit">활성화</button>
        <button type="button" class="ghost" onclick="closeAdminTokenModal()">닫기</button>
      </div>
    </form>
  </div>
</div>
<script>
  (function moveAdminModalToBody() {
    function run() {
      var m = document.getElementById("admin-token-modal");
      if (m && m.parentNode && m.parentNode !== document.body) {
        document.body.appendChild(m);
      }
    }
    if (document.readyState === "loading") {
      document.addEventListener("DOMContentLoaded", run);
    } else {
      run();
    }
  })();
  function openAdminTokenModal() {
    var m = document.getElementById("admin-token-modal");
    m.hidden = false;
    document.body.style.overflow = "hidden";
    document.getElementById("admin-token-error").style.display = "none";
    document.getElementById("admin-token-input").value = "";
  }
  function closeAdminTokenModal() {
    document.getElementById("admin-token-modal").hidden = true;
    document.body.style.overflow = "";
  }
  function initAdminTokenForm() {
    var form = document.getElementById("admin-token-form");
    if (form) {
      form.addEventListener("submit", function(e) {
        e.preventDefault();
        var input = document.getElementById("admin-token-input");
        var token = (input && input.value || "").trim();
        var errEl = document.getElementById("admin-token-error");
        var btn = document.getElementById("admin-token-submit");
        if (!token) {
          if (errEl) errEl.style.display = "block";
          return;
        }
        if (btn) btn.disabled = true;
        if (errEl) errEl.style.display = "none";
        fetch("/api/admin/validate-token", {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({ token: token })
        })
        .then(function(r) { return r.json(); })
        .then(function(data) {
          if (data && data.valid) {
            window.location.href = "/admin?token=" + encodeURIComponent(token);
          } else {
            if (errEl) errEl.style.display = "block";
          }
        })
        .catch(function() {
          if (errEl) {
            errEl.textContent = "토큰 검증 중 오류가 발생했습니다.";
            errEl.style.display = "block";
          }
        })
        .finally(function() {
          if (btn) btn.disabled = false;
        });
      });
    }
  }
  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", initAdminTokenForm);
  } else {
    initAdminTokenForm();
  }
</script>
