// screens-admin.jsx — Admin dashboard screens for 출입통제

// ═══════════════════════════════════════════════════════════════
// 1. 관리자 대시보드 — 오버뷰
// ═══════════════════════════════════════════════════════════════
function AdminDashboard({ density = 'regular' }) {
  const compact = density === 'compact';
  return (
    <div style={{ width: '100%', height: '100%', display: 'flex', background: 'var(--paper-1)', color: 'var(--ink-0)', fontFamily: 'var(--font-sans)' }}>
      <AdminSidebar active="dashboard" />
      <div style={{ flex: 1, display: 'flex', flexDirection: 'column', minWidth: 0 }}>
        <AdminTopbar title="대시보드" subtitle="2026년 5월 8일 · 금요일" />
        <div style={{ flex: 1, padding: compact ? '24px 36px' : '36px 48px', overflow: 'hidden', display: 'flex', flexDirection: 'column', gap: compact ? 18 : 24 }}>
          {/* Headline KPI strip */}
          <div style={{ display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 1, background: 'var(--paper-stroke)', border: '0.5px solid var(--paper-stroke)', borderRadius: 'var(--r-md)', overflow: 'hidden' }}>
            <KPI label="현재 체류" big="248" unit="명" delta="+12" trend={[5,6,5,7,8,6,9,8,10,9,11,12,10]} />
            <KPI label="오늘 출입" big="1,847" unit="건" delta="+8.2%" trend={[3,5,4,6,5,7,8,7,9,8,10,9,11]} />
            <KPI label="승인 대기" big="14" unit="건" delta="—" tone="ochre" trend={[2,3,2,4,3,5,4,3,5,4,3,5,4]} attention />
            <KPI label="이상 시도" big="2" unit="건" delta="-1" tone="clay" trend={[1,0,2,1,0,1,2,0,1,1,0,2,1]} />
          </div>

          {/* Main split */}
          <div style={{ flex: 1, display: 'grid', gridTemplateColumns: '1fr 380px', gap: 18, minHeight: 0 }}>
            {/* Live activity */}
            <div style={{ background: 'var(--paper-0)', border: '0.5px solid var(--paper-stroke)', borderRadius: 'var(--r-md)', padding: 22, display: 'flex', flexDirection: 'column', minHeight: 0 }}>
              <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 18 }}>
                <div>
                  <div className="serif" style={{ fontSize: 17, fontWeight: 500 }}>실시간 출입 현황</div>
                  <div style={{ fontSize: 11, color: 'var(--ink-3)', marginTop: 2, letterSpacing: '0.04em' }}>LIVE · 자동 갱신 5초</div>
                </div>
                <div style={{ display: 'flex', gap: 6 }}>
                  <SegBtn active>전체</SegBtn>
                  <SegBtn>입장</SegBtn>
                  <SegBtn>퇴장</SegBtn>
                  <SegBtn>이상</SegBtn>
                </div>
              </div>

              {/* Hourly bar chart */}
              <div style={{ display: 'flex', alignItems: 'flex-end', gap: 3, height: 84, marginBottom: 22, padding: '0 4px' }}>
                {[8,12,14,18,22,28,42,68,86,72,54,48,52,58,64,72,68,52,38,28,22,18,14,10].map((v, i) => (
                  <div key={i} style={{ flex: 1, display: 'flex', flexDirection: 'column', gap: 2, alignItems: 'center' }}>
                    <div style={{ width: '100%', height: v, background: i === 14 ? 'var(--celadon-deep)' : i < 14 ? 'var(--celadon)' : 'var(--paper-3)', borderRadius: '2px 2px 0 0' }} />
                    {i % 4 === 0 && <span style={{ fontSize: 9, color: 'var(--ink-4)' }}>{String(i).padStart(2,'0')}</span>}
                  </div>
                ))}
              </div>

              {/* Activity feed */}
              <div style={{ flex: 1, display: 'flex', flexDirection: 'column', gap: 1, background: 'var(--paper-stroke)', overflow: 'hidden', borderRadius: 6 }}>
                {[
                  { t:'14:32:08', name:'김민서', dest:'본관 정문 · IN', tone:'approved', kind:'직원', code:'EMP-2391' },
                  { t:'14:31:54', name:'이지훈', dest:'B1 식당 · 식권사용', tone:'approved', kind:'직원', code:'MEAL-0428' },
                  { t:'14:31:31', name:'박서연', dest:'본관 정문 · IN', tone:'approved', kind:'방문', code:'VST-7821' },
                  { t:'14:30:47', name:'알 수 없음', dest:'후문 · 거부', tone:'denied', kind:'미인증', code:'—' },
                  { t:'14:30:12', name:'정유나', dest:'5F 회의실 304 · 입실', tone:'approved', kind:'방문', code:'MTG-0921' },
                  { t:'14:29:55', name:'최도윤', dest:'본관 정문 · OUT', tone:'info', kind:'직원', code:'EMP-1102' },
                  { t:'14:29:40', name:'한지원', dest:'B1 식당 · 식권사용', tone:'approved', kind:'직원', code:'MEAL-0427' },
                  { t:'14:29:18', name:'송예진', dest:'주차장 게이트 · IN', tone:'approved', kind:'외부', code:'CAR-3304' },
                ].slice(0, compact ? 6 : 8).map((row, i) => (
                  <ActivityRow key={i} {...row} />
                ))}
              </div>
            </div>

            {/* Right column */}
            <div style={{ display: 'flex', flexDirection: 'column', gap: 18, minHeight: 0 }}>
              <PendingCard />
              <GatesCard />
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}

function KPI({ label, big, unit, delta, trend, tone = 'celadon', attention }) {
  const color = tone === 'ochre' ? 'var(--ochre)' : tone === 'clay' ? 'var(--clay)' : 'var(--celadon)';
  return (
    <div style={{ background: 'var(--paper-0)', padding: '20px 22px', display: 'flex', flexDirection: 'column', gap: 10, position: 'relative' }}>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
        <span style={{ fontSize: 11.5, color: 'var(--ink-2)', letterSpacing: '0.02em', fontWeight: 500 }}>{label}</span>
        {attention && <span style={{ width: 6, height: 6, borderRadius: 100, background: 'var(--ochre)' }} />}
      </div>
      <div style={{ display: 'flex', alignItems: 'baseline', gap: 4 }}>
        <span className="serif tabular" style={{ fontSize: 32, fontWeight: 500, letterSpacing: '-0.02em', color: 'var(--ink-0)', lineHeight: 1 }}>{big}</span>
        <span style={{ fontSize: 12, color: 'var(--ink-3)' }}>{unit}</span>
      </div>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
        <span style={{ fontSize: 11, color: tone === 'clay' ? 'var(--celadon-deep)' : 'var(--ink-3)', fontWeight: 500 }}>{delta}</span>
        <Spark points={trend} color={color} width={64} height={20} />
      </div>
    </div>
  );
}

function SegBtn({ children, active }) {
  return <button style={{
    background: active ? 'var(--ink-0)' : 'transparent',
    color: active ? 'var(--paper-0)' : 'var(--ink-2)',
    border: active ? 'none' : '0.5px solid var(--paper-stroke)',
    padding: '5px 12px', borderRadius: 100, fontSize: 11.5, fontWeight: 500, cursor: 'pointer',
    fontFamily: 'inherit',
  }}>{children}</button>;
}

function ActivityRow({ t, name, dest, tone, kind, code }) {
  return (
    <div style={{ display: 'grid', gridTemplateColumns: '70px 22px 1fr auto auto', gap: 14, alignItems: 'center', padding: '10px 14px', background: 'var(--paper-0)', fontSize: 12 }}>
      <span className="mono" style={{ color: 'var(--ink-3)', fontSize: 10.5 }}>{t}</span>
      <Avatar name={name === '알 수 없음' ? '?' : name.slice(1,2)} size={22} />
      <div style={{ display: 'flex', flexDirection: 'column', gap: 1, minWidth: 0 }}>
        <span style={{ color: 'var(--ink-0)', fontWeight: 500 }}>{name} <span style={{ color: 'var(--ink-3)', fontWeight: 400, marginLeft: 4 }}>· {kind}</span></span>
        <span style={{ color: 'var(--ink-2)', fontSize: 11 }}>{dest}</span>
      </div>
      <span className="mono" style={{ color: 'var(--ink-3)', fontSize: 10.5 }}>{code}</span>
      <Pill tone={tone} dot soft>{tone === 'approved' ? '승인' : tone === 'denied' ? '거부' : '기록'}</Pill>
    </div>
  );
}

function PendingCard() {
  return (
    <div style={{ background: 'var(--paper-0)', border: '0.5px solid var(--paper-stroke)', borderRadius: 'var(--r-md)', padding: 22, display: 'flex', flexDirection: 'column', gap: 14 }}>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
        <div className="serif" style={{ fontSize: 15, fontWeight: 500 }}>승인 대기</div>
        <Pill tone="pending" soft>14건</Pill>
      </div>
      {[
        { name:'김도현', co:'(주) 한솔디자인', when:'오늘 15:00', purpose:'미팅 · 5F 회의실' },
        { name:'박지혜', co:'베리타스컨설팅', when:'오늘 16:30', purpose:'납품 · B1 자재' },
        { name:'이재민', co:'프리랜서', when:'내일 10:00', purpose:'면접 · 4F' },
      ].map((r, i) => (
        <div key={i} style={{ display: 'flex', flexDirection: 'column', gap: 8, padding: '12px 0', borderTop: i === 0 ? 'none' : '0.5px solid var(--paper-stroke)' }}>
          <div style={{ display: 'flex', alignItems: 'flex-start', justifyContent: 'space-between', gap: 10 }}>
            <div>
              <div style={{ fontSize: 13, fontWeight: 500 }}>{r.name} <span style={{ color: 'var(--ink-3)', fontWeight: 400 }}>· {r.co}</span></div>
              <div style={{ fontSize: 11, color: 'var(--ink-2)', marginTop: 2 }}>{r.purpose}</div>
            </div>
            <span className="mono" style={{ fontSize: 10.5, color: 'var(--ink-3)', whiteSpace: 'nowrap' }}>{r.when}</span>
          </div>
          <div style={{ display: 'flex', gap: 6 }}>
            <button style={{ flex: 1, background: 'var(--ink-0)', color: 'var(--paper-0)', border: 'none', padding: '6px 0', borderRadius: 6, fontSize: 11.5, fontWeight: 500, cursor: 'pointer', fontFamily: 'inherit' }}>승인 · QR발급</button>
            <button style={{ background: 'transparent', color: 'var(--ink-2)', border: '0.5px solid var(--paper-stroke)', padding: '6px 12px', borderRadius: 6, fontSize: 11.5, fontWeight: 500, cursor: 'pointer', fontFamily: 'inherit' }}>보류</button>
          </div>
        </div>
      ))}
    </div>
  );
}

function GatesCard() {
  const gates = [
    { name:'본관 정문', count:'128명', state:'정상', tone:'approved' },
    { name:'후문', count:'34명', state:'정상', tone:'approved' },
    { name:'B1 식당', count:'847식', state:'정상', tone:'approved' },
    { name:'주차장', count:'92대', state:'점검중', tone:'pending' },
    { name:'5F 회의실', count:'12회', state:'정상', tone:'approved' },
  ];
  return (
    <div style={{ background: 'var(--paper-0)', border: '0.5px solid var(--paper-stroke)', borderRadius: 'var(--r-md)', padding: '20px 22px', display: 'flex', flexDirection: 'column', gap: 12, flex: 1, minHeight: 0 }}>
      <div className="serif" style={{ fontSize: 15, fontWeight: 500 }}>게이트 · 채널</div>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 0 }}>
        {gates.map((g, i) => (
          <div key={i} style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '10px 0', borderTop: i === 0 ? 'none' : '0.5px solid var(--paper-stroke)' }}>
            <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
              <span style={{ width: 6, height: 6, borderRadius: 100, background: g.tone === 'approved' ? 'var(--celadon)' : 'var(--ochre)' }} />
              <span style={{ fontSize: 12.5, color: 'var(--ink-0)', fontWeight: 500 }}>{g.name}</span>
            </div>
            <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
              <span className="tabular" style={{ fontSize: 12, color: 'var(--ink-2)' }}>{g.count}</span>
              <span style={{ fontSize: 10.5, color: g.tone === 'approved' ? 'var(--celadon-deep)' : 'var(--ochre)' }}>{g.state}</span>
            </div>
          </div>
        ))}
      </div>
    </div>
  );
}

// ═══════════════════════════════════════════════════════════════
// shared admin chrome
// ═══════════════════════════════════════════════════════════════
function AdminSidebar({ active }) {
  const items = [
    { id:'dashboard', label:'대시보드', icon:'home', href: ROUTES.admin },
    { id:'requests', label:'출입신청', icon:'list', badge:14, href: ROUTES.requests },
    { id:'visitors', label:'방문객·직원', icon:'users', href: ROUTES.requests },
    { id:'logs', label:'출입로그', icon:'door', href: ROUTES.logs },
    { id:'meals', label:'구내식당', icon:'food', href: ROUTES.meals },
    { id:'analytics', label:'분석', icon:'chart', href: ROUTES.logs },
    { id:'gates', label:'게이트관리', icon:'shield', href: ROUTES.gates },
    { id:'settings', label:'설정', icon:'settings', href: ROUTES.delivery },
  ];
  return (
    <aside style={{ width: 220, background: 'var(--paper-0)', borderRight: '0.5px solid var(--paper-stroke)', display: 'flex', flexDirection: 'column', padding: '24px 14px' }}>
      <div style={{ padding: '0 8px 28px' }}>
        <Brandmark size={24} />
      </div>
      <div style={{ display: 'flex', flexDirection: 'column', gap: 1 }}>
        {items.map(it => (
          <a key={it.id} href={it.href} style={{
            display: 'flex', alignItems: 'center', gap: 11, padding: '8px 10px', borderRadius: 7, cursor: 'pointer',
            background: active === it.id ? 'var(--paper-2)' : 'transparent',
            color: active === it.id ? 'var(--ink-0)' : 'var(--ink-2)',
            fontSize: 12.5, fontWeight: active === it.id ? 500 : 400,
            textDecoration: 'none',
          }}>
            <Icon name={it.icon} size={15} />
            <span style={{ flex: 1 }}>{it.label}</span>
            {it.badge && <span style={{ fontSize: 10, padding: '1px 6px', background: 'var(--ochre-soft)', color: '#7A5C1A', borderRadius: 100, fontWeight: 500 }}>{it.badge}</span>}
          </a>
        ))}
      </div>
      <div style={{ marginTop: 'auto', padding: '14px 10px', borderTop: '0.5px solid var(--paper-stroke)', display: 'flex', alignItems: 'center', gap: 10 }}>
        <Avatar name="조" size={28} />
        <div style={{ minWidth: 0 }}>
          <div style={{ fontSize: 12, fontWeight: 500, color: 'var(--ink-0)' }}>조민혁</div>
          <div style={{ fontSize: 10.5, color: 'var(--ink-3)' }}>총무팀 · 관리자</div>
        </div>
      </div>
    </aside>
  );
}

function AdminTopbar({ title, subtitle, actions }) {
  return (
    <header style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '24px 48px 0', flexShrink: 0 }}>
      <div>
        <div className="serif" style={{ fontSize: 26, fontWeight: 500, letterSpacing: '-0.015em', color: 'var(--ink-0)' }}>{title}</div>
        <div style={{ fontSize: 12, color: 'var(--ink-3)', marginTop: 2, letterSpacing: '0.02em' }}>{subtitle}</div>
      </div>
      <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
        {actions}
        <div style={{ display: 'flex', alignItems: 'center', gap: 8, padding: '7px 12px', border: '0.5px solid var(--paper-stroke)', borderRadius: 7, background: 'var(--paper-0)', fontSize: 12, color: 'var(--ink-3)', minWidth: 220 }}>
          <Icon name="search" size={13} color="var(--ink-3)" />
          <span>이름 · 부서 · QR코드 검색</span>
          <span className="mono" style={{ marginLeft: 'auto', fontSize: 10, padding: '1px 5px', background: 'var(--paper-2)', borderRadius: 3 }}>⌘K</span>
        </div>
        <button style={{ width: 34, height: 34, border: '0.5px solid var(--paper-stroke)', borderRadius: 7, background: 'var(--paper-0)', cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center', position: 'relative' }}>
          <Icon name="bell" size={15} color="var(--ink-2)" />
          <span style={{ position: 'absolute', top: 6, right: 7, width: 6, height: 6, borderRadius: 100, background: 'var(--clay)' }} />
        </button>
      </div>
    </header>
  );
}

Object.assign(window, { AdminDashboard, AdminSidebar, AdminTopbar, KPI, SegBtn });
