'use client'; import useGlobalStore from '@/config/use-global'; import { getPaymentPlatform } from '@/services/admin/payment'; import { zodResolver } from '@hookform/resolvers/zod'; import { useQuery } from '@tanstack/react-query'; import { Button } from '@workspace/ui/components/button'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@workspace/ui/components/form'; import { RadioGroup, RadioGroupItem } from '@workspace/ui/components/radio-group'; import { ScrollArea } from '@workspace/ui/components/scroll-area'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@workspace/ui/components/select'; import { Sheet, SheetContent, SheetFooter, SheetHeader, SheetTitle, SheetTrigger, } from '@workspace/ui/components/sheet'; import { MarkdownEditor } from '@workspace/ui/custom-components/editor'; import { EnhancedInput } from '@workspace/ui/custom-components/enhanced-input'; import { Icon } from '@workspace/ui/custom-components/icon'; import { unitConversion } from '@workspace/ui/utils'; import { useTranslations } from 'next-intl'; import { useEffect, useState } from 'react'; import { useForm } from 'react-hook-form'; import * as z from 'zod'; interface PaymentFormProps { trigger: React.ReactNode; title: string; loading?: boolean; initialValues?: T; onSubmit: (values: T) => Promise; isEdit?: boolean; } export default function PaymentForm({ trigger, title, loading, initialValues, onSubmit, isEdit, }: PaymentFormProps) { const t = useTranslations('payment'); const { common } = useGlobalStore(); const { currency } = common; const [open, setOpen] = useState(false); const { data: platformData } = useQuery({ queryKey: ['getPaymentPlatform'], queryFn: async () => { const { data } = await getPaymentPlatform(); return data?.data?.list || []; }, }); const formSchema = z.object({ name: z.string().min(1, { message: t('nameRequired') }), platform: z.string().optional(), icon: z.string().optional(), domain: z.string().optional(), config: z.any(), fee_mode: z.number().min(0).max(2), fee_percent: z.number().optional(), fee_amount: z.number().optional(), description: z.string().optional(), }); const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { name: '', platform: '', icon: '', domain: '', config: {}, fee_mode: 0, fee_percent: 0, fee_amount: 0, ...(initialValues as any), }, }); const feeMode = form.watch('fee_mode'); const platformValue = form.watch('platform'); const configValues = form.watch('config'); const currentPlatform = platformData?.find((p) => p.platform === platformValue); const currentFieldDescriptions = currentPlatform?.platform_field_description || {}; const configFields = Object.keys(currentFieldDescriptions) || []; const platformUrl = currentPlatform?.platform_url || ''; useEffect(() => { if (feeMode === 0) { form.setValue('fee_amount', 0); form.setValue('fee_percent', 0); } else if (feeMode === 1) { form.setValue('fee_amount', 0); } else if (feeMode === 2) { form.setValue('fee_percent', 0); } }, [feeMode, form]); const handleClose = () => { form.reset(); setOpen(false); }; const handleSubmit = async (values: z.infer) => { const cleanedValues = { ...values }; if (values.fee_mode === 0) { cleanedValues.fee_amount = undefined; cleanedValues.fee_percent = undefined; } else if (values.fee_mode === 1) { cleanedValues.fee_amount = undefined; } else if (values.fee_mode === 2) { cleanedValues.fee_percent = undefined; } const success = await onSubmit(cleanedValues as unknown as T); if (success) { handleClose(); } }; const openPlatformUrl = () => { if (platformUrl) { window.open(platformUrl, '_blank'); } }; return ( {trigger} {title}
( {t('name')} form.setValue('name', value as string)} /> )} /> ( {t('icon')} form.setValue('icon', value as string)} /> )} />
( {t('domain')} form.setValue('domain', value as string)} /> )} />
( {t('handlingFee')} field.onChange(parseInt(value))} value={field.value.toString()} className='flex flex-wrap gap-4' > {t('noFee')} {t('percentFee')} {t('fixedFee')} )} /> {feeMode === 1 && (
( {t('feePercent')} )} />
)} {feeMode === 2 && (
( {t('feeAmount')} field.onChange(unitConversion('dollarsToCents', value)) } /> )} />
)}
{(!platformValue || platformData?.find((p) => p.platform === platformValue)) && ( ( {t('platform')} {platformUrl ? (
) : (
)}
)} /> )} {configFields.length > 0 && (
{configFields.map((fieldKey) => ( {currentFieldDescriptions[fieldKey]} { const newConfig = { ...configValues }; newConfig[fieldKey] = value; form.setValue('config', newConfig); }} /> ))}
)} ( {t('description')} form.setValue(field.name, value as string)} /> )} />
); }