'use client'; import { Display } from '@/components/display'; import { ProTable, ProTableActions } from '@/components/pro-table'; import { createPaymentMethod, deletePaymentMethod, getPaymentMethodList, updatePaymentMethod, } from '@/services/admin/payment'; import { Avatar, AvatarFallback, AvatarImage } from '@workspace/ui/components/avatar'; import { Badge } from '@workspace/ui/components/badge'; import { Button } from '@workspace/ui/components/button'; import { Switch } from '@workspace/ui/components/switch'; import { ConfirmButton } from '@workspace/ui/custom-components/confirm-button'; import { useTranslations } from 'next-intl'; import { useRef, useState } from 'react'; import { toast } from 'sonner'; import PaymentForm from './payment-form'; export default function PaymentTable() { const t = useTranslations('payment'); const [loading, setLoading] = useState(false); const ref = useRef(null); return ( action={ref} header={{ title: t('paymentManagement'), toolbar: ( trigger={} title={t('createPayment')} loading={loading} onSubmit={async (values) => { setLoading(true); try { await createPaymentMethod({ ...values, enable: false, }); toast.success(t('createSuccess')); ref.current?.refresh(); setLoading(false); return true; } catch (error) { setLoading(false); return false; } }} /> ), }} columns={[ { accessorKey: 'enable', header: t('enable'), cell: ({ row }) => { return ( { await updatePaymentMethod({ ...row.original, enable: checked, }); ref.current?.refresh(); }} /> ); }, }, { accessorKey: 'icon', header: t('icon'), cell: ({ row }) => { const icon = row.getValue('icon') as string; return ( {icon ? : null} {(row.getValue('name') as string)?.charAt(0) || '?'} ); }, }, { accessorKey: 'name', header: t('name'), }, { accessorKey: 'platform', header: t('platform'), cell: ({ row }) => {t(row.original.platform)}, }, { accessorKey: 'notify_url', header: t('notify_url'), }, { accessorKey: 'fee', header: t('handlingFee'), cell: ({ row }) => { const feeMode = row.original.fee_mode; if (feeMode === 1) { return {row.original.fee_percent}%; } else if (feeMode === 2) { return ( ); } return '--'; }, }, ]} params={[ { key: 'search', placeholder: t('searchPlaceholder'), }, ]} request={async (pagination, filter) => { const { data } = await getPaymentMethodList({ ...pagination, ...filter, }); return { list: data?.data?.list || [], total: data?.data?.total || 0, }; }} actions={{ render: (row) => [ isEdit key='edit' trigger={} title={t('editPayment')} loading={loading} initialValues={row} onSubmit={async (values) => { setLoading(true); try { await updatePaymentMethod({ ...row, ...values, }); toast.success(t('updateSuccess')); ref.current?.refresh(); setLoading(false); return true; } catch (error) { setLoading(false); return false; } }} />, {t('delete')}} title={t('confirmDelete')} description={t('deleteWarning')} onConfirm={async () => { await deletePaymentMethod({ id: row.id, }); toast.success(t('deleteSuccess')); ref.current?.refresh(); }} cancelText={t('cancel')} confirmText={t('confirm')} />, , ], batchRender(rows) { return [ {t('batchDelete')}} title={t('confirmDelete')} description={t('deleteWarning')} onConfirm={async () => { for (const row of rows) { await deletePaymentMethod({ id: row.id }); } toast.success(t('deleteSuccess')); ref.current?.refresh(); }} cancelText={t('cancel')} confirmText={t('confirm')} />, ]; }, }} /> ); }