<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Expires", "0");
%>
<!doctype html>
<html lang="ko">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=1600" />
  <title>OpenAPI Spec 에디터</title>

  <%-- Tailwind (벤더된 Play CDN JS, 오프라인) --%>
  <script src="<c:url value='/plugins/openapi/tailwind.js'/>"></script>
  <script>
    tailwind.config = { theme: { extend: { colors: { brand: {
      50:'#eff6ff', 100:'#dbeafe', 500:'#3b82f6', 600:'#2563eb', 700:'#1d4ed8'
    } } } } };
  </script>

  <%-- Swagger UI 5 (벤더) --%>
  <link rel="stylesheet" href="<c:url value='/plugins/swaggerUI/swagger-ui.css'/>" />
  <script src="<c:url value='/plugins/swaggerUI/swagger-ui-bundle.js'/>"></script>
  <script src="<c:url value='/plugins/swaggerUI/swagger-ui-standalone-preset.js'/>"></script>
  <script src="<c:url value='/plugins/swaggerUI/djb-swagger-i18n.js'/>" charset="UTF-8"></script>

  <%-- js-yaml (벤더) --%>
  <script src="<c:url value='/plugins/openapi/js-yaml.min.js'/>"></script>

  <%-- CodeMirror (YAML 편집기 — Monaco 대체, admin addon) --%>
  <link rel="stylesheet" href="<c:url value='/addon/codemirror/lib/codemirror.css'/>" />
  <script src="<c:url value='/addon/codemirror/lib/codemirror.js'/>"></script>
  <script src="<c:url value='/addon/codemirror/mode/yaml/yaml.js'/>"></script>
  <script src="<c:url value='/addon/codemirror/mode/javascript/javascript.js'/>"></script>

  <%-- jQuery + Summernote(lite, 무-Bootstrap) — 설명 리치 에디터 --%>
  <link rel="stylesheet" href="<c:url value='/addon/summernote/summernote-lite.css'/>" />
  <%-- 개발자포탈과 동일한 에디터 콘텐츠 스타일(.editor-content, Noto Sans KR 16px) --%>
  <link rel="stylesheet" href="<c:url value='/js/djb/apispec/editor-content.css'/>" />
  <script src="<c:url value='/js/jquery-1.12.1.min.js'/>"></script>
  <script src="<c:url value='/addon/summernote/summernote-lite.min.js'/>"></script>
  <script src="<c:url value='/addon/summernote/lang/summernote-ko-KR.js'/>"></script>

  <link rel="stylesheet" href="<c:url value='/js/djb/apispec/styles.css'/>" />
  <style>
    /* 페이지 자체 세로 스크롤 제거 — 뷰포트 고정, 내부 패널만 스크롤 */
    html, body { height: 100%; margin: 0; overflow-x: auto; overflow-y: hidden; }
    #djb-wrap { height: 100vh; display: flex; flex-direction: column; }
    #djb-wrap > header, #djb-wrap > nav { flex: 0 0 auto; }
    #body { flex: 1 1 auto; min-height: 0 !important; overflow: hidden; }
    #form-panel { display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
    #form-content { flex: 1 1 auto; min-height: 0; overflow-y: auto; }
    #preview-panel { display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
    #preview-swagger, #preview-monaco { flex: 1 1 auto; min-height: 0; height: auto !important; overflow: auto; }
    #preview-monaco .CodeMirror { height: 100%; }
    /* 패널 내부 sticky 는 flex 행으로 (스크롤 컨테이너는 form-content / preview) */
    #form-panel > .sticky, #preview-panel > .sticky { position: static !important; }
  </style>

  <script>
    window.DJB_CTX = {
      eaiSvcName: "${param.eaiSvcName}",
      apiName: "${param.eaiSvcDesc}",
      detailUrl: "<c:url value='/onl/transaction/apim/djbApiSpecMan.json'/>?cmd=DETAIL_SPEC&serviceType=APIGW",
      saveUrl:   "<c:url value='/onl/transaction/apim/djbApiSpecMan.json'/>?cmd=SAVE&serviceType=APIGW",
      swaggerBase: "<c:url value='/plugins/swaggerUI/'/>",
      gwAddress: "${gwAddress}",
      useProxy: ${useProxy},
      tokenUseProxy: ${tokenUseProxy},
      orgPopupUrl: "<c:url value='/onl/transaction/apim/apiSpecMan.view'/>",
      groupPopupUrl: "<c:url value='/onl/transaction/apim/djbApiSpecMan.view'/>?cmd=GROUP_POPUP",
      jsonUrl: "<c:url value='/onl/transaction/apim/djbApiSpecMan.json'/>"
    };
  </script>
</head>
<body class="bg-slate-50 text-slate-800 antialiased" style="min-width:1600px;">

  <div id="djb-wrap" class="mx-auto" style="width:1600px;">

    <!-- ===== Header ===== -->
    <header class="h-14 flex items-center justify-between px-6 bg-white border-b border-slate-200 sticky top-0 z-30">
      <div class="flex items-center gap-3">
        <div class="font-semibold text-slate-900">OpenAPI Spec 에디터</div>
        <span class="text-slate-300">|</span>
        <span class="text-sm text-slate-500">인터페이스:</span>
        <span class="text-sm font-medium text-slate-800" id="hdr-project-name">${param.eaiSvcName}</span>
        <span class="ml-2 px-2 py-0.5 text-[11px] rounded-full bg-slate-100 text-slate-600 border border-slate-200" id="hdr-version">v1.0.0</span>
        <span id="hdr-source" class="hidden ml-1 px-2 py-0.5 text-[11px] rounded-full bg-emerald-50 text-emerald-700 border border-emerald-200"></span>
      </div>
      <div class="flex items-center gap-2">
        <button id="btn-newwin" class="hidden px-3 py-1.5 text-sm rounded border border-slate-300 bg-white hover:bg-slate-50">새창으로 띄우기</button>
        <button id="btn-regen" class="px-3 py-1.5 text-sm rounded border border-slate-300 bg-white hover:bg-slate-50">자동 생성(초기화)</button>
        <button id="btn-save" class="px-3 py-1.5 text-sm rounded bg-emerald-600 text-white hover:bg-emerald-700">저장</button>
        <div class="relative">
          <button id="btn-export" class="px-3 py-1.5 text-sm rounded bg-brand-600 text-white hover:bg-brand-700">내보내기 ▾</button>
          <div id="export-menu" class="hidden absolute right-0 mt-1 w-44 bg-white border border-slate-200 rounded shadow-lg z-40">
            <button data-export="yaml" class="block w-full text-left px-3 py-2 text-sm hover:bg-slate-50">YAML 다운로드</button>
            <button data-export="json" class="block w-full text-left px-3 py-2 text-sm hover:bg-slate-50">JSON 다운로드</button>
          </div>
        </div>
        <button id="btn-close" class="px-3 py-1.5 text-sm rounded border border-slate-300 bg-white hover:bg-slate-50">닫기</button>
      </div>
    </header>

    <!-- ===== Stepper ===== -->
    <nav class="bg-white border-b border-slate-200 sticky z-20" style="top:56px;">
      <div class="h-1 bg-slate-100">
        <div id="stepper-progress" class="h-1 bg-brand-500 transition-all duration-300" style="width:16.66%"></div>
      </div>
      <ol id="stepper" class="grid grid-cols-5 px-2"></ol>
    </nav>

    <!-- ===== Body ===== -->
    <main id="body" class="flex" style="min-height: calc(100vh - 56px - 72px);">
      <section id="form-panel" class="bg-white border-r border-slate-200 transition-all duration-300" style="width:960px;">
        <div class="flex items-center justify-between px-6 py-3 border-b border-slate-100 sticky bg-white z-10" style="top:128px;">
          <div class="flex items-baseline gap-3">
            <span class="text-xs uppercase tracking-wide text-slate-400">Step <span id="form-step-num">1</span> / 5</span>
            <h2 class="text-base font-semibold text-slate-800" id="form-step-title">기본정보</h2>
          </div>
          <button id="btn-toggle-preview-from-form" class="hidden text-xs px-2 py-1 rounded border border-slate-200 text-slate-600 hover:bg-slate-50">▶ 미리보기 다시 열기</button>
        </div>
        <div id="form-content" class="px-6 py-5"></div>
        <div class="sticky bottom-0 bg-white border-t border-slate-200 px-6 py-3 flex items-center justify-between">
          <button id="btn-prev" class="px-4 py-2 text-sm rounded border border-slate-300 bg-white hover:bg-slate-50 disabled:opacity-40 disabled:cursor-not-allowed">◀ 이전</button>
          <div class="text-xs text-slate-500" id="footer-hint">필수 항목을 채우고 다음 단계로 이동하세요</div>
          <button id="btn-next" class="px-4 py-2 text-sm rounded bg-brand-600 text-white hover:bg-brand-700">다음 ▶</button>
        </div>
      </section>

      <aside id="preview-panel" class="bg-slate-50 transition-all duration-300" style="width:640px;">
        <div class="flex items-center justify-between px-4 py-2 border-b border-slate-200 bg-white sticky z-10" style="top:128px;">
          <div class="flex items-center gap-1">
            <button data-ptab="swagger" class="preview-tab px-3 py-1.5 text-sm rounded-t border-b-2 border-brand-600 text-brand-700 font-medium">Swagger UI</button>
            <button data-ptab="yaml-ro" class="preview-tab px-3 py-1.5 text-sm rounded-t border-b-2 border-transparent text-slate-600 hover:text-slate-800">YAML 보기</button>
            <button data-ptab="yaml-edit" class="preview-tab px-3 py-1.5 text-sm rounded-t border-b-2 border-transparent text-slate-600 hover:text-slate-800">스펙 편집</button>
          </div>
          <button id="btn-toggle-preview" class="text-xs px-2 py-1 rounded border border-slate-200 text-slate-600 hover:bg-slate-100">패널 숨기기 ▶</button>
        </div>
        <div id="yaml-edit-bar" class="hidden flex items-center justify-between bg-amber-50 border-b border-amber-200 px-4 py-2 text-xs">
          <span class="text-amber-800">YAML 을 직접 편집한 뒤 [Form 에 적용] 을 눌러 양식에 동기화하세요.</span>
          <div class="flex items-center gap-2">
            <button id="btn-yaml-revert" class="px-2 py-1 rounded border border-amber-300 bg-white hover:bg-amber-100">변경 취소</button>
            <button id="btn-yaml-apply"  class="px-2 py-1 rounded bg-amber-600 text-white hover:bg-amber-700">Form 에 적용</button>
          </div>
        </div>
        <div id="preview-swagger" class="overflow-auto" style="height: calc(100vh - 56px - 72px - 41px);"></div>
        <div id="preview-monaco" class="hidden" style="height: calc(100vh - 56px - 72px - 41px);"></div>
      </aside>
    </main>
  </div>

  <div id="toast" class="fixed bottom-5 left-1/2 -translate-x-1/2 hidden px-4 py-2 rounded bg-slate-900 text-white text-sm shadow-lg z-50"></div>

  <!-- Security scheme modal -->
  <div id="sec-modal" class="hidden fixed inset-0 z-50 bg-slate-900/40 flex items-center justify-center">
    <div class="bg-white rounded-lg shadow-xl w-[640px] max-h-[80vh] overflow-auto">
      <div class="flex items-center justify-between px-5 py-3 border-b border-slate-200">
        <h3 class="font-semibold text-slate-800">보안 스킴 추가</h3>
        <button class="text-slate-400 hover:text-slate-700" data-sec-close>✕</button>
      </div>
      <div class="p-5" id="sec-modal-body"></div>
      <div class="px-5 py-3 border-t border-slate-200 flex justify-end gap-2 bg-slate-50">
        <button class="px-3 py-1.5 text-sm rounded border border-slate-300 bg-white" data-sec-close>취소</button>
        <button id="btn-sec-save" class="px-3 py-1.5 text-sm rounded bg-brand-600 text-white">추가</button>
      </div>
    </div>
  </div>

  <script src="<c:url value='/js/djb/apispec/sample-data.js'/>"></script>
  <script src="<c:url value='/js/djb/apispec/app.js'/>"></script>
</body>
</html>
