import {Col, Form, Input, message, Row, Select} from 'antd'; import {service} from '@/pages/device/Instance'; import type {DeviceInstance} from '../typings'; import {useEffect, useState} from 'react'; import {useIntl} from '@@/plugin-locale/localeExports'; import {Modal, UploadImage} from '@/components'; import {debounce} from 'lodash'; interface Props { visible: boolean; close: (data: DeviceInstance | undefined) => void; reload?: () => void; model?: 'add' | 'edit'; data?: Partial; } const Save = (props: Props) => { const { visible, close, data } = props; const [productList, setProductList] = useState([]); const [loading, setLoading] = useState(false); const [form] = Form.useForm(); useEffect(() => { if (visible && data) { form.setFieldsValue({ ...data, }); } }, [visible]); const intl = useIntl(); useEffect(() => { service.getProductList({ paging: false }).then((resp: any) => { if (resp.status === 200) { const list = resp.result.map((item: { name: any; id: any }) => ({ label: item.name, value: item.id, })); setProductList(list); } }); }, []); const intlFormat = ( id: string, defaultMessage: string, paramsID?: string, paramsMessage?: string, ) => { const paramsObj: Record = {}; if (paramsID) { const paramsMsg = intl.formatMessage({ id: paramsID, defaultMessage: paramsMessage, }); paramsObj.name = paramsMsg; } return intl.formatMessage( { id, defaultMessage, }, paramsObj, ); }; const handleSave = async () => { const values = await form.validateFields(); if (values) { if (values.id === '') { delete values.id; } setLoading(true); const resp = (await service.update(values)) as any; setLoading(false); if (resp.status === 200) { message.success('保存成功'); if (props.reload) { props.reload(); } props.close(values); form.resetFields(); } } }; const vailId = (_: any, value: any, callback: Function) => { if (props.model === 'add' && value) { service.isExists(value).then((resp: any) => { if (resp.status === 200 && resp.result) { callback( intl.formatMessage({ id: 'pages.form.tip.existsID', defaultMessage: 'ID重复', }), ); } else { callback(); } }); } else { callback(); } }; return ( { form.resetFields(); close(undefined); }} width="580px" title={intl.formatMessage({ id: `pages.data.option.${props.model || 'add'}`, defaultMessage: '新增', })} confirmLoading={loading} onOk={handleSave} permissionCode={'device/Instance'} permission={'add'} >
); }; export default Save;