// watchlist-tab-variants.jsx — common big-tech layouts for putting
// Watchlist (自選股) + Screeners (篩選器) in ONE bottom tab ("自選").
// Reuses edit-modes.jsx shared bits (EM_STOCKS, EMTicker, EMNameCell, EMPriceCell, EMScreenHead).

const { useState: wvS } = React;
const WT = () => window.flutterTokens;

// saved screeners (name + matched count)
const SCREENS = [
  { name: '半導體龍頭', n: 24 },
  { name: 'Magnificent 7', n: 7 },
  { name: '高股息 ETF 成分', n: 38 },
  { name: '突破年線', n: 112 },
];
// user can have MULTIPLE watchlists (each tagged with its market mix)
const WATCHLISTS = [
  { name: '我的持股', n: 8, mk: '混合' },
  { name: '觀察中', n: 15, mk: '台股' },
  { name: '高息定存股', n: 6, mk: '台股' },
  { name: 'ETF', n: 5, mk: '美股' },
];
const SCREEN_MK = ['台股', '美股', '台股', '美股'];
const MARKETS = ['全部', '台股', '美股', '幣圈'];
const WL = window.EM_STOCKS; // favorited stocks

function StockRow({ s }) {
  const t = WT();
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 12, padding: '11px 16px', borderBottom: `1px solid ${t.hairline}` }}>
      <window.EMTicker sym={s.sym}/><window.EMNameCell s={s}/><window.EMPriceCell s={s}/>
    </div>
  );
}
function MkBadge({ mk }) {
  const t = WT();
  return <span style={{ fontSize: 10.5, fontWeight: 700, color: t.ink3, background: t.surfaceAlt, borderRadius: 5, padding: '2px 7px' }}>{mk}</span>;
}
function MkChips({ sel, onSel }) {
  const t = WT();
  return (
    <div style={{ display: 'flex', gap: 7, padding: '10px 16px', overflowX: 'auto', scrollbarWidth: 'none', background: t.surface, borderBottom: `1px solid ${t.hairline}` }}>
      {MARKETS.map(m => {
        const on = sel === m;
        return <button key={m} onClick={() => onSel(m)} style={{ flexShrink: 0, background: on ? t.ink : t.surfaceAlt, color: on ? t.bg : t.ink2, border: `1px solid ${on ? t.ink : t.hairline}`, borderRadius: 999, padding: '6px 13px', fontSize: 12.5, fontWeight: on ? 700 : 600, cursor: 'pointer', fontFamily: 'inherit', whiteSpace: 'nowrap' }}>{m}</button>;
      })}
    </div>
  );
}
function ListRow({ name, n, icon, mk }) {
  const t = WT();
  return (
    <button style={{ width: '100%', display: 'flex', alignItems: 'center', gap: 12, padding: '13px 16px', background: 'transparent', border: 'none', borderBottom: `1px solid ${t.hairline}`, cursor: 'pointer', fontFamily: 'inherit', textAlign: 'left' }}>
      <span style={{ width: 36, height: 36, borderRadius: 10, background: t.surfaceAlt, display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}>
        <window.Icon name={icon} size={17} color={t.ink2}/>
      </span>
      <span style={{ flex: 1, minWidth: 0, display: 'inline-flex', alignItems: 'center', gap: 7 }}>
        <span style={{ fontSize: 15, fontWeight: 700, color: t.ink, letterSpacing: -0.2 }}>{name}</span>
        {mk && <MkBadge mk={mk}/>}
      </span>
      <span style={{ fontFamily: '"Plus Jakarta Sans",monospace', fontSize: 13.5, fontWeight: 700, color: t.ink2 }}>{n}</span>
      <window.Icon name="chevron" size={15} color={t.ink3}/>
    </button>
  );
}
function ScreenRow({ s }) {
  const t = WT();
  return (
    <button style={{ width: '100%', display: 'flex', alignItems: 'center', gap: 12, padding: '13px 16px', background: 'transparent', border: 'none', borderBottom: `1px solid ${t.hairline}`, cursor: 'pointer', fontFamily: 'inherit', textAlign: 'left' }}>
      <span style={{ width: 36, height: 36, borderRadius: 10, background: t.surfaceAlt, display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}>
        <window.Icon name="funnel" size={17} color={t.ink2}/>
      </span>
      <span style={{ flex: 1, fontSize: 15, fontWeight: 700, color: t.ink, letterSpacing: -0.2 }}>{s.name}</span>
      <span style={{ fontFamily: '"Plus Jakarta Sans",monospace', fontSize: 13.5, fontWeight: 700, color: t.ink2 }}>{s.n}</span>
      <window.Icon name="chevron" size={15} color={t.ink3}/>
    </button>
  );
}
function SectionHead({ children, action }) {
  const t = WT();
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 8, padding: '18px 16px 8px' }}>
      <span style={{ flex: 1, fontSize: 12.5, fontWeight: 700, color: t.ink2, letterSpacing: 0.3 }}>{children}</span>
      {action && <span style={{ fontSize: 12.5, fontWeight: 700, color: t.ink3 }}>{action}</span>}
    </div>
  );
}
function Head() {
  const t = WT();
  return (
    <div style={{ padding: '56px 16px 10px', background: t.surface, borderBottom: `1px solid ${t.hairline}` }}>
      <div style={{ fontFamily: '"Plus Jakarta Sans","Noto Sans TC",sans-serif', fontSize: 24, fontWeight: 700, color: t.ink, letterSpacing: -0.5 }}>自選</div>
    </div>
  );
}

// ════ A · 頂部 Segmented（自選股 / 篩選器）════════════════════════
function WVSegmented() {
  const t = WT();
  const [tab, setTab] = wvS('wl');
  return (
    <div style={{ height: '100%', background: t.bg, display: 'flex', flexDirection: 'column' }}>
      <Head/>
      <div style={{ padding: '10px 16px', background: t.surface, borderBottom: `1px solid ${t.hairline}` }}>
        <div style={{ display: 'flex', gap: 4, background: t.surfaceAlt, borderRadius: 11, padding: 3 }}>
          {[['wl', '自選股'], ['scr', '篩選器']].map(([k, lbl]) => {
            const on = tab === k;
            return <button key={k} onClick={() => setTab(k)} style={{ flex: 1, background: on ? t.surface : 'transparent', border: 'none', borderRadius: 8, padding: '8px 6px', fontSize: 13.5, fontWeight: on ? 700 : 500, color: on ? t.ink : t.ink2, cursor: 'pointer', fontFamily: 'inherit', boxShadow: on ? '0 1px 2px rgba(31,27,22,0.1)' : 'none' }}>{lbl}</button>;
          })}
        </div>
      </div>
      <div style={{ flex: 1, overflowY: 'auto' }}>
        {tab === 'wl' ? WL.map(s => <StockRow key={s.sym} s={s}/>) : SCREENS.map((s, i) => <ScreenRow key={i} s={s}/>)}
      </div>
    </div>
  );
}

// ════ B · 分區捲動（自選股 section + 篩選器 section）═══════════════
function WVSectioned() {
  const t = WT();
  return (
    <div style={{ height: '100%', background: t.bg, display: 'flex', flexDirection: 'column' }}>
      <Head/>
      <div style={{ flex: 1, overflowY: 'auto' }}>
        <SectionHead action="管理">自選股</SectionHead>
        {WL.slice(0, 4).map(s => <StockRow key={s.sym} s={s}/>)}
        <SectionHead action="全部">我的篩選器</SectionHead>
        {SCREENS.map((s, i) => <ScreenRow key={i} s={s}/>)}
        <div style={{ height: 20 }}/>
      </div>
    </div>
  );
}

// ════ C · 篩選器卡片橫滑 + 自選股清單 ════════════════════════════
function WVCards() {
  const t = WT();
  return (
    <div style={{ height: '100%', background: t.bg, display: 'flex', flexDirection: 'column' }}>
      <Head/>
      <div style={{ flex: 1, overflowY: 'auto' }}>
        <SectionHead action="全部">篩選器</SectionHead>
        <div style={{ display: 'flex', gap: 10, padding: '2px 16px 6px', overflowX: 'auto', scrollbarWidth: 'none' }}>
          {SCREENS.map((s, i) => (
            <div key={i} style={{ flexShrink: 0, width: 130, background: t.surface, border: `1px solid ${t.hairline}`, borderRadius: 14, padding: '13px 14px', cursor: 'pointer' }}>
              <window.Icon name="funnel" size={16} color={t.ink2}/>
              <div style={{ fontSize: 13.5, fontWeight: 700, color: t.ink, marginTop: 8, lineHeight: 1.3 }}>{s.name}</div>
              <div style={{ fontSize: 12, color: t.ink3, marginTop: 3, fontFamily: '"Plus Jakarta Sans",monospace' }}>{s.n} 檔</div>
            </div>
          ))}
        </div>
        <SectionHead action="管理">自選股</SectionHead>
        {WL.map(s => <StockRow key={s.sym} s={s}/>)}
        <div style={{ height: 20 }}/>
      </div>
    </div>
  );
}

// ════ D · 頂部 Tab Bar（Material 上分頁）═════════════════════════
function WVTopTabs() {
  const t = WT();
  const [tab, setTab] = wvS('wl');
  const [mk, setMk] = wvS('全部');
  const wls = mk === '全部' ? WATCHLISTS : WATCHLISTS.filter(w => w.mk === mk || w.mk === '混合');
  const scrs = SCREENS.map((s, i) => ({ ...s, mk: SCREEN_MK[i] })).filter(s => mk === '全部' || s.mk === mk);
  return (
    <div style={{ height: '100%', background: t.bg, display: 'flex', flexDirection: 'column' }}>
      <Head/>
      <div style={{ display: 'flex', background: t.surface, borderBottom: `1px solid ${t.hairline}` }}>
        {[['wl', '自選股'], ['scr', '篩選器']].map(([k, lbl]) => {
          const on = tab === k;
          return (
            <button key={k} onClick={() => setTab(k)} style={{ flex: 1, background: 'transparent', border: 'none', cursor: 'pointer', fontFamily: 'inherit', padding: '13px 0 11px', position: 'relative' }}>
              <span style={{ fontSize: 14, fontWeight: on ? 700 : 500, color: on ? t.ink : t.ink3 }}>{lbl}</span>
              {on && <span style={{ position: 'absolute', left: '50%', bottom: 0, transform: 'translateX(-50%)', width: 34, height: 3, borderRadius: 2, background: t.ink }}/>}
            </button>
          );
        })}
      </div>
      <MkChips sel={mk} onSel={setMk}/>
      <div style={{ flex: 1, overflowY: 'auto' }}>
        {tab === 'wl'
          ? <>{wls.map((w, i) => <ListRow key={i} name={w.name} n={w.n} mk={w.mk} icon="select"/>)}
              <button style={{ display: 'flex', alignItems: 'center', gap: 10, width: '100%', padding: '14px 16px', background: 'transparent', border: 'none', cursor: 'pointer', fontFamily: 'inherit', color: t.ink2 }}>
                <span style={{ width: 36, height: 36, borderRadius: 10, border: `1.5px dashed ${t.ink3}`, display: 'flex', alignItems: 'center', justifyContent: 'center' }}><window.Icon name="plus" size={17} color={t.ink2}/></span>
                <span style={{ fontSize: 14.5, fontWeight: 700 }}>建立自選清單</span>
              </button></>
          : scrs.map((s, i) => (
              <button key={i} style={{ width: '100%', display: 'flex', alignItems: 'center', gap: 12, padding: '13px 16px', background: 'transparent', border: 'none', borderBottom: `1px solid ${t.hairline}`, cursor: 'pointer', fontFamily: 'inherit', textAlign: 'left' }}>
                <span style={{ width: 36, height: 36, borderRadius: 10, background: t.surfaceAlt, display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}><window.Icon name="funnel" size={17} color={t.ink2}/></span>
                <span style={{ flex: 1, minWidth: 0, display: 'inline-flex', alignItems: 'center', gap: 7 }}><span style={{ fontSize: 15, fontWeight: 700, color: t.ink, letterSpacing: -0.2 }}>{s.name}</span><MkBadge mk={s.mk}/></span>
                <span style={{ fontFamily: '"Plus Jakarta Sans",monospace', fontSize: 13.5, fontWeight: 700, color: t.ink2 }}>{s.n}</span>
                <window.Icon name="chevron" size={15} color={t.ink3}/>
              </button>
            ))}
      </div>
    </div>
  );
}

Object.assign(window, { WVSegmented, WVSectioned, WVCards, WVTopTabs });

const VW = 402, VH = 874;
const vFrame = { background: 'transparent', boxShadow: 'none', borderRadius: 0, overflow: 'visible' };
function VFr({ children }) { return <window.IOSDevice width={VW} height={VH}>{children}</window.IOSDevice>; }

ReactDOM.createRoot(document.getElementById('root')).render(
  <window.DesignCanvas>
    <window.DCSection id="wltab" title="自選分頁 — Watchlist + Screeners 合併" subtitle="把自選股與篩選器放進同一個 bottom tab · 四種大廠常見樣式">
      <window.DCArtboard id="wv-seg" label="A · 頂部 Segmented（自選股 / 篩選器）" width={VW} height={VH} style={vFrame}>
        <VFr><window.WVSegmented/></VFr>
      </window.DCArtboard>
      <window.DCArtboard id="wv-sec" label="B · 分區捲動（兩段並列）" width={VW} height={VH} style={vFrame}>
        <VFr><window.WVSectioned/></VFr>
      </window.DCArtboard>
      <window.DCArtboard id="wv-cards" label="C · 篩選器卡片橫滑 + 自選清單" width={VW} height={VH} style={vFrame}>
        <VFr><window.WVCards/></VFr>
      </window.DCArtboard>
      <window.DCArtboard id="wv-tabs" label="D · 頂部 Tab Bar（Material 上分頁）" width={VW} height={VH} style={vFrame}>
        <VFr><window.WVTopTabs/></VFr>
      </window.DCArtboard>
    </window.DCSection>
  </window.DesignCanvas>
);
