"use client" import { ArrowDownLeft, ArrowUpRight, CreditCard, DollarSign, RefreshCw, Wallet, } from "lucide-react" import { useState } from "react" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Separator } from "@/components/ui/separator" import { notifySuccess } from "@/lib/toast" import { useAuthStore } from "@/store/auth" import { useWalletStore } from "@/store/wallet" const typeLabels: Record = { topup: "充值", payment: "支付", income: "收入", withdrawal: "提现", refund: "退款", } const typeIcons: Record = { topup: ArrowDownLeft, payment: ArrowUpRight, income: ArrowDownLeft, withdrawal: ArrowUpRight, refund: ArrowDownLeft, } export default function WalletPage() { const { currentRole } = useAuthStore() const isConsumer = currentRole === "consumer" const balance = useWalletStore((state) => state.balance) const transactions = useWalletStore((state) => state.transactions) const topUp = useWalletStore((state) => state.topUp) const withdraw = useWalletStore((state) => state.withdraw) const [selectedAmount, setSelectedAmount] = useState(null) const [customAmount, setCustomAmount] = useState("") const [lastRefreshedAt, setLastRefreshedAt] = useState(null) const filteredTransactions = transactions.filter((tx) => { if (isConsumer) { return ["topup", "payment", "refund"].includes(tx.type) } return ["income", "withdrawal"].includes(tx.type) }) const incomeBalance = transactions .filter((tx) => ["income", "withdrawal"].includes(tx.type)) .reduce((acc, tx) => acc + tx.amount, 0) const handleTopUp = (rawAmount?: number) => { const amount = rawAmount ?? Number(customAmount) if (!Number.isFinite(amount) || amount <= 0) return topUp(amount) notifySuccess(`充值成功 +¥${amount}`) setCustomAmount("") setSelectedAmount(null) } const handleWithdraw = () => { const amount = Number(customAmount || "0") || Number(incomeBalance.toFixed(2)) if (!Number.isFinite(amount) || amount <= 0) return withdraw(amount) notifySuccess(`提现申请已提交 ¥${amount}`) setCustomAmount("") } return (

钱包

{isConsumer ? "账户余额" : "收入余额"}

¥{isConsumer ? balance.toFixed(2) : incomeBalance.toFixed(2)}

{isConsumer ? ( ) : ( )}
{isConsumer ? ( 快捷充值
{[50, 100, 200, 500, 1000, 2000].map((amount) => ( ))}
{ setCustomAmount(event.target.value) setSelectedAmount(null) }} />
) : ( 收入概览

本月收入

¥1,280.00

待结算

¥320.00

已提现

¥5,400.00

)} 交易记录 {lastRefreshedAt && (

最近刷新:{lastRefreshedAt}

)} {filteredTransactions.length > 0 ? ( filteredTransactions.map((tx) => { const Icon = typeIcons[tx.type] const isIncome = tx.amount > 0 return (

{tx.description}

{new Date(tx.createdAt).toLocaleString("zh-CN")}

{isIncome ? "+" : ""}¥{Math.abs(tx.amount).toFixed(2)}

{typeLabels[tx.type]}
) }) ) : (
暂无交易记录
)}
) }