import React, { useEffect, useState } from 'react';
import { App, Button, Drawer, Form, Input, Popconfirm, Select, Space, Switch, Table, Tag, Tooltip } from 'antd';
import { DeleteOutlined, EditOutlined, PlusOutlined, ReloadOutlined, SaveOutlined, SettingOutlined } from '@ant-design/icons';
import apiClient from '../../utils/apiClient';
import { API_ENDPOINTS, buildApiUrl } from '../../config/api';
import AdminModuleShell from '../../components/AdminModuleShell';
import ActionButton from '../../components/ActionButton';
import StatusTag from '../../components/StatusTag';
const ParameterManagement = () => {
const { message } = App.useApp();
const [items, setItems] = useState([]);
const [loading, setLoading] = useState(false);
const [drawerOpen, setDrawerOpen] = useState(false);
const [editing, setEditing] = useState(null);
const [submitting, setSubmitting] = useState(false);
const [form] = Form.useForm();
const fetchItems = async () => {
setLoading(true);
try {
const res = await apiClient.get(buildApiUrl(API_ENDPOINTS.ADMIN.PARAMETERS));
setItems(res.data.items || []);
} catch {
message.error('获取参数列表失败');
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchItems();
}, []);
const openCreate = () => {
setEditing(null);
form.setFieldsValue({
param_key: '',
param_name: '',
param_value: '',
value_type: 'string',
category: 'system',
description: '',
is_active: true,
});
setDrawerOpen(true);
};
const openEdit = (row) => {
setEditing(row);
form.setFieldsValue({
...row,
is_active: Boolean(row.is_active),
});
setDrawerOpen(true);
};
const submit = async () => {
const values = await form.validateFields();
setSubmitting(true);
try {
if (editing) {
await apiClient.put(buildApiUrl(API_ENDPOINTS.ADMIN.PARAMETER_DETAIL(editing.param_key)), values);
message.success('参数更新成功');
} else {
await apiClient.post(buildApiUrl(API_ENDPOINTS.ADMIN.PARAMETERS), values);
message.success('参数创建成功');
}
setDrawerOpen(false);
fetchItems();
} catch (error) {
message.error(error?.response?.data?.message || '参数保存失败');
} finally {
setSubmitting(false);
}
};
const columns = [
{ title: '参数键', dataIndex: 'param_key', key: 'param_key', width: 220 },
{ title: '参数名', dataIndex: 'param_name', key: 'param_name', width: 180 },
{ title: '值', dataIndex: 'param_value', key: 'param_value' },
{ title: '类型', dataIndex: 'value_type', key: 'value_type', width: 100, render: (v) =>