// screens-approve.jsx — 승인 상세 + QR 발급 화면

function ApprovalDetail() {
  return (
    <div style={{ width: '100%', height: '100%', display: 'flex', background: 'var(--paper-1)', color: 'var(--ink-0)', fontFamily: 'var(--font-sans)' }}>
      <AdminSidebar active="requests" />
      <div style={{ flex: 1, display: 'flex', flexDirection: 'column', minWidth: 0 }}>
        {/* breadcrumb topbar */}
        <header style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '24px 48px 0' }}>
          <div>
            <div style={{ fontSize: 11.5, color: 'var(--ink-3)', display: 'flex', gap: 8, alignItems: 'center', marginBottom: 6 }}>
              <a href={ROUTES.requests} style={{ color: 'inherit', textDecoration: 'none' }}>출입신청</a>
              <Icon name="chevron" size={11} color="var(--ink-4)" />
              <a href={ROUTES.requests} style={{ color: 'inherit', textDecoration: 'none' }}>승인대기</a>
              <Icon name="chevron" size={11} color="var(--ink-4)" />
              <span className="mono" style={{ color: 'var(--ink-2)' }}>VST-7892</span>
            </div>
            <div className="serif" style={{ fontSize: 26, fontWeight: 500, letterSpacing: '-0.015em' }}>김도현 · 한솔디자인</div>
          </div>
          <div style={{ display: 'flex', gap: 8 }}>
            <button style={{ background: 'transparent', border: '0.5px solid var(--paper-stroke)', padding: '8px 14px', borderRadius: 7, fontSize: 12, color: 'var(--ink-2)', cursor: 'pointer', fontFamily: 'inherit' }}>거부</button>
            <button style={{ background: 'transparent', border: '0.5px solid var(--paper-stroke)', padding: '8px 14px', borderRadius: 7, fontSize: 12, color: 'var(--ink-2)', cursor: 'pointer', fontFamily: 'inherit' }}>보류</button>
            <button style={{ background: 'var(--ink-0)', color: 'var(--paper-0)', border: 'none', padding: '8px 16px', borderRadius: 7, fontSize: 12, fontWeight: 500, cursor: 'pointer', fontFamily: 'inherit', display: 'flex', alignItems: 'center', gap: 6 }}>
              <Icon name="check" size={13} color="var(--paper-0)" />승인 · QR 발급
            </button>
          </div>
        </header>

        <div style={{ flex: 1, padding: '28px 48px', overflow: 'hidden', display: 'grid', gridTemplateColumns: '1fr 380px', gap: 24, minHeight: 0 }}>
          {/* Left: form/details */}
          <div style={{ display: 'flex', flexDirection: 'column', gap: 18, overflow: 'hidden' }}>
            <DetailCard title="방문자 정보">
              <DetailRow label="이름">김도현 <span style={{ color: 'var(--ink-3)', fontWeight: 400 }}>· 본인 신청</span></DetailRow>
              <DetailRow label="소속">(주) 한솔디자인 · 디자인팀 책임</DetailRow>
              <DetailRow label="연락처" mono>010-4827-3091</DetailRow>
              <DetailRow label="이메일" mono>dh.kim@hansol.design</DetailRow>
              <DetailRow label="신분증" verified>주민등록증 · 본인확인 완료 (DI)</DetailRow>
              <DetailRow label="차량">25가 4719 (SM5 흰색)</DetailRow>
            </DetailCard>

            <DetailCard title="방문 목적">
              <DetailRow label="목적">프로젝트 미팅 · 디자인 시안 리뷰</DetailRow>
              <DetailRow label="호스트">박정민 책임 · 디자인팀 <span style={{ color: 'var(--ink-3)', fontWeight: 400 }}>(승인필요)</span></DetailRow>
              <DetailRow label="방문지">5F 회의실 304</DetailRow>
              <DetailRow label="일시">2026.05.08 (금) 15:00 — 17:00</DetailRow>
              <DetailRow label="동반자">없음</DetailRow>
            </DetailCard>

            {/* Permissions matrix */}
            <DetailCard title="출입 권한 설정">
              <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 1, background: 'var(--paper-stroke)', border: '0.5px solid var(--paper-stroke)', borderRadius: 8, overflow: 'hidden', marginTop: 4 }}>
                {[
                  { label:'본관 정문', on:true, time:'14:30—17:30' },
                  { label:'5F 출입구', on:true, time:'14:45—17:30' },
                  { label:'회의실 304', on:true, time:'15:00—17:00' },
                  { label:'B1 식당', on:true, time:'식권 1매' },
                  { label:'주차장', on:true, time:'2시간 무료' },
                  { label:'엘리베이터', on:false, time:'1F·5F 한정' },
                ].map((p, i) => (
                  <label key={i} style={{ background: 'var(--paper-0)', padding: '12px 14px', display: 'flex', alignItems: 'center', justifyContent: 'space-between', cursor: 'pointer' }}>
                    <div>
                      <div style={{ fontSize: 12.5, fontWeight: 500, color: 'var(--ink-0)' }}>{p.label}</div>
                      <div style={{ fontSize: 10.5, color: 'var(--ink-3)', marginTop: 2 }}>{p.time}</div>
                    </div>
                    <div style={{
                      width: 28, height: 16, borderRadius: 100,
                      background: p.on ? 'var(--celadon)' : 'var(--paper-3)',
                      position: 'relative', flexShrink: 0,
                    }}>
                      <div style={{ position: 'absolute', top: 2, left: p.on ? 14 : 2, width: 12, height: 12, borderRadius: 100, background: 'var(--paper-0)', transition: 'left .2s' }} />
                    </div>
                  </label>
                ))}
              </div>
            </DetailCard>
          </div>

          {/* Right: QR preview */}
          <div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
            <div style={{ background: 'var(--paper-0)', border: '0.5px solid var(--paper-stroke)', borderRadius: 'var(--r-md)', padding: 22 }}>
              <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 16 }}>
                <div className="serif" style={{ fontSize: 14, fontWeight: 500 }}>QR 미리보기</div>
                <Pill tone="info" soft>발급 전</Pill>
              </div>
              <div style={{ background: 'var(--paper-1)', border: '0.5px solid var(--paper-stroke)', borderRadius: 10, padding: 24, display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 14 }}>
                <div style={{ position: 'relative' }}>
                  <QRBlock size={180} seed="VST-7892-DH" dark="var(--ink-0)" />
                  <div style={{ position: 'absolute', top: '50%', left: '50%', transform: 'translate(-50%,-50%)', background: 'var(--paper-1)', padding: '4px 6px', borderRadius: 4 }}>
                    <Seal size={26} label="出" />
                  </div>
                </div>
                <div style={{ textAlign: 'center' }}>
                  <div className="mono" style={{ fontSize: 11, color: 'var(--ink-2)' }}>VST-7892 · 김도현</div>
                  <div style={{ fontSize: 10.5, color: 'var(--ink-3)', marginTop: 3 }}>2026.05.08 14:30 — 17:30</div>
                </div>
              </div>

              <div style={{ marginTop: 16, display: 'flex', flexDirection: 'column', gap: 10 }}>
                <DetailMini label="보안 정책" value="동적 QR · 30초 갱신" />
                <DetailMini label="유효 시간" value="당일 14:30 — 17:30" />
                <DetailMini label="사용 횟수" value="입장 1회 + 식권 1회" />
                <DetailMini label="허용 게이트" value="정문 · 5F · 회의실 304" />
              </div>
            </div>

            <div style={{ background: 'var(--paper-0)', border: '0.5px solid var(--paper-stroke)', borderRadius: 'var(--r-md)', padding: 22 }}>
              <div className="serif" style={{ fontSize: 14, fontWeight: 500, marginBottom: 14 }}>전송 채널</div>
              <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
                <Channel icon="mail" label="이메일" target="dh.kim@hansol.design" on />
                <Channel icon="msg" label="SMS" target="010-4827-3091" on />
                <Channel icon="msg" label="카카오 알림톡" target="010-4827-3091" on />
                <Channel icon="download" label="PDF 출력" target="A4 · 흑백" />
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}

function DetailCard({ title, children }) {
  return (
    <div style={{ background: 'var(--paper-0)', border: '0.5px solid var(--paper-stroke)', borderRadius: 'var(--r-md)', padding: '20px 22px' }}>
      <div className="serif" style={{ fontSize: 14, fontWeight: 500, marginBottom: 14 }}>{title}</div>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 0 }}>
        {children}
      </div>
    </div>
  );
}

function DetailRow({ label, children, mono, verified }) {
  return (
    <div style={{ display: 'grid', gridTemplateColumns: '90px 1fr', gap: 16, padding: '8px 0', borderTop: '0.5px solid var(--paper-stroke)', alignItems: 'baseline' }}>
      <span style={{ fontSize: 11.5, color: 'var(--ink-3)', letterSpacing: '0.02em' }}>{label}</span>
      <span className={mono ? 'mono' : ''} style={{ fontSize: mono ? 12 : 12.5, color: 'var(--ink-0)', fontWeight: 500, display: 'flex', alignItems: 'center', gap: 8 }}>
        {children}
        {verified && <Pill tone="approved" dot soft>인증</Pill>}
      </span>
    </div>
  );
}

function DetailMini({ label, value }) {
  return (
    <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', fontSize: 11.5 }}>
      <span style={{ color: 'var(--ink-3)' }}>{label}</span>
      <span style={{ color: 'var(--ink-1)', fontWeight: 500 }}>{value}</span>
    </div>
  );
}

function Channel({ icon, label, target, on }) {
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 12, padding: '10px 12px', border: '0.5px solid var(--paper-stroke)', borderRadius: 7, background: on ? 'var(--paper-1)' : 'transparent' }}>
      <Icon name={icon} size={14} color={on ? 'var(--ink-1)' : 'var(--ink-3)'} />
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ fontSize: 12, fontWeight: 500, color: 'var(--ink-0)' }}>{label}</div>
        <div className="mono" style={{ fontSize: 10.5, color: 'var(--ink-3)', marginTop: 1, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{target}</div>
      </div>
      <div style={{
        width: 26, height: 14, borderRadius: 100,
        background: on ? 'var(--celadon)' : 'var(--paper-3)',
        position: 'relative', flexShrink: 0,
      }}>
        <div style={{ position: 'absolute', top: 2, left: on ? 14 : 2, width: 10, height: 10, borderRadius: 100, background: 'var(--paper-0)', transition: 'left .2s' }} />
      </div>
    </div>
  );
}

Object.assign(window, { ApprovalDetail, DetailCard, DetailRow, DetailMini, Channel });
