/* ============================================================
   chrome.jsx — 頁首 / 手機選單 / 頁尾 / 購物車側欄 / 搜尋 / 通知
   ============================================================ */

/* ---- 品牌標記 ---- */
function Brand({ onClick, compact }) {
  return (
    <div onClick={onClick} style={{ display: 'flex', alignItems: 'center', gap: 10, cursor: 'pointer' }}>
      <div style={{
        width: 38, height: 38, borderRadius: 12, background: 'var(--fg-1)',
        color: 'var(--accent)', display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 20,
      }}>🍌</div>
      <div>
        <div style={{ fontSize: 16, fontWeight: 900, color: 'var(--fg-1)', fontFamily: 'var(--font-display)', lineHeight: 1 }}>蕉您來好康</div>
        {!compact && <div style={{ fontSize: 11, color: 'var(--fg-3)', letterSpacing: 3, marginTop: 2 }}>FARM SHOP</div>}
      </div>
    </div>
  );
}

/* ---- 頁首 ---- */
function Header() {
  const app = useApp();
  return (
    <header style={{
      position: 'sticky', top: 0, zIndex: 40,
      display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 20,
      padding: '14px 32px', background: 'rgba(255,251,242,.86)', backdropFilter: 'blur(10px)',
      borderBottom: '1px solid var(--border)',
    }}>
      <Brand onClick={() => app.navigate('home')} />

      {/* 桌面導覽 — 平板以下由 CSS 隱藏(header > nav) */}
      <nav style={{ display: 'flex', alignItems: 'center', gap: 28 }}>
        {NAV.map((n) => (
          <a key={n.id} onClick={() => app.navigate(n.id)}
            style={{ cursor: 'pointer', fontSize: 15, fontWeight: 600, color: app.route === n.id ? 'var(--fg-1)' : 'var(--fg-2)', position: 'relative' }}>
            {n.label}
            {app.route === n.id && <span style={{ position: 'absolute', left: 0, right: 0, bottom: -6, height: 2, background: 'var(--accent)', borderRadius: 2 }} />}
          </a>
        ))}
      </nav>

      <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
        <IconButton icon="ph-magnifying-glass" label="搜尋" onClick={app.openSearch} />
        <IconButton icon="ph-user" label="會員" onClick={() => app.navigate('account')} />
        <IconButton icon="ph-shopping-cart-simple" label="購物車" onClick={app.openCart} badge={app.cartCount} />
        <button className="mobile-menu-btn" onClick={app.openMenu} aria-label="選單"
          style={{
            display: 'none', width: 42, height: 42, borderRadius: '50%',
            border: '1px solid var(--border)', background: 'var(--bg-surface)',
            alignItems: 'center', justifyContent: 'center', color: 'var(--fg-1)', fontSize: 22,
          }}>
          <i className="ph ph-list"></i>
        </button>
      </div>
    </header>
  );
}

/* ---- 手機選單 ---- */
function MobileMenu() {
  const app = useApp();
  if (!app.menuOpen) return null;
  return (
    <div onClick={app.closeMenu} style={{ position: 'fixed', inset: 0, zIndex: 55, background: 'rgba(0,0,0,.35)', animation: 'fadeIn .2s ease' }}>
      <div onClick={(e) => e.stopPropagation()} style={{
        background: 'var(--bg-page)', padding: '18px 22px 26px',
        borderBottomLeftRadius: 22, borderBottomRightRadius: 22, animation: 'fadeIn .2s ease',
      }}>
        <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 14 }}>
          <Brand compact />
          <IconButton icon="ph-x" label="關閉" onClick={app.closeMenu} />
        </div>
        <nav style={{ display: 'flex', flexDirection: 'column' }}>
          {NAV.map((n) => (
            <a key={n.id} onClick={() => app.navigate(n.id)}
              style={{ padding: '14px 4px', fontSize: 18, fontWeight: 700, color: 'var(--fg-1)', borderBottom: '1px solid var(--border)', cursor: 'pointer' }}>
              {n.label}
            </a>
          ))}
        </nav>
      </div>
    </div>
  );
}

/* ---- 頁尾 ---- */
function Footer() {
  const app = useApp();
  return (
    <footer style={{ background: 'var(--fg-1)', color: '#EFE7D6', marginTop: 72 }}>
      <div style={{ maxWidth: 1200, margin: '0 auto', padding: '56px 32px', display: 'grid', gridTemplateColumns: '2fr 1fr 1fr 1fr', gap: 40 }}>
        <div style={{ display: 'flex', flexDirection: 'column', gap: 14, maxWidth: 320 }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
            <div style={{ width: 38, height: 38, borderRadius: 12, background: 'var(--accent)', color: 'var(--accent-ink)', display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 20 }}>🍌</div>
            <div style={{ fontSize: 18, fontWeight: 900, fontFamily: 'var(--font-display)', color: '#fff' }}>蕉您來好康農場</div>
          </div>
          <p style={{ fontSize: 14, lineHeight: 1.7, color: '#C9BFA9' }}>
            來自台灣山林的友善小農。我們相信好好種出來的香蕉,值得好好被吃掉。
          </p>
          <div style={{ display: 'flex', gap: 10, marginTop: 4 }}>
            {['ph-facebook-logo', 'ph-instagram-logo', 'ph-line-logo', 'ph-youtube-logo'].map((i) => (
              <span key={i} style={{ width: 38, height: 38, borderRadius: '50%', background: 'rgba(255,255,255,.08)', display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 18, cursor: 'pointer' }}>
                <i className={'ph ' + i}></i>
              </span>
            ))}
          </div>
        </div>
        {FOOTER_LINKS.map((col) => (
          <div key={col.title} style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
            <div style={{ fontSize: 14, fontWeight: 800, color: '#fff', fontFamily: 'var(--font-display)' }}>{col.title}</div>
            {col.links.map((l) => (
              <a key={l} onClick={() => app.navigate('shop')} style={{ fontSize: 14, color: '#C9BFA9', cursor: 'pointer' }}>{l}</a>
            ))}
          </div>
        ))}
      </div>
      <div style={{ borderTop: '1px solid rgba(255,255,255,.1)', padding: '18px 32px', textAlign: 'center', fontSize: 13, color: '#A99F8C' }}>
        © 2026 蕉您來好康農場 · 本站為設計示範,非真實交易
      </div>
    </footer>
  );
}

/* ---- 購物車側欄 ---- */
function CartDrawer() {
  const app = useApp();
  if (!app.cartOpen) return null;
  const items = app.cart;
  const subtotal = items.reduce((s, it) => s + it.p.price * it.qty, 0);
  const shipping = subtotal >= 600 || subtotal === 0 ? 0 : 80;
  return (
    <div onClick={app.closeCart} style={{ position: 'fixed', inset: 0, zIndex: 60, background: 'rgba(0,0,0,.4)', animation: 'fadeIn .2s ease' }}>
      <div onClick={(e) => e.stopPropagation()} style={{
        position: 'absolute', top: 0, right: 0, height: '100%', width: '440px', maxWidth: '100vw',
        background: 'var(--bg-page)', display: 'flex', flexDirection: 'column',
        animation: 'slideIn .3s ease', boxShadow: '-12px 0 44px rgba(0,0,0,.22)',
      }}>
        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '20px 24px', borderBottom: '1px solid var(--border)' }}>
          <h3 style={{ fontSize: 20 }}>購物車 <span style={{ color: 'var(--fg-3)', fontSize: 15 }}>({app.cartCount})</span></h3>
          <IconButton icon="ph-x" label="關閉" onClick={app.closeCart} />
        </div>

        <div style={{ flex: 1, overflowY: 'auto', padding: '12px 24px' }}>
          {items.length === 0 && (
            <div style={{ textAlign: 'center', color: 'var(--fg-3)', padding: '64px 0', display: 'flex', flexDirection: 'column', gap: 12, alignItems: 'center' }}>
              <div style={{ fontSize: 56 }}>🧺</div>
              <div>購物車是空的</div>
              <Button variant="dark" onClick={() => { app.closeCart(); app.navigate('shop'); }}>去逛逛</Button>
            </div>
          )}
          {items.map((it) => (
            <div key={it.p.id} style={{ display: 'flex', gap: 14, padding: '14px 0', borderBottom: '1px solid var(--border)' }}>
              <div style={{ width: 64, height: 64, borderRadius: 14, background: `linear-gradient(135deg, ${it.p.grad[0]}, ${it.p.grad[1]})`, display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 30, flexShrink: 0 }}>{it.p.emoji}</div>
              <div style={{ flex: 1, display: 'flex', flexDirection: 'column', gap: 6 }}>
                <div style={{ display: 'flex', justifyContent: 'space-between', gap: 8 }}>
                  <span style={{ fontWeight: 700, color: 'var(--fg-1)' }}>{it.p.name}</span>
                  <i className="ph ph-trash" onClick={() => app.removeFromCart(it.p.id)} style={{ cursor: 'pointer', color: 'var(--fg-3)', fontSize: 18 }}></i>
                </div>
                <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
                  <QtyStepper value={it.qty} onChange={(q) => app.setQty(it.p.id, q)} />
                  <span style={{ fontWeight: 800, fontFamily: 'var(--font-display)' }}>{NT(it.p.price * it.qty)}</span>
                </div>
              </div>
            </div>
          ))}
        </div>

        {items.length > 0 && (
          <div style={{ padding: '18px 24px 24px', borderTop: '1px solid var(--border)', background: 'var(--bg-cream)', display: 'flex', flexDirection: 'column', gap: 10 }}>
            <Row label="小計" value={NT(subtotal)} />
            <Row label="運費" value={shipping === 0 ? '免運' : NT(shipping)} />
            <div style={{ height: 1, background: 'var(--border)' }} />
            <Row label="總計" value={NT(subtotal + shipping)} big />
            <Button variant="dark" full size="lg" onClick={app.goCheckout}>前往結帳 <i className="ph-bold ph-arrow-right"></i></Button>
          </div>
        )}
      </div>
    </div>
  );
}
function Row({ label, value, big }) {
  return (
    <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline' }}>
      <span style={{ color: 'var(--fg-2)', fontSize: big ? 16 : 14, fontWeight: big ? 800 : 500 }}>{label}</span>
      <span style={{ color: 'var(--fg-1)', fontWeight: 800, fontFamily: 'var(--font-display)', fontSize: big ? 22 : 15 }}>{value}</span>
    </div>
  );
}

/* ---- 搜尋遮罩 ---- */
function SearchOverlay() {
  const app = useApp();
  const [q, setQ] = useState('');
  if (!app.searchOpen) return null;
  const res = q.trim()
    ? PRODUCTS.filter((p) => (p.name + p.blurb + p.detail).toLowerCase().includes(q.trim().toLowerCase()))
    : PRODUCTS.filter((p) => p.hot);
  return (
    <div onClick={app.closeSearch} style={{ position: 'fixed', inset: 0, zIndex: 60, background: 'rgba(20,16,8,.5)', backdropFilter: 'blur(4px)', animation: 'fadeIn .2s ease' }}>
      <div onClick={(e) => e.stopPropagation()} style={{ maxWidth: 640, margin: '80px auto 0', padding: '0 16px' }}>
        <div style={{ background: 'var(--bg-surface)', borderRadius: 20, boxShadow: 'var(--shadow-card)', overflow: 'hidden' }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 12, padding: '18px 22px', borderBottom: '1px solid var(--border)' }}>
            <i className="ph ph-magnifying-glass" style={{ fontSize: 22, color: 'var(--fg-3)' }}></i>
            <input autoFocus value={q} onChange={(e) => setQ(e.target.value)} placeholder="搜尋香蕉、果醬、鮮果箱…"
              style={{ flex: 1, border: 'none', outline: 'none', fontSize: 17, background: 'transparent', color: 'var(--fg-1)', fontFamily: 'var(--font-ui)' }} />
            <i className="ph ph-x" onClick={app.closeSearch} style={{ cursor: 'pointer', color: 'var(--fg-3)', fontSize: 20 }}></i>
          </div>
          <div style={{ maxHeight: '50vh', overflowY: 'auto', padding: 10 }}>
            <div style={{ fontSize: 12, color: 'var(--fg-3)', padding: '6px 12px', fontWeight: 700 }}>{q.trim() ? `找到 ${res.length} 項` : '熱門推薦'}</div>
            {res.map((p) => (
              <div key={p.id} onClick={() => { app.openProduct(p.id); app.closeSearch(); }}
                style={{ display: 'flex', alignItems: 'center', gap: 14, padding: '10px 12px', borderRadius: 12, cursor: 'pointer' }}
                onMouseEnter={(e) => (e.currentTarget.style.background = 'var(--bg-sunk)')}
                onMouseLeave={(e) => (e.currentTarget.style.background = 'transparent')}>
                <div style={{ width: 46, height: 46, borderRadius: 12, background: `linear-gradient(135deg, ${p.grad[0]}, ${p.grad[1]})`, display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 24 }}>{p.emoji}</div>
                <div style={{ flex: 1 }}>
                  <div style={{ fontWeight: 700, color: 'var(--fg-1)' }}>{p.name}</div>
                  <div style={{ fontSize: 13, color: 'var(--fg-3)' }}>{p.blurb}</div>
                </div>
                <span style={{ fontWeight: 800, fontFamily: 'var(--font-display)' }}>{NT(p.price)}</span>
              </div>
            ))}
            {res.length === 0 && <div style={{ textAlign: 'center', color: 'var(--fg-3)', padding: '40px 0' }}>找不到「{q}」相關商品</div>}
          </div>
        </div>
      </div>
    </div>
  );
}

/* ---- 通知 ---- */
function Toast() {
  const app = useApp();
  if (!app.toast) return null;
  return (
    <div style={{
      position: 'fixed', right: 24, bottom: 24, zIndex: 80,
      background: 'var(--fg-1)', color: '#fff', padding: '14px 20px', borderRadius: 14,
      boxShadow: 'var(--shadow-card)', fontWeight: 700, fontSize: 15,
      display: 'flex', alignItems: 'center', gap: 10, animation: 'slideIn .3s ease',
    }}>
      <i className="ph-fill ph-check-circle" style={{ color: 'var(--accent)', fontSize: 20 }}></i>
      {app.toast}
    </div>
  );
}
