import TableActionPopconfirm from '@/components/common/table/TableActionPopconfirm'; import { useEffectEvent } from '../../hooks/common/useEffectEvent'; import React, { useState, useEffect } from 'react'; import { Form, Button, Table, Modal, Space, Card, Typography, Tag, } from '@/components/ui'; import { IconPlus } from '@/components/ui/icons'; import { useTranslation } from 'react-i18next'; import { API, showError, showSuccess } from '../../helpers'; const { Title } = Typography; const AmountOptionsManager = ({ apiPath = '/api/user/payment/amount-options', title = '充值档位管理', }) => { const { t } = useTranslation(); const [options, setOptions] = useState([]); const [loading, setLoading] = useState(false); const [editModal, setEditModal] = useState({ visible: false, data: null }); const onScopeChange = useEffectEvent(() => { loadOptions(); }); useEffect(() => onScopeChange(), [onScopeChange, apiPath]); const loadOptions = async () => { try { setLoading(true); const res = await API.get(apiPath); if (res.data.success) { setOptions(res.data.data || []); } else { showError(t('加载失败')); } } catch (error) { showError(t('加载失败')); } finally { setLoading(false); } }; const columns = [ { title: 'ID', dataIndex: 'id', width: 60 }, { title: t('金额'), dataIndex: 'amount', render: (v) => `¥${v}`, width: 100, }, { title: t('折扣'), dataIndex: 'discount', render: (v) => t('{{rate}}折', { rate: (v * 10).toFixed(1), }), width: 80, }, { title: t('标签'), dataIndex: 'label', width: 100 }, { title: t('角标'), dataIndex: 'badge', render: (text, record) => { if (!text) return '-'; return {text}; }, width: 100, }, { title: t('赠送额度'), dataIndex: 'bonus', render: (v) => (v > 0 ? v.toLocaleString() : '-'), width: 120, }, { title: t('标记'), render: (text, record) => ( {record.is_recommend && {t('推荐')}} {record.is_hot && {t('热门')}} {record.min_vip_level > 0 && ( VIP{record.min_vip_level}+ )} ), width: 150, }, { title: t('排序'), dataIndex: 'sort', width: 70 }, { title: t('操作'), render: (text, record) => ( handleDelete(record.id)} > ), width: 150, }, ]; const handleEdit = (record) => { setEditModal({ visible: true, data: record }); }; const handleDelete = async (id) => { const newOptions = options.filter((opt) => opt.id !== id); return saveOptions(newOptions); }; const handleAdd = () => { setEditModal({ visible: true, data: { id: Math.max(...options.map((o) => o.id), 0) + 1, amount: 10, discount: 1.0, label: '', badge: '', badge_color: '#1890ff', is_recommend: false, is_hot: false, min_vip_level: 0, bonus: 0, sort: options.length + 1, }, }); }; const handleSave = async (values) => { try { setLoading(true); const newOptions = editModal.data.id && options.find((o) => o.id === editModal.data.id) ? options.map((opt) => opt.id === editModal.data.id ? { ...opt, ...values } : opt, ) : [...options, { ...values, id: editModal.data.id }]; await saveOptions(newOptions); setEditModal({ visible: false, data: null }); } catch (error) { showError(t('保存失败')); } finally { setLoading(false); } }; const saveOptions = async (newOptions) => { try { const res = await API.post(apiPath, { amount_options: newOptions.sort((a, b) => a.sort - b.sort), }); if (res.data.success) { showSuccess(t('保存成功')); loadOptions(); return true; } else { showError(res.data.message); } } catch (error) { showError(t('保存失败')); } return false; }; return (
{t(title)}
{/* 编辑/新增弹窗 */} o.id === editModal.data.id) ? t('编辑档位') : t('新增档位') } visible={editModal.visible} onCancel={() => setEditModal({ visible: false, data: null })} footer={null} width={600} // initValues 仅在挂载时生效,关闭后销毁表单以便下次按当前行初始化。 keepDOM={false} >
); }; export default AmountOptionsManager;