import { PlusOutlined } from '@ant-design/icons'; import { ModalForm, ProFormDateTimePicker, ProFormDigit, ProFormSelect, ProFormText, } from '@ant-design/pro-form'; import { FormattedMessage, useIntl, useModel } from '@umijs/max'; import { Button, Col, FormInstance, Row, Table } from 'antd'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; import { useEffect, useRef, useState } from 'react'; import FormShareVms from './FormShareVms'; dayjs.extend(utc); interface ShipFormValues extends SgwModel.ShipCreateParams { targets?: SgwModel.SgwThing[]; } interface FormAddProps { visible: boolean; onVisibleChange: (visible: boolean) => void; onSubmit: (values: ShipFormValues) => Promise; } const FormAdd: React.FC = ({ visible, onVisibleChange, onSubmit, }) => { const intl = useIntl(); const formRef = useRef>(); const [shareModalVisible, handleShareModalVisible] = useState(false); const [selectedDevices, setSelectedDevices] = useState( [], ); const { shipTypes, getShipTypes } = useModel('slave.sgw.useShipTypes'); const { homeports } = useModel('slave.sgw.useHomePorts'); const { groups, getGroups } = useModel('master.useGroups'); useEffect(() => { if (!shipTypes) { getShipTypes(); } }, [shipTypes]); useEffect(() => { if (!groups) { getGroups(); } }, [groups]); console.log('groups', homeports, groups); // Lọc homeports theo province_code của groups const groupProvinceCodes = Array.isArray(groups) ? groups.map((g: MasterModel.GroupNode) => g.metadata?.code).filter(Boolean) : []; const filteredHomeports = Array.isArray(homeports) ? homeports.filter((p: SgwModel.Port) => groupProvinceCodes.includes(p.province_code), ) : []; return ( { console.log('FormAdd onFinish - formValues:', formValues); console.log('FormAdd onFinish - selectedDevices:', selectedDevices); // Gửi selectedDevices vào targets const rest = formValues; const thing_id = selectedDevices?.[0]?.id; const result = await onSubmit({ ...rest, thing_id, targets: selectedDevices, }); console.log('FormAdd onFinish - result:', result); return result; }} > ({ label: t.name, value: t.id })) : [] } rules={[{ required: true, message: 'Chọn loại tàu' }]} showSearch /> ({ label: p.name, value: p.id, }))} rules={[{ required: true, message: 'Chọn cảng nhà' }]} showSearch /> { if (!value) return {}; return { fishing_license_expiry_date: dayjs(value) .endOf('day') .utc() .format(), // ISO 8601 format: YYYY-MM-DDTHH:mm:ssZ }; }} fieldProps={{ format: 'YYYY-MM-DD', }} /> {/* */} {/* Hiển thị bảng thiết bị đã chọn */} {selectedDevices.length > 0 && ( ({ key: idx, device: item.name || item, }))} columns={[ { title: 'Thiết bị', dataIndex: 'device', key: 'device' }, ]} scroll={{ y: 240 }} style={{ marginTop: 12 }} /> )} { setSelectedDevices(values.things || []); handleShareModalVisible(false); }} /> ); }; export default FormAdd;