/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
For commercial licensing, please contact support@quantumnous.com
*/
import { useEffectEvent } from '../../hooks/common/useEffectEvent';
import React, { useState, useEffect } from 'react';
import {
Form,
Button,
Table,
Modal,
Space,
Card,
Typography,
Tag,
Popconfirm,
} 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);
await 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();
} else {
showError(res.data.message);
}
} catch (error) {
showError(t('保存失败'));
}
};
return (
{t(title)}
} onClick={handleAdd}>
{t('新增档位')}
{/* 编辑/新增弹窗 */}
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;