/* ============================================================
   screens-extra.jsx — 品牌故事 / 會員 / 結帳 / 完成
   ============================================================ */

/* ---- 品牌故事 ---- */
function AboutScreen() {
  const app = useApp();
  return (
    <div>
      <Wrap style={{ paddingTop: 48, paddingBottom: 8 }}>
        <div style={{ textAlign: 'center', display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 14 }}>
          <Tag tone="leaf">EST. 2018 · 台灣 · 山林小農</Tag>
          <div style={{ fontSize: '140px', lineHeight: 1, fontFamily: 'var(--font-display)', fontWeight: 800, color: 'var(--fg-1)' }}>🍌</div>
          <h1 style={{ fontSize: '48px', lineHeight: 1.15 }}>把一根香蕉,<br />好好種、好好送到你家</h1>
          <p style={{ maxWidth: 600, fontSize: 17, lineHeight: 1.8, color: 'var(--fg-2)' }}>
            「蕉您來好康」是一群相信土地的農人。我們在海拔 800 公尺的山坡地友善耕作,不催熟、不搶快,
            只為了讓你嚐到香蕉本來該有的綿密與香甜。
          </p>
        </div>
      </Wrap>

      <Wrap>
        <div style={{ display: 'grid', gridTemplateColumns: '260px 1fr', gap: 36, alignItems: 'start', background: 'var(--bg-cream)', border: '1px solid var(--border)', borderRadius: 'var(--radius-lg)', padding: '40px 44px' }}>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 18 }}>
            {[['🏔️', '海拔 800m', '山坡地栽種'], ['🌱', '友善耕作', '減藥草生'], ['🚚', '48 小時', '產地到府'], ['📋', '產銷履歷', '全程可溯源']].map(([e, a, b]) => (
              <div key={a} style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
                <div style={{ fontSize: 30 }}>{e}</div>
                <div>
                  <div style={{ fontWeight: 800, fontFamily: 'var(--font-display)', color: 'var(--fg-1)' }}>{a}</div>
                  <div style={{ fontSize: 13, color: 'var(--fg-3)' }}>{b}</div>
                </div>
              </div>
            ))}
          </div>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
            <h2 style={{ fontSize: '28px' }}>我們的堅持</h2>
            <p style={{ fontSize: 15, lineHeight: 1.9, color: 'var(--fg-2)' }}>
              市場上多數香蕉為了賣相與運送,會提早採收、用藥催熟。我們選擇相反的路——讓香蕉在欉上自然黃熟,
              採收後直接走冷鏈宅配。雖然成本更高、損耗更大,但這是我們認為對的事。
            </p>
            <p style={{ fontSize: 15, lineHeight: 1.9, color: 'var(--fg-2)' }}>
              每一箱出貨的水果都附上產銷履歷,你可以清楚知道它是誰、在哪裡、怎麼種出來的。
              支持我們,就是支持一塊被溫柔對待的土地,以及一群願意慢慢來的農人。
            </p>
            <div style={{ marginTop: 6 }}>
              <Button variant="dark" size="lg" onClick={() => app.navigate('shop')}>逛逛我們的商品 <i className="ph-bold ph-arrow-right"></i></Button>
            </div>
          </div>
        </div>
      </Wrap>
    </div>
  );
}

/* ---- 會員(登入 / 後台)---- */
function AccountScreen() {
  const app = useApp();
  if (!app.user) return <LoginCard />;
  return (
    <Wrap>
      <div style={{ display: 'grid', gridTemplateColumns: '260px 1fr', gap: 32, alignItems: 'start' }}>
        <aside style={{ background: 'var(--bg-surface)', border: '1px solid var(--border)', borderRadius: 'var(--radius)', padding: 20, display: 'flex', flexDirection: 'column', gap: 6 }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 12, padding: '6px 6px 16px' }}>
            <div style={{ width: 48, height: 48, borderRadius: '50%', background: 'var(--accent-soft)', display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 24 }}>🧑‍🌾</div>
            <div>
              <div style={{ fontWeight: 800, color: 'var(--fg-1)' }}>{app.user.name}</div>
              <div style={{ fontSize: 12, color: 'var(--fg-3)' }}>{app.user.email}</div>
            </div>
          </div>
          {[['ph-package', '我的訂單'], ['ph-arrows-clockwise', '訂閱管理'], ['ph-map-pin', '收件地址'], ['ph-gear', '帳號設定']].map(([i, l]) => (
            <a key={l} style={{ display: 'flex', alignItems: 'center', gap: 10, padding: '11px 12px', borderRadius: 10, color: 'var(--fg-2)', fontWeight: 600, cursor: 'pointer' }}
              onMouseEnter={(e) => (e.currentTarget.style.background = 'var(--bg-sunk)')}
              onMouseLeave={(e) => (e.currentTarget.style.background = 'transparent')}>
              <i className={'ph ' + i} style={{ fontSize: 18 }}></i> {l}
            </a>
          ))}
          <div style={{ height: 1, background: 'var(--border)', margin: '8px 0' }} />
          <a onClick={app.logout} style={{ display: 'flex', alignItems: 'center', gap: 10, padding: '11px 12px', borderRadius: 10, color: 'var(--danger)', fontWeight: 600, cursor: 'pointer' }}>
            <i className="ph ph-sign-out" style={{ fontSize: 18 }}></i> 登出
          </a>
        </aside>

        <div style={{ display: 'flex', flexDirection: 'column', gap: 20 }}>
          <h2 style={{ fontSize: '28px' }}>嗨,{app.user.name} 👋</h2>
          <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 16 }}>
            {[['12', '累積訂單'], ['NT$8,640', '消費總額'], ['340', '好康紅利']].map(([n, l]) => (
              <div key={l} style={{ background: 'var(--bg-surface)', border: '1px solid var(--border)', borderRadius: 'var(--radius)', padding: '22px 20px' }}>
                <div style={{ fontSize: 26, fontWeight: 900, fontFamily: 'var(--font-display)', color: 'var(--fg-1)' }}>{n}</div>
                <div style={{ fontSize: 13, color: 'var(--fg-3)' }}>{l}</div>
              </div>
            ))}
          </div>
          <div style={{ background: 'var(--bg-surface)', border: '1px solid var(--border)', borderRadius: 'var(--radius)', padding: 24 }}>
            <h3 style={{ fontSize: 18, marginBottom: 16 }}>最近的訂單</h3>
            {[['#10231', '有機山蕉 × 2、香蕉脆片 × 1', '已送達', 'var(--success)'], ['#10198', '每週鮮果箱', '配送中', 'var(--accent-strong)']].map(([no, items, st, c]) => (
              <div key={no} style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '14px 0', borderBottom: '1px solid var(--border)' }}>
                <div>
                  <div style={{ fontWeight: 700, color: 'var(--fg-1)' }}>{no}</div>
                  <div style={{ fontSize: 13, color: 'var(--fg-3)' }}>{items}</div>
                </div>
                <span style={{ color: c, fontWeight: 700, fontSize: 14 }}>{st}</span>
              </div>
            ))}
          </div>
        </div>
      </div>
    </Wrap>
  );
}

function LoginCard() {
  const app = useApp();
  const [name, setName] = useState('');
  const [email, setEmail] = useState('');
  const submit = (e) => {
    e.preventDefault();
    app.login({ name: name.trim() || '好康會員', email: email.trim() || 'guest@banana.farm' });
  };
  return (
    <Wrap style={{ maxWidth: 920 }}>
      <div className="login-card" style={{ display: 'grid', gridTemplateColumns: '1.05fr .95fr', borderRadius: 'var(--radius-lg)', overflow: 'hidden', border: '1px solid var(--border)', boxShadow: 'var(--shadow-card)', margin: '24px 0' }}>
        <div className="login-brand" style={{ background: 'linear-gradient(160deg, #2C2820, #4a4030)', color: '#fff', padding: '48px 44px', minHeight: 460, display: 'flex', flexDirection: 'column', justifyContent: 'space-between' }}>
          <Brand compact />
          <div style={{ display: 'flex', flexDirection: 'column', gap: 14 }}>
            <div style={{ fontSize: 56 }}>🍌</div>
            <h2 style={{ fontSize: '30px', color: '#fff', lineHeight: 1.25 }}>歡迎回來,<br />一起來好康!</h2>
            <p style={{ color: '#C9BFA9', fontSize: 15, lineHeight: 1.7 }}>登入即可累積好康紅利、管理訂閱鮮果箱,並追蹤每一筆訂單的配送進度。</p>
          </div>
          <div style={{ fontSize: 13, color: '#A99F8C' }}>🔒 本站為設計示範,不會儲存任何真實資料</div>
        </div>
        <form onSubmit={submit} style={{ background: 'var(--bg-surface)', padding: '48px 44px', display: 'flex', flexDirection: 'column', gap: 18, justifyContent: 'center' }}>
          <h2 style={{ fontSize: '26px' }}>會員登入 / 註冊</h2>
          <Field label="暱稱"><Input value={name} onChange={(e) => setName(e.target.value)} placeholder="怎麼稱呼你?" /></Field>
          <Field label="電子郵件"><Input type="email" value={email} onChange={(e) => setEmail(e.target.value)} placeholder="you@example.com" /></Field>
          <Field label="密碼"><Input type="password" placeholder="••••••••" /></Field>
          <Button type="submit" variant="primary" size="lg" full>登入</Button>
          <div style={{ textAlign: 'center', fontSize: 13, color: 'var(--fg-3)' }}>或使用</div>
          <div style={{ display: 'flex', gap: 10 }}>
            <Button variant="ghost" full onClick={submit}><i className="ph ph-google-logo"></i> Google</Button>
            <Button variant="ghost" full onClick={submit}><i className="ph ph-line-logo"></i> LINE</Button>
          </div>
        </form>
      </div>
    </Wrap>
  );
}

/* ---- 結帳 ---- */
function CheckoutScreen() {
  const app = useApp();
  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;
  const submit = (e) => { e.preventDefault(); app.placeOrder(); };

  if (items.length === 0) {
    return (
      <Wrap>
        <div style={{ textAlign: 'center', padding: '80px 0', display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 16 }}>
          <div style={{ fontSize: 64 }}>🧺</div>
          <h2 style={{ fontSize: 26 }}>購物車是空的</h2>
          <Button variant="dark" size="lg" onClick={() => app.navigate('shop')}>去逛逛</Button>
        </div>
      </Wrap>
    );
  }

  return (
    <Wrap>
      <div style={{ display: 'flex', alignItems: 'center', gap: 8, fontSize: 14, color: 'var(--fg-3)', marginBottom: 18, cursor: 'pointer' }} onClick={() => app.navigate('shop')}>
        <i className="ph ph-arrow-left"></i> 繼續購物
      </div>
      <h1 style={{ fontSize: '36px', marginBottom: 24 }}>結帳</h1>
      <section style={{ display: 'grid', gridTemplateColumns: '1.5fr 1fr', gap: 32, alignItems: 'start' }}>
        <form onSubmit={submit} style={{ display: 'flex', flexDirection: 'column', gap: 22 }}>
          <div style={{ background: 'var(--bg-surface)', border: '1px solid var(--border)', borderRadius: 'var(--radius)', padding: 24, display: 'flex', flexDirection: 'column', gap: 16 }}>
            <h3 style={{ fontSize: 18 }}>收件資訊</h3>
            <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 14 }}>
              <Field label="姓名"><Input required placeholder="王小蕉" /></Field>
              <Field label="手機"><Input required placeholder="0912-345-678" /></Field>
            </div>
            <Field label="電子郵件"><Input type="email" required placeholder="you@example.com" /></Field>
            <Field label="配送地址"><Input required placeholder="台中市 ... 路 ... 號" /></Field>
            <Field label="備註(選填)"><Input placeholder="指定配送時段、其他需求…" /></Field>
          </div>
          <div style={{ background: 'var(--bg-surface)', border: '1px solid var(--border)', borderRadius: 'var(--radius)', padding: 24, display: 'flex', flexDirection: 'column', gap: 12 }}>
            <h3 style={{ fontSize: 18 }}>付款方式</h3>
            {[['ph-credit-card', '信用卡 / 行動支付'], ['ph-truck', '貨到付款(+30 元手續費)'], ['ph-bank', 'ATM 轉帳']].map(([i, l], idx) => (
              <label key={l} style={{ display: 'flex', alignItems: 'center', gap: 12, padding: '12px 14px', border: '1px solid var(--border-strong)', borderRadius: 12, cursor: 'pointer' }}>
                <input type="radio" name="pay" defaultChecked={idx === 0} />
                <i className={'ph ' + i} style={{ fontSize: 20, color: 'var(--fg-2)' }}></i>
                <span style={{ fontWeight: 600 }}>{l}</span>
              </label>
            ))}
          </div>
        </form>

        <div style={{ background: 'var(--bg-cream)', border: '1px solid var(--border)', borderRadius: 'var(--radius)', padding: 24, display: 'flex', flexDirection: 'column', gap: 14, position: 'sticky', top: 90 }}>
          <h3 style={{ fontSize: 18 }}>訂單摘要</h3>
          {items.map((it) => (
            <div key={it.p.id} style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
              <div style={{ width: 48, height: 48, borderRadius: 12, background: `linear-gradient(135deg, ${it.p.grad[0]}, ${it.p.grad[1]})`, display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 24, flexShrink: 0 }}>{it.p.emoji}</div>
              <div style={{ flex: 1 }}>
                <div style={{ fontWeight: 700, fontSize: 14, color: 'var(--fg-1)' }}>{it.p.name}</div>
                <div style={{ fontSize: 12, color: 'var(--fg-3)' }}>數量 {it.qty}</div>
              </div>
              <span style={{ fontWeight: 800, fontFamily: 'var(--font-display)' }}>{NT(it.p.price * it.qty)}</span>
            </div>
          ))}
          <div style={{ height: 1, background: 'var(--border)' }} />
          <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" size="lg" full onClick={submit}>確認下單 <i className="ph-bold ph-check"></i></Button>
          <div style={{ fontSize: 12, color: 'var(--fg-3)', textAlign: 'center' }}>點擊下單即代表同意服務條款(示範用途)</div>
        </div>
      </section>
    </Wrap>
  );
}

/* ---- 完成 ---- */
function SuccessScreen() {
  const app = useApp();
  const o = app.lastOrder || { no: '#00000', total: 0 };
  return (
    <Wrap>
      <div style={{ maxWidth: 540, margin: '80px auto', textAlign: 'center', background: 'var(--bg-surface)', border: '1px solid var(--border)', borderRadius: 'var(--radius-lg)', padding: '48px 40px', boxShadow: 'var(--shadow-card)', display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 16 }}>
        <div style={{ width: 88, height: 88, borderRadius: '50%', background: 'var(--leaf-soft)', display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 48 }}>🎉</div>
        <h1 style={{ fontSize: '32px' }}>訂單成立!</h1>
        <p style={{ color: 'var(--fg-2)', fontSize: 16, lineHeight: 1.7 }}>
          謝謝你的支持 🍌 我們已收到訂單,將盡快為你新鮮採收、冷鏈出貨。
        </p>
        <div style={{ width: '100%', background: 'var(--bg-cream)', borderRadius: 'var(--radius)', padding: '18px 20px', display: 'flex', flexDirection: 'column', gap: 10 }}>
          <Row label="訂單編號" value={o.no} />
          <Row label="付款金額" value={NT(o.total)} />
          <Row label="預計到貨" value="48 小時內" />
        </div>
        <div style={{ display: 'flex', gap: 12, marginTop: 4 }}>
          <Button variant="dark" size="lg" onClick={() => app.navigate('account')}>查看訂單</Button>
          <Button variant="ghost" size="lg" onClick={() => app.navigate('shop')}>繼續購物</Button>
        </div>
      </div>
    </Wrap>
  );
}
