import { Col, Form, Input, Modal, Row, Select } from 'antd'; import { service } from '@/pages/device/Instance'; import { useEffect, useState } from 'react'; import { useIntl } from '@@/plugin-locale/localeExports'; import { UploadImage } from '@/components'; import { debounce } from 'lodash'; import encodeQuery from '@/utils/encodeQuery'; import { onlyMessage } from '@/utils/util'; import { PlusOutlined } from '@ant-design/icons'; import SaveProductModal from '@/pages/media/Device/Save/SaveProduct'; import { DeviceInstance } from '@/pages/device/Instance/typings'; interface Props { close: (data: DeviceInstance | undefined) => void; reload?: () => void; model?: 'add' | 'edit'; data?: Partial; } const defaultImage = '/images/device-type-3-big.png'; const Save = (props: Props) => { const { close, data } = props; const [productList, setProductList] = useState([]); const [loading, setLoading] = useState(false); const [visible, setVisible] = useState(false); const [form] = Form.useForm(); useEffect(() => { if (data && Object.keys(data).length) { form.setFieldsValue({ ...data }); } else { form.setFieldsValue({ photoUrl: defaultImage, }); } }, [data]); const intl = useIntl(); useEffect(() => { service .getProductList( encodeQuery({ sorts: { createTime: 'desc', }, terms: { state: 1, accessProvider: 'official-edge-gateway', }, }), ) .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) { onlyMessage('保存成功'); 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} >
{ setVisible(true); }} >
{ setVisible(false); }} reload={(productId: string, name: string) => { form.setFieldsValue({ productId }); productList.push({ id: productId, name, }); setProductList([...productList]); }} />
); }; export default Save;