import { DeleteButton, EditButton } from '@/components/shared/Button'; import { DEFAULT_PAGE_SIZE } from '@/constants'; import { apiAddShip, apiDeleteShip, apiQueryShips, apiUpdateShip, } from '@/services/slave/sgw/ShipController'; import { PlusOutlined } from '@ant-design/icons'; import ProCard from '@ant-design/pro-card'; import ProDescriptions from '@ant-design/pro-descriptions'; import { FooterToolbar } from '@ant-design/pro-layout'; import ProTable, { ActionType, ProColumns } from '@ant-design/pro-table'; import { FormattedMessage, useIntl, useModel } from '@umijs/max'; import { Button, Drawer, message, Typography } from 'antd'; import { useEffect, useRef, useState } from 'react'; import EditModal from './components/EditModal'; import FormAdd from './components/FormAdd'; const { Paragraph } = Typography; type ShipFormValues = Partial; type ShipFormAdd = Partial; const ManagerShips: React.FC = () => { const intl = useIntl(); const { initialState } = useModel('@@initialState'); const { currentUserProfile } = initialState || {}; console.log('Current User Profile:', currentUserProfile); const [responsive] = useState(false); const [updateModalVisible, handleUpdateModalVisible] = useState(false); const [createModalVisible, handleCreateModalVisible] = useState(false); const [showDetail, setShowDetail] = useState(false); const [currentRow, setCurrentRow] = useState( null, ); const actionRef = useRef(null); const [messageApi, contextHolder] = message.useMessage(); const [selectedRowsState, setSelectedRowsState] = useState< SgwModel.ShipDetail[] >([]); const { shipTypes, getShipTypes } = useModel('slave.sgw.useShipTypes'); const { homeports, getHomeportsByProvinceCode } = useModel( 'slave.sgw.useHomePorts', ); useEffect(() => { if (!shipTypes) { getShipTypes(); } }, [shipTypes]); useEffect(() => { getHomeportsByProvinceCode(); }, [getHomeportsByProvinceCode]); useEffect(() => { return () => { setSelectedRowsState([]); setCurrentRow(null); actionRef.current = null; }; }, []); const handleAdd = async (fields: ShipFormAdd) => { const key = 'add_ship'; const { reg_number, name, // Changed from shipname to name thing_id, ship_type, ship_length, ship_power, ship_group_id, home_port, fishing_license_number, fishing_license_expiry_date, } = fields; console.log('Fields received from form:', fields); if (!thing_id || thing_id.length === 0) { message.error('Vui lòng chọn một thiết bị để liên kết.'); return false; } const newShip = { name: name, // Use name directly reg_number: reg_number, thing_id: thing_id, ship_type: ship_type, home_port: home_port, fishing_license_number: fishing_license_number, fishing_license_expiry_date: fishing_license_expiry_date, ship_length: Number(ship_length), ship_power: Number(ship_power), ship_group_id: ship_group_id, }; try { messageApi.open({ type: 'loading', content: intl.formatMessage({ id: 'pages.things.creating', defaultMessage: 'creating...', }), duration: 0, key, }); const id = await apiAddShip({ ...newShip }); if (id) { messageApi.open({ type: 'success', content: intl.formatMessage({ id: 'pages.things.add.success', defaultMessage: 'Added successfully and will refresh soon', }), key, }); return true; } return false; } catch (error) { messageApi.open({ type: 'error', content: intl.formatMessage({ id: 'pages.things.add.failed', defaultMessage: 'Adding failed, please try again!', }), key, }); return false; } }; const handleRemove = async ( selectedRows: SgwModel.ShipDetail[], ): Promise => { const key = 'remove_ship'; if (!selectedRows) return true; try { messageApi.open({ type: 'loading', content: intl.formatMessage({ id: 'pages.ships.deleting', defaultMessage: 'Deleting...', }), duration: 0, key, }); const allDelete = selectedRows.map(async (row) => { if (row.id) await apiDeleteShip(row.id); }); await Promise.all(allDelete); messageApi.open({ type: 'success', content: intl.formatMessage({ id: 'pages.ships.delete.success', defaultMessage: 'Deleted successfully and will refresh soon', }), key, }); return true; } catch (error) { messageApi.open({ type: 'error', content: intl.formatMessage({ id: 'pages.ships.delete.failed', defaultMessage: 'Delete failed, please try again!', }), key, }); return false; } }; const handleUpdate = async (values: ShipFormValues): Promise => { const key = 'update_ship'; try { messageApi.open({ type: 'loading', content: intl.formatMessage({ id: 'pages.ships.updating', defaultMessage: 'Updating...', }), duration: 0, key, }); // Fix: loại bỏ ship_group_id nếu là null const patch = { ...values }; if (patch.ship_group_id === null) delete patch.ship_group_id; if (!currentRow?.id) throw new Error('Missing ship id'); await apiUpdateShip(currentRow.id, patch as SgwModel.ShipUpdateParams); messageApi.open({ type: 'success', content: intl.formatMessage({ id: 'pages.ships.update.success', defaultMessage: 'Updated successfully and will refresh soon', }), key, }); return true; } catch (error) { messageApi.open({ type: 'error', content: intl.formatMessage({ id: 'pages.ships.update.failed', defaultMessage: 'Update failed, please try again!', }), key, }); return false; } }; const columns: ProColumns[] = [ { key: 'reg_number', title: ( ), dataIndex: 'reg_number', render: (_, record) => ( { setCurrentRow(record); setShowDetail(true); }} > {record?.reg_number} ), }, { key: 'name', title: ( ), dataIndex: 'name', render: (dom, record) => ( {record?.name} ), }, { key: 'ship_type', title: , dataIndex: 'ship_type', render: (dom, record) => { const typeObj = Array.isArray(shipTypes) ? shipTypes.find((t) => t.id === record?.ship_type) : undefined; return typeObj?.name || record?.ship_type || '-'; }, }, { key: 'home_port', title: ( ), dataIndex: 'home_port', hideInSearch: true, render: (dom, record) => { const portObj = Array.isArray(homeports) ? homeports.find((p) => p.id === record?.home_port) : undefined; return portObj?.name || record?.home_port || '-'; }, }, { title: ( ), hideInSearch: true, render: (dom, record) => ( { setCurrentRow(record); handleUpdateModalVisible(true); }} /> ), }, ]; // Định nghĩa tạm detailColumns cho ProDescriptions const detailColumns = [ { title: 'Tên tàu', dataIndex: 'name' }, { title: 'Chiều dài tàu', dataIndex: 'ship_length' }, { title: 'Công suất tàu', dataIndex: 'ship_power' }, { title: 'Đội tàu', dataIndex: 'group_ship' }, { title: 'Ảnh tàu', dataIndex: 'reg_number' }, ]; return ( <> {contextHolder} row.id!) .filter(Boolean), onChange: ( _: React.Key[], selectedRows: SgwModel.ShipDetail[], ) => { setSelectedRowsState(selectedRows); }, }} pagination={{ pageSize: DEFAULT_PAGE_SIZE * 2 }} request={async (params = {}) => { const { current = 1, pageSize, name, registration_number, } = params as { current?: number; pageSize?: number; name?: string; registration_number?: string; }; const size = pageSize || DEFAULT_PAGE_SIZE * 2; const offset = current === 1 ? 0 : (current - 1) * size; const query: SgwModel.ShipQueryParams = { offset: offset, limit: size, order: 'name', dir: 'asc', name, registration_number, }; const resp = await apiQueryShips(query); return { data: resp.ships || [], success: true, total: resp.total || 0, }; }} toolBarRender={() => { return [ , ]; }} /> { console.log('index.tsx onSubmit called with values:', values); const success = await handleAdd(values); console.log('index.tsx onSubmit - success:', success); if (success) { handleCreateModalVisible(false); if (actionRef.current) { actionRef.current.reload(); } } return success; }} /> {currentRow && ( { handleUpdateModalVisible(visible); if (!visible) setCurrentRow(null); }} onFinish={async (values: ShipFormValues) => { const success = await handleUpdate(values); if (success) { handleUpdateModalVisible(false); setCurrentRow(null); actionRef.current?.reload(); } return success; }} /> )} {selectedRowsState?.length > 0 && ( {' '} {selectedRowsState.length}{' '} } > { const success = await handleRemove(selectedRowsState); if (success) { setSelectedRowsState([]); if (actionRef.current) { actionRef.current.reload(); } } }} /> )} } onClose={() => { setShowDetail(false); setCurrentRow(null); }} > column={1} bordered request={async () => ({ data: currentRow ? [currentRow] : [], success: true, })} params={{ id: currentRow?.id }} columns={detailColumns} /> ); }; export default ManagerShips;