// screens-public-v2.jsx — 신청화면 대안 구성
// v1: 3-step wizard / v2: 단일 페이지 좌우 분할 (양식 + 라이브 패스 미리보기)

function PublicFormV2() {
  return (
    <div style={{ width: '100%', height: '100%', background: 'var(--paper-1)', color: 'var(--ink-0)', fontFamily: 'var(--font-sans)', display: 'flex', flexDirection: 'column', overflow: 'hidden' }}>
      <header style={{ padding: '18px 48px', display: 'flex', alignItems: 'center', justifyContent: 'space-between', borderBottom: '0.5px solid var(--paper-stroke)' }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 14 }}>
          <Brandmark size={22} />
          <span style={{ fontSize: 11, color: 'var(--ink-3)' }}>· 방문 신청</span>
        </div>
        <div style={{ display: 'flex', alignItems: 'center', gap: 14, fontSize: 11, color: 'var(--ink-3)' }}>
          <span>3분 소요</span>
          <span style={{ width: 1, height: 12, background: 'var(--paper-stroke)' }} />
          <span>모든 항목은 SSL로 암호화됩니다</span>
          <span style={{ width: 1, height: 12, background: 'var(--paper-stroke)' }} />
          <a href={ROUTES.admin} style={{ color: 'var(--ink-1)', textDecoration: 'none', fontWeight: 500 }}>관리자 로그인</a>
        </div>
      </header>

      {/* hero band */}
      <div style={{ padding: '32px 48px 20px', borderBottom: '0.5px solid var(--paper-stroke)' }}>
        <div className="serif" style={{ fontSize: 36, fontWeight: 500, letterSpacing: '-0.02em', lineHeight: 1.15 }}>
          어디로, 누구를 만나러 오시나요?
        </div>
        <div style={{ fontSize: 13, color: 'var(--ink-3)', marginTop: 8 }}>
          담당자 승인 후 카카오톡으로 QR이 전송됩니다.
        </div>
      </div>

      <div style={{ flex: 1, display: 'grid', gridTemplateColumns: '1fr 420px', overflow: 'hidden' }}>
        {/* Left: form, single page with sections */}
        <div style={{ padding: '32px 48px', overflow: 'auto', display: 'flex', flexDirection: 'column', gap: 28 }}>
          {/* Section 1: who */}
          <FormSection num="01" title="방문하시는 분" sub="실명을 입력해 주세요. 신분증과 대조됩니다.">
            <div style={{ display: 'grid', gridTemplateColumns: '1.2fr 1fr', gap: 12 }}>
              <FieldV2 label="이름" value="김도현" />
              <FieldV2 label="휴대전화" value="010-1234-5678" mono />
            </div>
            <div style={{ display: 'grid', gridTemplateColumns: '1.5fr 1fr', gap: 12, marginTop: 12 }}>
              <FieldV2 label="이메일" value="dohyun@hansol-design.co.kr" mono />
              <FieldV2 label="소속" value="한솔디자인" />
            </div>
          </FormSection>

          {/* Section 2: when — calendar */}
          <FormSection num="02" title="언제 방문하시나요?">
            <CalendarPicker />
            <div style={{ marginTop: 16, display: 'flex', alignItems: 'center', gap: 10 }}>
              <span style={{ fontSize: 11.5, color: 'var(--ink-3)' }}>시간대</span>
              <div style={{ display: 'flex', gap: 6, flexWrap: 'wrap' }}>
                {['09:00','10:00','11:00','13:00','14:00','15:00','16:00','17:00'].map((t, i) => (
                  <span key={i} style={{
                    fontSize: 11.5, padding: '6px 12px', borderRadius: 100,
                    border: '0.5px solid var(--paper-stroke)',
                    background: i === 5 ? 'var(--ink-0)' : 'var(--paper-0)',
                    color: i === 5 ? 'var(--paper-0)' : 'var(--ink-1)',
                    fontFamily: 'var(--font-mono)',
                    cursor: 'pointer',
                  }}>{t}</span>
                ))}
              </div>
              <span style={{ fontSize: 11, color: 'var(--ink-3)', marginLeft: 'auto' }}>~ 18:00 까지</span>
            </div>
          </FormSection>

          {/* Section 3: where + host */}
          <FormSection num="03" title="누구를, 어디서 만나시나요?">
            <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 12 }}>
              <FieldV2 label="담당자(호스트)" value="박정민 책임" hint="@ 선임연구소" />
              <FieldV2 label="장소" value="5F 회의실 304" hint="자동 추천" />
            </div>
            <div style={{ marginTop: 12 }}>
              <FieldV2 label="방문 목적" value="2분기 디자인 시스템 리뷰 미팅" textarea />
            </div>

            {/* options */}
            <div style={{ marginTop: 16, display: 'flex', flexDirection: 'column', gap: 8 }}>
              <CheckRow checked label="주차장 이용" sub="주차권 1매 자동 발급 · 4시간 무료" />
              <CheckRow label="점심 식권" sub="구내식당 1식권 추가 (8,000원, 호스트 부담)" />
              <CheckRow label="노트북·카메라 등 반입 장비" sub="장비 시리얼 등록 시 보안검색 우회" />
            </div>
          </FormSection>

          {/* consent + submit */}
          <div style={{ marginTop: 4, display: 'flex', alignItems: 'flex-end', justifyContent: 'space-between', paddingTop: 20, borderTop: '0.5px solid var(--paper-stroke)' }}>
            <div style={{ display: 'flex', flexDirection: 'column', gap: 6, fontSize: 11.5, color: 'var(--ink-2)' }}>
              <label style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
                <span style={{ width: 14, height: 14, borderRadius: 3, background: 'var(--ink-0)', display: 'inline-flex', alignItems: 'center', justifyContent: 'center' }}>
                  <Icon name="check" size={9} color="var(--paper-0)" />
                </span>
                <span>개인정보 수집·이용에 동의합니다 (필수)</span>
              </label>
              <label style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
                <span style={{ width: 14, height: 14, borderRadius: 3, border: '0.5px solid var(--paper-stroke)' }} />
                <span>방문 후기 안내 메일 수신 (선택)</span>
              </label>
            </div>
            <button style={{ background: 'var(--ink-0)', color: 'var(--paper-0)', border: 'none', padding: '14px 28px', borderRadius: 8, fontSize: 13, fontWeight: 500, cursor: 'pointer', fontFamily: 'inherit', display: 'flex', alignItems: 'center', gap: 10 }}>
              승인 요청 보내기
              <Icon name="arrow" size={14} color="var(--paper-0)" />
            </button>
          </div>
        </div>

        {/* Right: live pass preview */}
        <aside style={{ background: 'var(--paper-2)', padding: '32px 28px', borderLeft: '0.5px solid var(--paper-stroke)', display: 'flex', flexDirection: 'column', gap: 18 }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
            <span style={{ fontSize: 10.5, color: 'var(--ink-3)', letterSpacing: '0.2em', textTransform: 'uppercase' }}>실시간 미리보기</span>
            <span style={{ flex: 1, height: 0.5, background: 'var(--paper-stroke)' }} />
          </div>

          {/* visitor pass card — looks like a real ticket */}
          <div style={{ background: 'var(--paper-0)', borderRadius: 14, overflow: 'hidden', boxShadow: 'var(--shadow-2)', border: '0.5px solid var(--paper-stroke)', position: 'relative' }}>
            {/* Top stripe */}
            <div style={{ background: 'var(--ink-0)', color: 'var(--paper-0)', padding: '14px 18px', display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
              <div>
                <div style={{ fontSize: 9.5, letterSpacing: '0.25em', opacity: 0.6 }}>VISITOR PASS</div>
                <div className="serif" style={{ fontSize: 16, fontWeight: 500, marginTop: 2 }}>방문증</div>
              </div>
              <Brandmark size={16} subtle />
            </div>

            {/* QR section */}
            <div style={{ padding: '20px 18px 14px', display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 10, position: 'relative' }}>
              <div style={{ position: 'relative' }}>
                <QRBlock size={170} seed="VST-PREVIEW-V2" dark="var(--ink-0)" />
                <div style={{ position: 'absolute', top: '50%', left: '50%', transform: 'translate(-50%,-50%)', background: 'var(--paper-0)', padding: 6, borderRadius: 7 }}>
                  <Seal size={32} label="出" />
                </div>
              </div>
              <div className="mono" style={{ fontSize: 10.5, color: 'var(--ink-3)' }}>VST-PENDING-XXXX</div>
            </div>

            {/* perforation */}
            <div style={{ position: 'relative', height: 14, display: 'flex', alignItems: 'center' }}>
              <div style={{ position: 'absolute', left: -7, width: 14, height: 14, borderRadius: 100, background: 'var(--paper-2)' }} />
              <div style={{ position: 'absolute', right: -7, width: 14, height: 14, borderRadius: 100, background: 'var(--paper-2)' }} />
              <div style={{ flex: 1, borderTop: '0.5px dashed var(--paper-stroke)', margin: '0 12px' }} />
            </div>

            {/* details */}
            <div style={{ padding: '14px 18px 18px', display: 'flex', flexDirection: 'column', gap: 8 }}>
              <DetailRow label="방문자" value="김도현 · 한솔디자인" />
              <DetailRow label="일시" value="2026.05.08 · 14:00 — 17:00" />
              <DetailRow label="방문지" value="5F 회의실 304" />
              <DetailRow label="호스트" value="박정민 책임" />
              <DetailRow label="권한" value="정문 · 5F · 식당 · 주차" tone="celadon" />
            </div>
          </div>

          <div style={{ marginTop: 'auto', padding: 14, background: 'rgba(143,177,164,0.08)', borderRadius: 10, fontSize: 11.5, color: 'var(--celadon-deep)', display: 'flex', gap: 10 }}>
            <Icon name="shield" size={14} color="var(--celadon-deep)" />
            <div>
              <div style={{ fontWeight: 500, marginBottom: 2 }}>QR은 30초마다 갱신됩니다</div>
              <div style={{ color: 'var(--ink-2)' }}>화면 캡쳐로는 다른 사람이 쓸 수 없어요.</div>
            </div>
          </div>
        </aside>
      </div>
    </div>
  );
}

function FormSection({ num, title, sub, children }) {
  return (
    <section>
      <div style={{ display: 'flex', alignItems: 'baseline', gap: 12, marginBottom: 14 }}>
        <span className="mono" style={{ fontSize: 10.5, color: 'var(--celadon-deep)', fontWeight: 600 }}>{num}</span>
        <div className="serif" style={{ fontSize: 18, fontWeight: 500, color: 'var(--ink-0)' }}>{title}</div>
        {sub && <span style={{ fontSize: 11, color: 'var(--ink-3)' }}>{sub}</span>}
      </div>
      {children}
    </section>
  );
}

function FieldV2({ label, value, hint, mono, textarea }) {
  return (
    <label style={{ display: 'flex', flexDirection: 'column', gap: 4 }}>
      <div style={{ display: 'flex', alignItems: 'baseline', gap: 8 }}>
        <span style={{ fontSize: 10.5, color: 'var(--ink-3)', letterSpacing: '0.04em' }}>{label}</span>
        {hint && <span style={{ fontSize: 10, color: 'var(--celadon-deep)' }}>{hint}</span>}
      </div>
      <div style={{
        background: 'var(--paper-0)', border: '0.5px solid var(--paper-stroke)',
        borderRadius: 7, padding: textarea ? '10px 14px' : '10px 14px',
        fontSize: 13, color: 'var(--ink-0)',
        fontFamily: mono ? 'var(--font-mono)' : 'inherit',
        minHeight: textarea ? 56 : 'auto',
        borderBottom: '1.5px solid var(--ink-0)',
      }}>{value}<span style={{ display: 'inline-block', width: 1, height: 14, background: 'var(--ink-0)', verticalAlign: 'middle', marginLeft: 1, animation: 'blink 1s infinite' }} /></div>
    </label>
  );
}

function CalendarPicker() {
  const days = ['일','월','화','수','목','금','토'];
  // Build 5 weeks for May 2026 starting Friday
  const cells = [];
  for (let i = 0; i < 35; i++) {
    const d = i - 4; // May 1 = Friday => index 5
    cells.push(d > 0 && d <= 31 ? d : null);
  }
  return (
    <div style={{ background: 'var(--paper-0)', border: '0.5px solid var(--paper-stroke)', borderRadius: 10, padding: 16 }}>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 10 }}>
        <Icon name="chevron" size={14} color="var(--ink-3)" />
        <div className="serif" style={{ fontSize: 14, fontWeight: 500 }}>2026년 5월</div>
        <Icon name="chevron" size={14} color="var(--ink-2)" />
      </div>
      <div style={{ display: 'grid', gridTemplateColumns: 'repeat(7, 1fr)', gap: 2 }}>
        {days.map((d, i) => (
          <div key={d} style={{ textAlign: 'center', fontSize: 10, color: i === 0 ? 'var(--clay)' : 'var(--ink-3)', padding: '4px 0' }}>{d}</div>
        ))}
        {cells.map((d, i) => {
          const sel = d === 8;
          const today = d === 5;
          const past = d && d < 5;
          return (
            <div key={i} style={{
              aspectRatio: '1',
              display: 'flex', alignItems: 'center', justifyContent: 'center',
              fontSize: 12, fontFamily: 'var(--font-mono)',
              color: !d ? 'transparent' : past ? 'var(--ink-4)' : (i % 7 === 0 ? 'var(--clay)' : 'var(--ink-1)'),
              background: sel ? 'var(--ink-0)' : 'transparent',
              borderRadius: 7,
              border: today && !sel ? '0.5px solid var(--celadon)' : 'none',
              position: 'relative',
              cursor: d && !past ? 'pointer' : 'default',
            }}>
              <span style={{ color: sel ? 'var(--paper-0)' : 'inherit' }}>{d}</span>
              {/* host availability dot */}
              {d && !past && [10, 12, 13, 15, 19].includes(d) && (
                <span style={{ position: 'absolute', bottom: 4, width: 3, height: 3, borderRadius: 100, background: sel ? 'var(--paper-0)' : 'var(--celadon)' }} />
              )}
            </div>
          );
        })}
      </div>
      <div style={{ marginTop: 10, paddingTop: 10, borderTop: '0.5px solid var(--paper-stroke)', display: 'flex', gap: 16, fontSize: 10.5, color: 'var(--ink-3)' }}>
        <span style={{ display: 'flex', alignItems: 'center', gap: 5 }}><span style={{ width: 5, height: 5, borderRadius: 100, background: 'var(--celadon)' }} />호스트 가능</span>
        <span style={{ display: 'flex', alignItems: 'center', gap: 5 }}><span style={{ width: 8, height: 8, border: '0.5px solid var(--celadon)', borderRadius: 2 }} />오늘</span>
      </div>
    </div>
  );
}

function CheckRow({ checked, label, sub }) {
  return (
    <label style={{
      display: 'flex', alignItems: 'flex-start', gap: 12, cursor: 'pointer',
      padding: '10px 14px', background: checked ? 'rgba(143,177,164,0.06)' : 'var(--paper-0)',
      border: `0.5px solid ${checked ? 'var(--celadon)' : 'var(--paper-stroke)'}`,
      borderRadius: 8,
    }}>
      <span style={{
        width: 16, height: 16, borderRadius: 4, marginTop: 1,
        background: checked ? 'var(--ink-0)' : 'transparent',
        border: checked ? 'none' : '0.5px solid var(--paper-stroke)',
        display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0,
      }}>
        {checked && <Icon name="check" size={10} color="var(--paper-0)" />}
      </span>
      <div style={{ minWidth: 0 }}>
        <div style={{ fontSize: 12.5, fontWeight: 500 }}>{label}</div>
        <div style={{ fontSize: 11, color: 'var(--ink-3)', marginTop: 2 }}>{sub}</div>
      </div>
    </label>
  );
}

function DetailRow({ label, value, tone }) {
  return (
    <div style={{ display: 'grid', gridTemplateColumns: '60px 1fr', gap: 10, fontSize: 11.5 }}>
      <span style={{ color: 'var(--ink-3)' }}>{label}</span>
      <span style={{ color: tone === 'celadon' ? 'var(--celadon-deep)' : 'var(--ink-0)', fontWeight: tone ? 500 : 400 }}>{value}</span>
    </div>
  );
}

Object.assign(window, { PublicFormV2 });
