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'; import IconFont from '@/components/iconfont'; import get from 'lodash/get'; const PROPERTIES = [ { name: '命令', value: 'command' }, { name: '名称', value: 'name' }, { name: '定时规则', value: 'schedule' }, { name: '状态', value: 'status' }, { name: '标签', value: 'labels' }, ]; const EOperation: any = { Reg: '', NotReg: '', In: 'select', Nin: 'select', }; const OPERATIONS = [ { name: '包含', value: 'Reg' }, { name: '不包含', value: 'NotReg' }, { name: '属于', value: 'In', type: 'select' }, { name: '不属于', value: 'Nin', type: 'select' }, // { name: '等于', value: 'Eq' }, // { name: '不等于', value: 'Ne' }, // { name: '为空', value: 'IsNull' }, // { name: '不为空', value: 'NotNull' }, ]; const SORTTYPES = [ { name: '顺序', value: 'ASC' }, { name: '倒序', value: 'DESC' }, ]; const STATUS_MAP = { status: [ { name: '运行中', value: 0 }, { name: '空闲中', value: 1 }, { name: '已禁用', value: 2 }, ], }; enum ViewFilterRelation { 'and' = '且', 'or' = '或', } const ViewCreateModal = ({ view, handleCancel, visible, }: { view?: any; visible: boolean; handleCancel: (param?: any) => void; }) => { const [form] = Form.useForm(); const [loading, setLoading] = useState(false); const [filterRelation, setFilterRelation] = useState<'and' | 'or'>('and'); const handleOk = async (values: any) => { setLoading(true); values.filterRelation = filterRelation; 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 = (property: keyof typeof STATUS_MAP) => { return ( ); }; return ( { form .validateFields() .then((values) => { handleOk(values); }) .catch((info) => { console.log('Validate Failed:', info); }); }} onCancel={() => handleCancel()} confirmLoading={loading} >
{(fields, { add, remove }) => (
1 ? 'active' : '' }`} > {fields.length > 1 && (
)}
{fields.map(({ key, name, ...restField }, index) => ( 1 ? { width: 'calc(100% - 40px)' } : {} } > {propertyElement(PROPERTIES, { width: 90 })} {operationElement} { const preOperation = EOperation[ get(prevValues, ['filters', name, 'operation']) ]; const nextOperation = EOperation[ get(nextValues, ['filters', name, 'operation']) ]; const flag = preOperation !== nextOperation; if (flag) { form.setFieldValue( ['filters', name, 'value'], nextOperation === 'select' ? [] : '', ); } return flag; }} > {() => { const property = form.getFieldValue([ 'filters', index, 'property', ]) as 'status'; const operate = form.getFieldValue([ 'filters', name, 'operation', ]); return ( {EOperation[operate] === 'select' ? ( statusElement(property) ) : ( )} ); }} {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;