"use client" import * as React from "react" import { CircleAlertIcon, Loader2Icon } from "lucide-react" import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert" import { Button } from "@/components/ui/button" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { Field, FieldGroup, FieldLabel } from "@/components/ui/field" import { Input } from "@/components/ui/input" import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { createAccountAction, updateAccountAction, type AccountInput, type AccountWithChannels, } from "@/lib/actions/account" type AccountType = "BANK" | "E_WALLET" | "CASH" const accountLabels = { BANK: "银行账户", E_WALLET: "电子钱包", CASH: "现金", } as const const balanceLabels = { ASSET: "资产类", LIABILITY: "负债类", EQUITY: "权益类", } as const export function AccountDialog({ open, onOpenChange, account, onSuccess, }: { open: boolean onOpenChange: (open: boolean) => void account?: AccountWithChannels | null onSuccess: () => void }) { const editing = Boolean(account) const [accountType, setAccountType] = React.useState( account?.accountType || "BANK" ) const [balanceType, setBalanceType] = React.useState< "ASSET" | "LIABILITY" | "EQUITY" >(account?.balanceType || "ASSET") const [name, setName] = React.useState(account?.name || "") const [primaryCurrency, setPrimaryCurrency] = React.useState( account?.primaryCurrency || "" ) const [supportedCurrencies, setSupportedCurrencies] = React.useState( account?.supportedCurrencies?.join(", ") || "" ) const [remark, setRemark] = React.useState(account?.remark || "") const [issuerName, setIssuerName] = React.useState(account?.issuerName || "") const [accountNumber, setAccountNumber] = React.useState( account?.accountNumber || "" ) const [platform, setPlatform] = React.useState(account?.platform || "") const [accountId, setAccountId] = React.useState(account?.accountId || "") const [location, setLocation] = React.useState(account?.location || "") const [isPending, setIsPending] = React.useState(false) const [errorMessage, setErrorMessage] = React.useState(null) const submit = async (event: React.FormEvent) => { event.preventDefault() setErrorMessage(null) if (!name.trim()) { setErrorMessage("请输入账户名称") return } const currencies = supportedCurrencies .split(/[,,\s]+/) .map((item) => item.trim().toUpperCase()) .filter(Boolean) const payload: AccountInput = { name: name.trim(), accountType, balanceType, primaryCurrency: primaryCurrency.trim().toUpperCase() || null, supportedCurrencies: currencies.length ? currencies : null, remark: remark.trim() || null, issuerName: accountType === "BANK" ? issuerName.trim() || null : null, accountNumber: accountType === "BANK" ? accountNumber.trim() || null : null, platform: accountType === "E_WALLET" ? platform.trim() || null : null, accountId: accountType === "E_WALLET" ? accountId.trim() || null : null, location: accountType === "CASH" ? location.trim() || null : null, } setIsPending(true) try { const result = editing && account ? await updateAccountAction(account.id, payload) : await createAccountAction(payload) if (!result.success) { setErrorMessage(result.error || "保存失败,请检查输入") return } onOpenChange(false) onSuccess() } catch { setErrorMessage("网络异常,提交失败") } finally { setIsPending(false) } } return ( {editing ? "编辑资金账户" : "新建资金账户"} 填写账户主体、分类及其识别信息。
{errorMessage && ( 无法保存账户 {errorMessage} )} 账户类型 余额分类 账户名称 setName(event.target.value)} placeholder="例如:招商银行个人消费卡账户" disabled={isPending} required /> 主币种 setPrimaryCurrency(event.target.value)} placeholder="例如:HKD" disabled={isPending} /> 支持币种 setSupportedCurrencies(event.target.value) } placeholder="用逗号分隔" disabled={isPending} /> {accountType === "BANK" && ( <> 银行机构 setIssuerName(event.target.value)} placeholder="例如:汇丰银行" disabled={isPending} /> 账户号码或尾号 setAccountNumber(event.target.value)} placeholder="可填写完整账号或尾号" disabled={isPending} /> )} {accountType === "E_WALLET" && ( <> 平台名称 setPlatform(event.target.value)} placeholder="例如:支付宝" disabled={isPending} /> 平台账户标识 setAccountId(event.target.value)} placeholder="手机号、邮箱或会员号" disabled={isPending} /> )} {accountType === "CASH" && ( 存放位置 setLocation(event.target.value)} placeholder="例如:随身钱包" disabled={isPending} /> )} 备注 setRemark(event.target.value)} placeholder="可选" disabled={isPending} />
) }