| import React, { useState, useEffect } from "react"; | 
| import { | 
|     Dialog, | 
|     DialogActions, | 
|     DialogContent, | 
|     DialogTitle, | 
|     Stack, | 
|     Grid, | 
|     TextField, | 
|     Box, | 
|     Button, | 
|     Paper, | 
|     styled | 
| } from '@mui/material'; | 
| import { EDIT_MODE, DEFAULT_START_PAGE, DEFAULT_PAGE_SIZE, REFERENCE_INPUT_PAGESIZE } from '@/config/setting'; | 
| import DialogCloseButton from "../../components/DialogCloseButton"; | 
| import { useTranslate, useNotify, useRefresh } from 'react-admin'; | 
| import request from '@/utils/request'; | 
| import { DataGrid } from '@mui/x-data-grid'; | 
| import SaveIcon from '@mui/icons-material/Save'; | 
| import TreeSelectInput from "@/page/components/TreeSelectInput"; | 
| const MatnrInfoModal = (props) => { | 
|     const { open, setOpen, data, setData } = props; | 
|   | 
|     const translate = useTranslate(); | 
|     const notify = useNotify(); | 
|     const refresh = useRefresh(); | 
|   | 
|     const handleClose = (event, reason) => { | 
|         if (reason !== "backdropClick") { | 
|             setOpen(false); | 
|         } | 
|     }; | 
|   | 
|     const [formData, setFormData] = useState({}); | 
|     const [tableData, setTableData] = useState([]); | 
|     const [dyFields, setDyFields] = useState([]); | 
|     const [selectedRows, setSelectedRows] = useState([]); | 
|     const [page, setPage] = useState({ page: DEFAULT_START_PAGE, pageSize: DEFAULT_PAGE_SIZE }); | 
|     const [rowCount, setRowCount] = useState(0); | 
|     const [isLoading, setIsLoading] = useState(false); | 
|     const handleChange = (e) => { | 
|         const { name, value } = e.target; | 
|         setFormData(() => ({ | 
|             [name]: value | 
|         })); | 
|     }; | 
|   | 
|     const reset = () => { | 
|         setFormData({ | 
|             name: null, | 
|             code: null, | 
|             groupId: null | 
|         }) | 
|     } | 
|   | 
|     const handleSubmit = () => { | 
|         const hasarr = data.map(el => +el.matnrId) | 
|         const selectedData = selectedRows.filter(item => !hasarr.includes(item)).map(id => (tableData.find(row => row.id === id))); | 
|         const value = selectedData.map((el => { | 
|             const dynamicFields = dyFields.reduce((acc, item) => { | 
|                 acc[item.fields] = el['extendFields']?.[item.fields] || ''; | 
|                 return acc; | 
|             }, {}); | 
|             return { | 
|                 matnrId: el.id, | 
|                 maktx: el.name, | 
|                 matnrCode: el.code, | 
|                 stockUnit: el.stockUnit || '', | 
|                 purUnit: el.purchaseUnit || '', | 
|                 ...dynamicFields | 
|             } | 
|         })) | 
|         setData([...data, ...value]); | 
|         setOpen(false); | 
|         reset(); | 
|     }; | 
|   | 
|     const getData = async () => { | 
|         setIsLoading(true) | 
|         const res = await request.post(`/matnr/page`, { | 
|             ...formData, | 
|             current: page?.page, | 
|             pageSize: page?.pageSize, | 
|             orderBy: "create_time desc" | 
|         }); | 
|         if (res?.data?.code === 200) { | 
|             setTableData(res.data.data.records); | 
|             setRowCount(res.data?.data?.total); | 
|   | 
|         } else { | 
|             notify(res.data.msg); | 
|         } | 
|         setIsLoading(false) | 
|     }; | 
|   | 
|     useEffect(() => { | 
|         getData(); | 
|     }, [open, page]); | 
|   | 
|     const handleSearch = () => { | 
|         getData() | 
|     }; | 
|   | 
|     return ( | 
|         <Dialog | 
|             open={open} | 
|             onClose={handleClose} | 
|             aria-labelledby="form-dialog-title" | 
|             fullWidth | 
|             disableRestoreFocus | 
|             maxWidth="lg" | 
|         > | 
|             <DialogTitle id="form-dialog-title" sx={{ | 
|                 position: 'sticky', | 
|                 top: 0, | 
|                 backgroundColor: 'background.paper', | 
|                 zIndex: 1000 | 
|             }}> | 
|                 {translate("common.action.newAddMats")} | 
|                 <Box sx={{ position: 'absolute', top: 8, right: 8, zIndex: 1001 }}> | 
|                     <DialogCloseButton onClose={handleClose} /> | 
|                 </Box> | 
|             </DialogTitle> | 
|             <DialogContent sx={{ mt: 2 }}> | 
|                 <Box component="form" onSubmit={handleSubmit} sx={{ display: 'flex', flexDirection: 'column', gap: 3 }}> | 
|                     <Grid container spacing={2}> | 
|                         <Grid item md={4}> | 
|                             <TextField | 
|                                 label={translate('table.field.matnr.name')} | 
|                                 name="name" | 
|                                 value={formData.name} | 
|                                 onChange={handleChange} | 
|                                 size="small" | 
|                             /> | 
|                         </Grid> | 
|                         <Grid item md={4}> | 
|                             <TextField | 
|                                 label={translate('table.field.matnr.code')} | 
|                                 name="code" | 
|                                 value={formData.code} | 
|                                 onChange={handleChange} | 
|                                 size="small" | 
|                             /> | 
|                         </Grid> | 
|                         <Grid item md={4}> | 
|                             <TreeSelectInput | 
|                                 label="table.field.matnr.groupId" | 
|                                 value={formData.groupId} | 
|                                 resource={'matnrGroup'} | 
|                                 source="groupId" | 
|                                 name="groupId" | 
|                                 onChange={handleChange} | 
|                             /> | 
|                         </Grid> | 
|                     </Grid> | 
|                 </Box> | 
|                 <Box sx={{ mt: 2 }}> | 
|                     <Stack direction="row" spacing={2}> | 
|                         <Button variant="contained" onClick={handleSearch}>搜索</Button> | 
|                     </Stack> | 
|                 </Box> | 
|                 <Box sx={{ mt: 2, height: 400, width: '100%' }}> | 
|                     <AsnWareModalTable | 
|                         tableData={tableData} | 
|                         setTableData={setTableData} | 
|                         dyFields={dyFields} | 
|                         setDyFields={setDyFields} | 
|                         page={page} | 
|                         rowCount={rowCount} | 
|                         setPage={setPage} | 
|                         isLoading={isLoading} | 
|                         selectedRows={selectedRows} | 
|                         setSelectedRows={setSelectedRows} | 
|                     /> | 
|                 </Box> | 
|             </DialogContent> | 
|             <DialogActions sx={{ position: 'sticky', bottom: 0, backgroundColor: 'background.paper', zIndex: 1000 }}> | 
|                 <Box sx={{ width: '100%', display: 'flex', justifyContent: 'space-between' }}> | 
|                     <Button onClick={handleSubmit} variant="contained" startIcon={<SaveIcon />}> | 
|                         {translate('toolbar.confirm')} | 
|                     </Button> | 
|                 </Box> | 
|             </DialogActions> | 
|         </Dialog> | 
|     ); | 
| }; | 
|   | 
| export default MatnrInfoModal; | 
|   | 
| const AsnWareModalTable = ({ tableData, page, isLoading, pageSize, setPage, rowCount, setTableData, selectedRows, setSelectedRows, dyFields, setDyFields }) => { | 
|     const translate = useTranslate(); | 
|     const notify = useNotify(); | 
|   | 
|     const [columns, setColumns] = useState([ | 
|         // { field: 'id', headerName: 'ID', width: 100 }, | 
|         { field: 'name', headerName: translate('table.field.matnr.name'), width: 300 }, | 
|         { field: 'code', headerName: translate('table.field.matnr.code'), width: 200 }, | 
|         { field: 'groupId$', headerName: translate('table.field.matnr.groupId'), width: 100 }, | 
|         { field: 'spec', headerName: translate('table.field.matnr.spec'), width: 100 }, | 
|         { field: 'model', headerName: translate('table.field.matnr.model'), width: 100 }, | 
|         { field: 'weight', headerName: translate('table.field.matnr.weight'), width: 100 }, | 
|         { field: 'describle', headerName: translate('table.field.matnr.describle'), width: 100 }, | 
|         { field: 'nromNum', headerName: translate('table.field.matnr.nromNum'), width: 100 }, | 
|         { field: 'unit', headerName: translate('table.field.matnr.unit'), width: 100 }, | 
|         { field: 'purchaseUnit', headerName: translate('table.field.matnr.purUnit'), width: 100 }, | 
|         { field: 'stockUnit', headerName: translate('table.field.matnr.stockUnit'), width: 100 }, | 
|         { field: 'stockLeval$', headerName: translate('table.field.matnr.stockLevel'), width: 100, sortable: false }, | 
|     ]) | 
|   | 
|   | 
|     const handleSelectionChange = (ids) => { | 
|         setSelectedRows(ids) | 
|   | 
|     }; | 
|   | 
|     useEffect(() => { | 
|         getDynamicFields(); | 
|     }, []); | 
|   | 
|     const getDynamicFields = async () => { | 
|         const { | 
|             data: { code, data, msg }, | 
|         } = await request.get("/fields/enable/list"); | 
|         if (code === 200) { | 
|             const cols = data.map(el => ({ | 
|                 field: el.fields, | 
|                 headerName: el.fieldsAlise, | 
|                 minWidth: 100, | 
|                 flex: 1, | 
|                 editable: el.unique, | 
|                 valueGetter: (value, row) => { | 
|                     return row.extendFields?.[el.fields] || ''; | 
|                 }, | 
|             })) | 
|             setDyFields(data) | 
|             setColumns([...columns, ...cols]) | 
|         } else { | 
|             notify(msg); | 
|         } | 
|     } | 
|   | 
|     return ( | 
|         <div style={{ height: 400, width: '100%' }}> | 
|             <DataGrid | 
|                 size="small" | 
|                 rows={tableData} | 
|                 columns={columns} | 
|                 checkboxSelection | 
|                 onRowSelectionModelChange={handleSelectionChange} | 
|                 selectionModel={selectedRows} | 
|                 disableColumnMenu={true} | 
|                 disableColumnSorting | 
|                 disableMultipleColumnsSorting | 
|                 rowCount={rowCount} | 
|                 paginationMode="server" | 
|                 paginationModel={page} | 
|                 onPaginationModelChange={setPage} | 
|                 loading={isLoading} | 
|                 slotProps={{ | 
|                     loadingOverlay: { | 
|                         variant: 'linear-progress', | 
|                         noRowsVariant: 'linear-progress', | 
|                     }, | 
|                 }} /> | 
|         </div> | 
|     ); | 
| }; |