import React, { useEffect, useState } from 'react'; import { Modal, message, Input, Form, Statistic, Button, Space, Select, } from 'antd'; import { request } from '@/utils/http'; import config from '@/utils/config'; import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons'; const PROPERTIES = [ { name: '命令', value: 'command' }, { name: '名称', value: 'name' }, { name: '定时规则', value: 'schedule' }, { name: '状态', value: 'status' }, ]; const OPERATIONS = [ { name: '包含', value: 'Reg' }, { name: '不包含', value: 'NotReg' }, { name: '属于', value: 'In' }, { name: '不属于', value: 'Nin' }, // { name: '等于', value: 'Eq' }, // { name: '不等于', value: 'Ne' }, // { name: '为空', value: 'IsNull' }, // { name: '不为空', value: 'NotNull' }, ]; const SORTTYPES = [ { name: '顺序', value: 'ASC' }, { name: '倒序', value: 'DESC' }, ]; const STATUS = [ { name: '运行中', value: 0 }, { name: '空闲中', value: 1 }, { name: '已禁用', value: 2 }, ]; const ViewCreateModal = ({ view, handleCancel, visible, }: { view?: any; visible: boolean; handleCancel: (param?: any) => void; }) => { const [form] = Form.useForm(); const [loading, setLoading] = useState(false); const [operationMap, setOperationMap] = useState(); const handleOk = async (values: any) => { setLoading(true); const method = view ? 'put' : 'post'; try { const { code, data } = await request[method]( `${config.apiPrefix}crons/views`, { data: view ? { ...values, id: view.id } : values, }, ); if (code === 200) { handleCancel(data); } setLoading(false); } catch (error: any) { setLoading(false); } }; useEffect(() => { if (!view) { form.resetFields(); } form.setFieldsValue( view || { filters: [{ property: 'command', operation: 'Reg' }], }, ); }, [view, visible]); const operationElement = ( ); const propertyElement = (props: any, style: React.CSSProperties = {}) => { return ( ); }; const typeElement = ( ); const statusElement = ( ); return ( { form .validateFields() .then((values) => { handleOk(values); }) .catch((info) => { console.log('Validate Failed:', info); }); }} onCancel={() => handleCancel()} confirmLoading={loading} >
{(fields, { add, remove }) => ( <> {fields.map(({ key, name, ...restField }, index) => ( {propertyElement(PROPERTIES, { width: 120 })} {operationElement} {['In', 'Nin'].includes( form.getFieldValue(['filters', index, 'operation']), ) ? ( statusElement ) : ( )} {index !== 0 && ( remove(name)} /> )} ))} add({ property: 'command', operation: 'Reg' })} > 新增筛选条件 )} {(fields, { add, remove }) => ( <> {fields.map(({ key, name, ...restField }, index) => ( {propertyElement(PROPERTIES, { width: 240 })} {typeElement} remove(name)} /> ))} add({ property: 'command', type: 'ASC' })}> 新增排序方式 )}
); }; export default ViewCreateModal;