<?php
declare(strict_types=1);
/**
 * 임베드(탭/iframe) 전용 레이아웃 — 헤더·사이드바 없이 본문만.
 * /workspace 탭 iframe 안에서 업무 페이지를 중첩 크롬 없이 표시한다.
 *
 * @var string $title
 * @var string $content
 * @var bool   $bare   true면 본문을 카드 래퍼 없이 그대로 출력(대시보드용)
 */
$bare = ! empty($bare);
helper('admin');
$helpUrl = function_exists('manual_help_url_for_path') ? manual_help_url_for_path() : '';
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<title><?= esc($title ?? 'GBLS') ?></title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet"/>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/orioncactus/pretendard@v1.3.9/dist/web/static/pretendard.min.css"/>
<script src="https://cdn.tailwindcss.com?plugins=forms,container-queries"></script>
<script>
tailwind.config = {
  theme: {
    extend: {
      fontFamily: { sans: ['Pretendard', '"Malgun Gothic"', '"Noto Sans KR"', 'sans-serif'] },
      colors: {
        'system-header': '#ffffff', 'title-bar': '#1a2b4b', 'control-panel': '#f8f9fa',
        'btn-search': '#243a5e', 'btn-excel-border': '#28a745', 'btn-excel-text': '#28a745',
        'btn-print-border': '#ced4da', 'btn-exit': '#d9534f',
      },
      fontSize: { 'xxs': '0.65rem' }
    }
  }
}
</script>
<style>
  body { margin: 0; background: #f0f4f8; font-family: 'Pretendard', 'Malgun Gothic', 'Noto Sans KR', sans-serif; letter-spacing: -0.01em; -webkit-font-smoothing: antialiased; }
  .embed-titlebar { display: flex; align-items: center; gap: .5rem; font-size: 1.05rem; font-weight: 800; color: #1a2b4b; letter-spacing: -0.03em; margin: 0 0 0.75rem; }
  .embed-flash { margin-bottom: .75rem; padding: .6rem .9rem; border-radius: 8px; font-size: .8125rem; }
  .embed-flash.ok { background: #ecfdf5; border: 1px solid #a7f3d0; color: #065f46; }
  .embed-flash.err { background: #fef2f2; border: 1px solid #fecaca; color: #991b1b; }
  .data-table { width: 100%; border-collapse: collapse; font-family: 'Pretendard', 'Malgun Gothic', 'Noto Sans KR', sans-serif; }
  .data-table { font-size: 13px; }
  .data-table th, .data-table td { text-align: left; padding: 0.55rem 0.5rem; white-space: nowrap; border: 0; border-bottom: 1px solid #e5e7eb; }
  .data-table thead th { font-size: 0.6875rem; font-weight: 600; color: #6b7280; background: transparent; vertical-align: middle; }
  .data-table tbody td { color: #374151; }
  .data-table tbody tr:last-child td { border-bottom: 0; }
  .data-table tbody tr:hover td { background-color: #f9fafb; }
  @media print { .no-print { display: none !important; } .embed-titlebar { display: none; } }
  /* 화면 설명 드로어(팝업) — 현재 화면 위 오른쪽에 겹쳐 띄움 */
  .help-drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(920px, 92vw); background: #fff; box-shadow: -8px 0 26px rgba(0,0,0,.18); z-index: 9999; display: none; flex-direction: column; }
  .help-drawer.open { display: flex; }
  .help-drawer-head { display: flex; align-items: center; justify-content: space-between; padding: .5rem .75rem; background: #1a2b4b; color: #fff; font-size: .8rem; font-weight: 700; flex-shrink: 0; }
  .help-drawer-head .hd-btns { display: flex; gap: 4px; }
  .help-drawer-head .hd-btn { color: #fff; background: rgba(255,255,255,.14); border: 0; width: 26px; height: 26px; border-radius: 6px; cursor: pointer; font-size: 13px; line-height: 1; display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }
  .help-drawer-head .hd-btn:hover { background: rgba(255,255,255,.28); }
  .help-drawer iframe { flex: 1; width: 100%; border: 0; background: #fff; }
  .help-drawer-grip { position: absolute; left: -4px; top: 0; bottom: 0; width: 8px; cursor: col-resize; }
</style>
</head>
<body>
<div style="padding: 0.875rem 1rem 1.25rem;">
  <?php if (! empty($title) || $helpUrl !== ''): ?>
  <div style="display:flex;align-items:center;justify-content:space-between;gap:.5rem;margin:0 0 .75rem;">
    <h1 class="embed-titlebar" style="margin:0;"><?php if (! empty($title)): ?><i class="fa-solid fa-folder-open" style="color:#007bff;"></i><?= esc($title) ?><?php endif; ?></h1>
    <?php if ($helpUrl !== ''): ?>
    <a href="<?= esc($helpUrl, 'attr') ?>" class="embed-help no-print" style="display:inline-flex;align-items:center;gap:.3rem;padding:.3rem .6rem;border-radius:6px;background:#eef2f7;border:1px solid #dde4ec;color:#1a2b4b;text-decoration:none;font-size:.75rem;font-weight:600;white-space:nowrap;">
      <i class="fa-regular fa-circle-question"></i> 이 화면 설명
    </a>
    <?php endif; ?>
  </div>
  <?php endif; ?>
  <?php if (session()->getFlashdata('success')): ?>
  <div class="embed-flash ok"><?= esc(session()->getFlashdata('success')) ?></div>
  <?php endif; ?>
  <?php if (session()->getFlashdata('error')): ?>
  <div class="embed-flash err"><?= esc(session()->getFlashdata('error')) ?></div>
  <?php endif; ?>
  <?php if ($bare): ?>
    <?= $content ?>
  <?php else: ?>
  <div class="bg-white border border-[#dde4ec] rounded-xl shadow-sm p-4">
    <?= $content ?>
  </div>
  <?php endif; ?>
</div>

<!-- 화면 설명 드로어(팝업) -->
<div id="helpDrawer" class="help-drawer no-print" aria-hidden="true">
  <div class="help-drawer-grip" id="helpDrawerGrip"></div>
  <div class="help-drawer-head">
    <span><i class="fa-regular fa-circle-question"></i> 화면 설명</span>
    <div class="hd-btns">
      <a id="helpDrawerTab" href="#" class="hd-btn" title="탭으로 열기"><i class="fa-solid fa-up-right-from-square"></i></a>
      <button type="button" id="helpDrawerClose" class="hd-btn" title="닫기">&times;</button>
    </div>
  </div>
  <iframe id="helpDrawerFrame" title="화면 설명"></iframe>
</div>

<script>
(function () {
  // 세션 만료 등으로 iframe 안에서 로그인 페이지가 열리면 상위 프레임을 로그인으로 보낸다.
  if (window.top !== window.self && /\/login(\/|$)/.test(location.pathname)) {
    try { window.top.location.href = location.href; } catch (e) {}
  }

  // 방문한 업무 메뉴 경로 기록 (대시보드 "최근 방문 메뉴"용). localStorage 는 동일 출처 탭과 공유된다.
  try {
    var rp = (location.pathname || '').replace(/\/+$/, '') || '/';
    if (rp !== '/' && !/\/login|\/logout|\/register|\/manual/.test(rp)) {
      var RK = 'jrj_recent_menus';
      var ra = JSON.parse(localStorage.getItem(RK) || '[]');
      if (!Array.isArray(ra)) ra = [];
      ra = ra.filter(function (x) { return x && x.p && x.p !== rp; });
      ra.unshift({ p: rp, t: Date.now() });
      localStorage.setItem(RK, JSON.stringify(ra.slice(0, 12)));
    }
  } catch (e) {}

  // 탭(iframe) 안에서의 링크·폼 이동은 항상 embed 유지 → 중첩 헤더/사이드바 방지
  function withEmbed(href) {
    try {
      var u = new URL(href, location.href);
      if (u.origin !== location.origin) return null;
      if (u.searchParams.get('embed') === '1') return null;
      u.searchParams.set('embed', '1');
      return u.href;
    } catch (e) { return null; }
  }
  // "이 화면 설명" → 현재 화면 위 오른쪽 드로어(팝업)로 띄워 동시에 보기
  var drawer = document.getElementById('helpDrawer');
  var dFrame = document.getElementById('helpDrawerFrame');
  function withEmbedUrl(url) { try { var x = new URL(url, location.href); x.searchParams.set('embed', '1'); return x.href; } catch (e) { return url; } }
  function openHelp(url) {
    var u = withEmbedUrl(url);
    if (dFrame.getAttribute('data-src') !== u) {
      // 새 URL → 재로드(로드 후 매뉴얼 자체 스크립트가 강조 실행)
      dFrame.src = u; dFrame.setAttribute('data-src', u);
    } else {
      // 같은 URL → 재로드 안 함. 매뉴얼에 다시 강조하라고 알림(껐다 켜도 강조되도록)
      try { dFrame.contentWindow.postMessage({ type: 'manual-hl' }, location.origin); } catch (e) {}
    }
    var tab = document.getElementById('helpDrawerTab'); if (tab) tab.setAttribute('href', url);
    drawer.classList.add('open');
  }
  function closeHelp() { drawer.classList.remove('open'); }
  document.addEventListener('click', function (e) {
    var h = e.target.closest ? e.target.closest('a.embed-help') : null;
    if (!h) return;
    e.preventDefault(); e.stopPropagation();
    openHelp(h.getAttribute('href'));
  }, true);
  document.getElementById('helpDrawerClose').addEventListener('click', closeHelp);
  document.addEventListener('keydown', function (e) { if (e.key === 'Escape') closeHelp(); });
  // 드로어 헤더의 "탭으로 열기" → 워크스페이스 탭으로(없으면 새 창)
  document.getElementById('helpDrawerTab').addEventListener('click', function (e) {
    e.preventDefault();
    var url = this.getAttribute('href') || '';
    try { if (window.parent && window.parent !== window && typeof window.parent.wsOpenTab === 'function') { window.parent.wsOpenTab(url, '도움말'); closeHelp(); return; } } catch (err) {}
    window.open(url, '_blank');
  });
  // 드로어 폭 드래그 조절
  (function () {
    var grip = document.getElementById('helpDrawerGrip'), dragging = false;
    // 드래그 중 iframe 이 마우스 이벤트를 가로채면(특히 좁힐 때) 멈추므로, 화면 전체 투명 오버레이로 이벤트를 가로챈다.
    var ov = document.createElement('div');
    ov.style.cssText = 'position:fixed;inset:0;z-index:10000;cursor:col-resize;display:none;';
    document.body.appendChild(ov);
    grip.addEventListener('mousedown', function (e) { e.preventDefault(); dragging = true; ov.style.display = 'block'; document.body.style.userSelect = 'none'; });
    document.addEventListener('mousemove', function (e) { if (!dragging) return; var w = window.innerWidth - e.clientX; drawer.style.width = Math.min(window.innerWidth * 0.92, Math.max(300, w)) + 'px'; });
    document.addEventListener('mouseup', function () { if (!dragging) return; dragging = false; ov.style.display = 'none'; document.body.style.userSelect = ''; });
  })();

  // 글씨 크기(zoom) — 상단바에서 조절한 값을 적용. localStorage 공유 + storage 이벤트로 실시간 반영.
  function applyFontScale() {
    try { var s = parseInt(localStorage.getItem('jrj_font_scale') || '100', 10); if (!(s >= 70 && s <= 150)) s = 100; document.documentElement.style.zoom = (s / 100); } catch (e) {}
  }
  applyFontScale();
  window.addEventListener('storage', function (e) { if (e.key === 'jrj_font_scale') applyFontScale(); });

  document.addEventListener('click', function (e) {
    var a = e.target.closest ? e.target.closest('a[href]') : null;
    if (!a || a.classList.contains('embed-help')) return;
    var t = (a.getAttribute('target') || '').toLowerCase();
    if (t && t !== '_self') return;
    if (a.hasAttribute('download')) return;
    var href = a.getAttribute('href') || '';
    if (!href || href.charAt(0) === '#' || /^(javascript:|mailto:|tel:|data:)/i.test(href)) return;
    var ne = withEmbed(href);
    if (ne) a.setAttribute('href', ne);
  }, true);
  document.addEventListener('submit', function (e) {
    var f = e.target;
    if (!f || f.tagName !== 'FORM') return;
    var method = (f.getAttribute('method') || 'get').toLowerCase();
    if (method === 'get') {
      // GET 폼은 제출 시 action의 쿼리스트링이 폼 필드로 대체되므로,
      // action 수정 대신 hidden input 으로 embed=1 을 유지한다(중첩 셸 방지).
      if (!f.querySelector('input[name="embed"]')) {
        var h = document.createElement('input');
        h.type = 'hidden'; h.name = 'embed'; h.value = '1';
        f.appendChild(h);
      }
    } else {
      var ne = withEmbed(f.getAttribute('action') || location.href);
      if (ne) f.setAttribute('action', ne);
    }
  }, true);
  // 표 '번호' 컬럼 역순 채번 (사이트 레이아웃과 동일)
  var run = function () {
    document.querySelectorAll('table').forEach(function (table) {
      var head = table.querySelector('thead tr'); if (!head) return;
      var ths = Array.prototype.slice.call(head.querySelectorAll('th'));
      var col = ths.findIndex(function (th) { return (th.textContent || '').replace(/\s+/g, '').trim() === '번호'; });
      if (col < 0) return;
      var body = table.querySelector('tbody'); if (!body) return;
      var rows = Array.prototype.slice.call(body.querySelectorAll(':scope > tr')).filter(function (tr) {
        var c = tr.querySelectorAll('td'); if (!c.length) return false;
        if (c.length === 1 && Number(c[0].getAttribute('colspan') || '1') > 1) return false; return true;
      });
      var n = rows.length;
      rows.forEach(function (tr) { var c = tr.querySelectorAll('td'); if (c[col]) c[col].textContent = String(n--); });
    });
  };
  if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', run, { once: true }); else run();
})();
</script>
<script>
// 네이티브 날짜 입력의 월 표기를 한글(예: 6월)로 — 요소 lang 지정(브라우저 영어 로케일 대응)
(function () {
  function fixLang() {
    document.querySelectorAll('input[type=date],input[type=month],input[type=datetime-local],input[type=week],input[type=time]')
      .forEach(function (el) { el.setAttribute('lang', 'ko'); });
  }
  if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', fixLang); else fixLang();
})();
</script>
<?= view('components/feedback_widget') ?>
<script>
/* 인쇄 시 활동 로그 기록(beacon) */
(function(){var last=0;window.addEventListener('beforeprint',function(){var now=Date.now();if(now-last<4000)return;last=now;try{var fd=new FormData();fd.append('title',document.title||'');fd.append('path',location.pathname||'');navigator.sendBeacon('<?= site_url('bag/activity/print-log') ?>',fd);}catch(e){}});})();
</script>
</body>
</html>
