| import React, { useState, useRef, useEffect, useMemo } from "react"; | 
| import { | 
|     CreateBase, | 
|     useTranslate, | 
|     TextInput, | 
|     NumberInput, | 
|     BooleanInput, | 
|     DateInput, | 
|     SaveButton, | 
|     SelectInput, | 
|     ReferenceInput, | 
|     ReferenceArrayInput, | 
|     AutocompleteInput, | 
|     Toolbar, | 
|     required, | 
|     useDataProvider, | 
|     useNotify, | 
|     Form, | 
|     useCreateController, | 
|     SimpleForm, | 
|     useRefresh, | 
| } from 'react-admin'; | 
| import { | 
|     Dialog, | 
|     DialogActions, | 
|     DialogContent, | 
|     DialogTitle, | 
|     TextField, | 
|     IconButton, | 
|     MenuItem, | 
|     Tooltip, | 
|     Select, | 
|     Button, | 
|     Stack, | 
|     Grid, | 
|     Box, | 
| } from '@mui/material'; | 
| import ConfirmationNumberIcon from '@mui/icons-material/ConfirmationNumber'; | 
| import DialogCloseButton from "../../components/DialogCloseButton.jsx"; | 
| import WarehouseSelect from "../../components/WarehouseSelect.jsx"; | 
| import { DataGrid, useGridApiRef } from '@mui/x-data-grid'; | 
| import ConfirmButton from "../../components/ConfirmButton"; | 
| import { useFormContext, useWatch } from "react-hook-form"; | 
| import CreateBySelectMats from "./CreateBySelectMats.jsx"; | 
| import { Add, Edit, Delete } from '@mui/icons-material'; | 
| import DictSelect from "../../components/DictSelect"; | 
| import { minHeight, padding } from "@mui/system"; | 
| import SaveIcon from '@mui/icons-material/Save'; | 
| import request from '@/utils/request'; | 
| import _, { set } from 'lodash'; | 
|   | 
| const ManualCreate = (props) => { | 
|     const { open, setOpen, orderId } = props; | 
|     const tableRef = useRef(); | 
|     const notify = useNotify(); | 
|     const refresh = useRefresh(); | 
|     const translate = useTranslate(); | 
|     const [tabelData, setTableData] = useState([]); | 
|     const [disabled, setDisabled] = useState(false); | 
|     const [selectedRows, setSelectedRows] = useState([]); | 
|     const [createDialog, setCreateDialog] = useState(false); | 
|     const [formData, setFormData] = useState({ type: '1', orgAreaId: null, tarAreaId: null }); | 
|     const [list, setList] = useState([]); | 
|   | 
|     const handleClose = (event, reason) => { | 
|         if (reason !== "backdropClick") { | 
|             setOpen(false); | 
|         } | 
|     }; | 
|   | 
|     const setFinally = () => { | 
|         const rows = tableRef.current.state.editRows; | 
|         for (const key in rows) { | 
|             const find = tabelData.find(item => item.matnrId === +key); | 
|             find.anfme = rows[key].anfme.value; | 
|         } | 
|         setTableData([...tabelData]); | 
|     } | 
|   | 
|   | 
|     const handleSubmit = async () => { | 
|         setFinally() | 
|         setDisabled(true) | 
|   | 
|         if (orderId == null || orderId == undefined) { | 
|             const parmas = { | 
|                 "transfer": formData, | 
|                 "items": tabelData, | 
|             } | 
|   | 
|             const res = await request.post(`/transfer/items/save`, parmas); | 
|             if (res?.data?.code === 200) { | 
|                 setOpen(false); | 
|             } else { | 
|                 notify(res.data.msg); | 
|             } | 
|         } else { | 
|             const parmas = { | 
|                 "transfer": formData, | 
|                 "items": tabelData, | 
|             } | 
|             const res = await request.post(`/transfer/items/update`, parmas); | 
|             if (res?.data?.code === 200) { | 
|                 setOpen(false); | 
|             } else { | 
|                 notify(res.data.msg); | 
|             } | 
|         } | 
|         setDisabled(false) | 
|         refresh(); | 
|   | 
|     }; | 
|   | 
|     const handleSuccess = async (data) => { | 
|         setOpen(false); | 
|         notify('common.response.success'); | 
|     }; | 
|   | 
|     const handleDeleteItem = () => { | 
|         const newTableData = _.filter(tabelData, (item) => !selectedRows.includes(item.matnrId)); | 
|         setTableData(newTableData); | 
|     } | 
|   | 
|     const handleError = async (error) => { | 
|         notify(error.message || 'common.response.fail', { type: 'error', messageArgs: { _: error.message } }); | 
|     }; | 
|   | 
|     const getWarehouse = async () => { | 
|         const res = await request.post('/warehouseAreas/page'); | 
|         if (res?.data?.code === 200) { | 
|             setList(res.data.data.records.map((item) => { | 
|                 return { | 
|                     id: item.id, | 
|                     name: item.name | 
|                 }; | 
|             })); | 
|         } else { | 
|             notify(res.data.msg); | 
|         } | 
|     } | 
|   | 
|     const newAddClick = () => { | 
|         if (formData.orgAreaId == null || formData.orgAreaId == undefined) { | 
|             notify("原库区不能为空!!", { type: 'error' }) | 
|             return | 
|         } | 
|         if (formData.tarAreaId == null || formData.tarAreaId == undefined) { | 
|             notify("目标库区不能为空!!", { type: 'error' }) | 
|             return | 
|         } | 
|         console.log(formData); | 
|          | 
|         setCreateDialog(true) | 
|     } | 
|   | 
|   | 
|     const handleChange = (value, name) => { | 
|         setFormData((prevData) => ({ | 
|             ...prevData, | 
|             [name]: value | 
|         })); | 
|     }; | 
|   | 
|     return ( | 
|         <> | 
|             <Box sx={{ padding: 1 }}> | 
|                 <SimpleForm toolbar={false}> | 
|                     <Grid container spacing={2}> | 
|                         <Grid item md={2}> | 
|                             <DictSelect | 
|                                 label={translate("table.field.transfer.type")} | 
|                                 value={formData.type} | 
|                                 variant="filled" | 
|                                 onChange={(e) => handleChange(e.target.value, 'type')} | 
|                                 dictTypeCode="sys_transfer_type" | 
|                             /> | 
|                         </Grid> | 
|                         <Grid item md={2}> | 
|                             <WarehouseSelect | 
|                                 value={formData.orgAreaId} | 
|                                 variant="filled" | 
|                                 dictTypeCode="warehouse" | 
|                                 onChange={(e) => handleChange(e.target.value, 'orgAreaId')} | 
|                                 label={translate("table.field.transfer.orgAreaName")} /> | 
|                         </Grid> | 
|                         <Grid item md={2}> | 
|                             <WarehouseSelect | 
|                                 label={translate("table.field.transfer.tarAreaName")} | 
|                                 value={formData.tarAreaId} | 
|                                 dictTypeCode="warehouse" | 
|                                 variant="filled" | 
|                                 onChange={(e) => handleChange(e.target.value, 'tarAreaId')} | 
|                             /> | 
|                         </Grid> | 
|                     </Grid> | 
|                 </SimpleForm> | 
|                 <Box sx={{ mt: 2 }}> | 
|                     <Stack direction="row" spacing={2} sx={{ justifyContent: "flex-end" }}> | 
|                         <Button variant="contained" onClick={newAddClick} > | 
|                             {translate('common.action.newAddMats')} | 
|                         </Button> | 
|                         <ConfirmButton label={"toolbar.delete"} variant="outlined" color="error" onConfirm={handleDeleteItem} /> | 
|                     </Stack> | 
|                 </Box> | 
|                 <Box sx={{ mt: 2 }}> | 
|                     <TransferTableView | 
|                         tabelData={tabelData} | 
|                         setTableData={setTableData} | 
|                         orderId={orderId} | 
|                         selectedRows={selectedRows} | 
|                         setSelectedRows={setSelectedRows} | 
|                         tableRef={tableRef}> | 
|                     </TransferTableView> | 
|                 </Box> | 
|                 <Toolbar sx={{ width: '100%', justifyContent: 'flex-end', bgcolor: 'white' }}  > | 
|                     <Button disabled={disabled} onClick={handleSubmit} variant="contained" startIcon={<SaveIcon />}> | 
|                         {translate('toolbar.confirm')} | 
|                     </Button> | 
|                 </Toolbar> | 
|                 <CreateBySelectMats | 
|                     data={tabelData} | 
|                     queryForm={formData} | 
|                     open={createDialog} | 
|                     setOpen={setCreateDialog} | 
|                     selectedRows={selectedRows} | 
|                     setSelectedRows={setSelectedRows} | 
|                     setData={setTableData} | 
|                 /> | 
|             </Box> | 
|         </> | 
|     ) | 
| } | 
|   | 
| const SelectInputSplrNameEditCell = (params) => { | 
|     const [formData, setFormData] = useState([{}]) | 
|     useEffect(() => { | 
|         getOptions(); | 
|     }, []); | 
|     const getOptions = async () => { | 
|         const parmas = { | 
|             "type": "supplier" | 
|         } | 
|         const { | 
|             data: { code, data, msg }, | 
|         } = await request.post("companys/page", parmas); | 
|         if (code === 200) { | 
|             setFormData(data.records) | 
|         } else { | 
|             notify(msg); | 
|         } | 
|     } | 
|   | 
|     return ( | 
|         <Select | 
|             value={params.value} | 
|             onChange={(e) => { | 
|                 params.api.setEditCellValue({ | 
|                     id: params.id, | 
|                     field: params.field, | 
|                     value: e.target.value, | 
|                 }) | 
|                 // 找到选中的供应商记录 | 
|                 const selectedSupplier = formData.find(supplier => supplier.name === e.target.value); | 
|   | 
|                 // 如果找到对应的供应商记录,同时更新splrCode字段 | 
|                 if (selectedSupplier) { | 
|                     params.api.setEditCellValue({ | 
|                         id: params.id, | 
|                         field: 'splrCode', | 
|                         value: selectedSupplier.id, | 
|                     }); | 
|                 } | 
|             }} | 
|             fullWidth | 
|         > | 
|             {formData.map(e => { | 
|                 return ( | 
|                     <MenuItem value={e.name} children={e.name} key={e.id} /> | 
|                 ); | 
|   | 
|             })} | 
|   | 
|         </Select> | 
|     ); | 
| }; | 
|   | 
| const SelectInputSplrCodeEditCell = (params) => { | 
|     const [formData, setFormData] = useState([{}]) | 
|     useEffect(() => { | 
|         getOptions(); | 
|     }, []); | 
|     const getOptions = async () => { | 
|         const parmas = { | 
|             "type": "supplier" | 
|         } | 
|         const { | 
|             data: { code, data, msg }, | 
|         } = await request.post("companys/page", parmas); | 
|         if (code === 200) { | 
|             setFormData(data.records) | 
|         } else { | 
|             notify(msg); | 
|         } | 
|     } | 
|   | 
|     return ( | 
|         <Select | 
|             value={params.value} | 
|             onChange={(e) => { | 
|                 params.api.setEditCellValue({ | 
|                     id: params.id, | 
|                     field: params.field, | 
|                     value: e.target.value, | 
|                 }) | 
|                 const selectedSupplier = formData.find(supplier => supplier.id === e.target.value); | 
|   | 
|                 // 如果找到对应的供应商记录,同时更新splrCode字段 | 
|                 if (selectedSupplier) { | 
|                     params.api.setEditCellValue({ | 
|                         id: params.id, | 
|                         field: 'splrName', | 
|                         value: selectedSupplier.name, | 
|                     }); | 
|                 } | 
|             }} | 
|             fullWidth | 
|   | 
|         > | 
|             {formData.map(e => { | 
|                 return ( | 
|                     <MenuItem value={e.id} children={e.name} key={e.id} /> | 
|                 ); | 
|   | 
|             })} | 
|   | 
|         </Select> | 
|     ); | 
| }; | 
|   | 
|   | 
|   | 
| const TransferTableView = ({ tabelData, setTableData, orderId, selectedRows, setSelectedRows, tableRef }) => { | 
|     const translate = useTranslate(); | 
|     const notify = useNotify(); | 
|     const [extendColumns, setExtendColumns] = useState([]); | 
|     const [columns, setColumns] = useState([ | 
|         { | 
|             field: 'maktx', | 
|             headerName: translate('table.field.outStockItem.maktx'), | 
|             width: 250, | 
|             editable: false, | 
|         }, | 
|         { | 
|             field: 'matnrCode', | 
|             headerName: translate('table.field.outStockItem.matnrCode'), | 
|             width: 130, | 
|             editable: false, | 
|         }, | 
|         { | 
|             field: 'anfme', | 
|             headerName: translate('table.field.outStockItem.anfme'), | 
|             type: 'number', | 
|             minWidth: 100, | 
|             flex: 1, | 
|             editable: true, | 
|             valueFormatter: (val) => val < 0 ? 0 : val, | 
|         }, | 
|         { | 
|             field: 'splrCode', | 
|             headerName: translate('table.field.outStockItem.splrCode'), | 
|             minWidth: 100, | 
|             flex: 1, | 
|             editable: true, | 
|             renderEditCell: (params) => ( | 
|                 <SelectInputSplrCodeEditCell {...params} /> | 
|             ), | 
|         }, | 
|         { | 
|             field: 'splrName', | 
|             headerName: translate('table.field.outStockItem.splrName') + "*", | 
|             minWidth: 100, | 
|             flex: 1, | 
|             editable: true, | 
|             renderEditCell: (params) => ( | 
|                 <SelectInputSplrNameEditCell {...params} /> | 
|             ), | 
|         }, | 
|         { | 
|             field: 'batch', | 
|             headerName: translate('table.field.outStockItem.splrBatch'), | 
|             minWidth: 100, | 
|             flex: 1, | 
|             editable: true, | 
|         }, | 
|         { | 
|             field: 'stockUnit', | 
|             headerName: translate('table.field.outStockItem.stockUnit'), | 
|             minWidth: 100, | 
|             flex: 1, | 
|             editable: true, | 
|         }, | 
|     ]) | 
|   | 
|     const action = { | 
|         field: 'action', | 
|         headerName: '操作', | 
|         width: 70, | 
|         lockPosition: 'left', | 
|         renderCell: (params) => ( | 
|             <Tooltip title="Delete"> | 
|                 <IconButton onClick={() => handleDelete(params.row)}> | 
|                     <Delete /> | 
|                 </IconButton> | 
|             </Tooltip> | 
|         ), | 
|     } | 
|   | 
|     let cdata = useRef([]); | 
|   | 
|     useEffect(() => { | 
|         if (extendColumns == undefined || extendColumns.length < 1) { | 
|             getDynamicFields(); | 
|         } | 
|     }, []); | 
|   | 
|     useEffect(() => { | 
|         cdata.current = tabelData | 
|     }, [tabelData]); | 
|   | 
|   | 
|     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: false | 
|             })) | 
|             setColumns([...columns, ...cols, action]) | 
|             setExtendColumns(cols); | 
|         } else { | 
|             notify(msg); | 
|         } | 
|     } | 
|   | 
|   | 
|     const handleDelete = (row) => { | 
|         const newData = _.filter(cdata.current, (item) => item.matnrId !== row.matnrId); | 
|         setTableData(newData); | 
|     }; | 
|   | 
|   | 
|     const processRowUpdate = (newRow, oldRow) => { | 
|         const rows = tabelData.map((r) => | 
|             r.matnrId === newRow.matnrId ? { ...newRow } : r | 
|         ) | 
|         setTableData(rows) | 
|         return newRow; | 
|     }; | 
|   | 
|     const handleSelectionChange = (ids) => { | 
|         setSelectedRows(ids) | 
|     }; | 
|   | 
|     tableRef.current = useGridApiRef(); | 
|   | 
|     const tableIds = tabelData.map(map => map.id); | 
|     // setSelectedRows(tableIds); | 
|     // // console.log(selectedRows); | 
|   | 
|   | 
|     return ( | 
|         <Box> | 
|             <DataGrid | 
|                 apiRef={tableRef} | 
|                 rows={tabelData} | 
|                 columns={columns} | 
|                 disableRowSelectionOnClick | 
|                 initialState={{ | 
|                     pagination: { | 
|                         paginationModel: { | 
|                             pageSize: 25, | 
|                         }, | 
|                     }, | 
|                 }} | 
|                 pageSizeOptions={[15, 25, 50, 100]} | 
|                 editMode="row" | 
|                 checkboxSelection | 
|                 rowSelectionModel={tableIds} | 
|                 onRowSelectionModelChange={handleSelectionChange} | 
|                 sx={{ | 
|                     height: 580, | 
|                     '& .MuiDataGrid-cell input': { | 
|                         border: '1px solid #ccc' | 
|                     }, | 
|                 }} | 
|             /> | 
|         </Box> | 
|     ); | 
| }; | 
|   | 
|   | 
| export default ManualCreate; |