| import React, { useState, useEffect, useCallback, useRef } from "react"; | 
| import { | 
|     Dialog, | 
|     DialogActions, | 
|     DialogContent, | 
|     DialogTitle, | 
|     Stack, | 
|     Grid, | 
|     TextField, | 
|     Box, | 
|     Button, | 
|     Paper, | 
|     styled, | 
|     RadioGroup, | 
|     Radio, | 
|     FormControlLabel, | 
| } from '@mui/material'; | 
| import DialogCloseButton from "../../components/DialogCloseButton"; | 
| import ConfirmButton from "../../components/ConfirmButton"; | 
| import { useTranslate, useNotify, useRefresh } from 'react-admin'; | 
| import request from '@/utils/request'; | 
| import { DataGrid, useGridApiRef } from '@mui/x-data-grid'; | 
| import SaveIcon from '@mui/icons-material/Save'; | 
| import TreeSelectInput from "@/page/components/TreeSelectInput"; | 
| import { set, throttle } from 'lodash'; | 
|   | 
| const InspectModal = (props) => { | 
|     const { open, setOpen, ispectId } = props; | 
|   | 
|     const translate = useTranslate(); | 
|     const notify = useNotify(); | 
|     const refresh = useRefresh(); | 
|   | 
|     const handleClose = (event, reason) => { | 
|         if (reason !== "backdropClick") { | 
|             setOpen(false); | 
|         } | 
|     }; | 
|   | 
|     const [formData, setFormData] = useState({ | 
|         code: '' | 
|     }); | 
|   | 
|     const [tableData, setTableData] = useState([]); | 
|     const [selectedRows, setSelectedRows] = useState([]); | 
|   | 
|     const handleChange = (e) => { | 
|         const { name, value } = e.target; | 
|         setFormData((prevData) => ({ | 
|             ...prevData, | 
|             [name]: value | 
|         })); | 
|     }; | 
|   | 
|     const reset = () => { | 
|         setFormData({ | 
|             code: '' | 
|         }) | 
|     } | 
|   | 
|     const handleSubmit = async () => { | 
|         const rows = tableData.filter(el => !(el.safeQty == 0 && el.disQty == 0)) | 
|         if (rows.length) { | 
|             const { data: { code, data, msg } } = await request.post(`/qlyIsptItem/batch/update`, { isptItem: rows, type: '0' }); | 
|             if (code === 200) { | 
|                 notify(msg); | 
|                 refresh() | 
|             } else { | 
|                 notify(msg); | 
|             } | 
|         } | 
|   | 
|         setOpen(false); | 
|         reset(); | 
|     }; | 
|   | 
|     useEffect(() => { | 
|         getData(); | 
|     }, [open]); | 
|   | 
|   | 
|     const getData = async () => { | 
|         const res = await request.post(`/qlyIsptItem/ispts/page`, { ispectId }); | 
|         if (res?.data?.code === 200) { | 
|             const data = res.data.data.records.map(item => { | 
|                 return { | 
|                     ...item, | 
|                     isptResult: item.isptResult || '' | 
|                 } | 
|             }) | 
|             setTableData(data); | 
|         } else { | 
|             notify(res.data.msg); | 
|         } | 
|     }; | 
|   | 
|   | 
|   | 
|     const handleSearch = () => { | 
|         getData() | 
|     }; | 
|   | 
|     const batchQualified = async () => { | 
|         if (selectedRows.length) { | 
|             const rows = tableData.filter(el => selectedRows.includes(el.id)); | 
|             const { data: { code, data, msg } } = await request.post(`/qlyIsptItem/batch/update`, { isptItem: rows, type: '1' }); | 
|             if (code === 200) { | 
|                 notify(msg); | 
|                 getData() | 
|                 refresh() | 
|             } else { | 
|                 notify(msg); | 
|             } | 
|   | 
|         } else { | 
|             notify('请选择物料'); | 
|         } | 
|     } | 
|   | 
|     const batchUnQualified = async () => { | 
|         if (selectedRows.length) { | 
|             const rows = tableData.filter(el => selectedRows.includes(el.id)); | 
|             const { data: { code, data, msg } } = await request.post(`/qlyIsptItem/batch/update`, { isptItem: rows, type: '2' }); | 
|             if (code === 200) { | 
|                 notify(msg); | 
|                 getData() | 
|                 refresh() | 
|             } else { | 
|                 notify(msg); | 
|             } | 
|   | 
|         } else { | 
|             notify('请选择物料'); | 
|         } | 
|     } | 
|   | 
|     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 | 
|             }}> | 
|                 <Box sx={{ position: 'absolute', top: 8, right: 8, zIndex: 1001 }}> | 
|                     <DialogCloseButton onClose={handleClose} /> | 
|                 </Box> | 
|             </DialogTitle> | 
|             <DialogContent sx={{ mt: 2 }}> | 
|                 <Box sx={{ mt: 2 }}> | 
|                     <Stack direction="row" spacing={2}> | 
|                         <Button variant="contained" onClick={batchQualified}>{translate('common.action.batchSafe')} </Button> | 
|                         <Button variant="contained" color="error" onClick={batchUnQualified}>{translate('common.action.batchDef')} </Button> | 
|                     </Stack> | 
|                 </Box> | 
|                 <Box sx={{ mt: 2, height: 400, width: '100%' }}> | 
|                     <InspectModalTable | 
|                         tableData={tableData} | 
|                         setTableData={setTableData} | 
|                         selectedRows={selectedRows} | 
|                         setSelectedRows={setSelectedRows} | 
|                     /> | 
|                 </Box> | 
|             </DialogContent> | 
|             <DialogActions sx={{ position: 'sticky', bottom: 0, backgroundColor: 'background.paper', zIndex: 1000 }}> | 
|                 <Box sx={{ width: '100%', display: 'flex', justifyContent: 'end' }}> | 
|                     <Button onClick={handleSubmit} variant="contained" startIcon={<SaveIcon />}> | 
|                         {translate('toolbar.confirm')} | 
|                     </Button> | 
|                 </Box> | 
|             </DialogActions> | 
|         </Dialog> | 
|     ); | 
| }; | 
|   | 
| export default InspectModal; | 
|   | 
| const InspectModalTable = ({ tableData, setTableData, selectedRows, setSelectedRows }) => { | 
|     const translate = useTranslate(); | 
|     const notify = useNotify(); | 
|     const apiRef = useGridApiRef(); | 
|   | 
|     const handleSelectionChange = (ids) => { | 
|         setSelectedRows(ids) | 
|   | 
|     }; | 
|   | 
|     const [columns, setColumns] = useState([ | 
|         // { field: 'id', headerName: 'ID', width: 100 }, | 
|         { field: 'matnrCode', headerName: translate('table.field.qlyIsptItem.matnrCode') }, | 
|         { field: 'maktx', headerName: translate('table.field.qlyIsptItem.maktx'), width: 300 }, | 
|         { field: 'splrName', headerName: translate('table.field.qlyIsptItem.splrName') }, | 
|         { field: 'splrBatch', headerName: translate('table.field.qlyIsptItem.splrBatch') }, | 
|         // { field: 'stockBatch', headerName: translate('table.field.qlyIsptItem.stockBatch') }, | 
|         // { field: 'dlyQty', headerName: translate('table.field.qlyIsptItem.dlyQty') }, | 
|         { field: 'rcptQty', headerName: translate('table.field.qlyIsptItem.rcptQty') }, | 
|         { | 
|             field: 'safeQty', headerName: translate('table.field.qlyIsptItem.safeQty'), editable: true, type: 'number', | 
|             renderHeader: () => ( | 
|                 <strong> | 
|                     {translate('table.field.qlyIsptItem.safeQty')} | 
|                     <span style={{ color: '#d32f2f' }}> | 
|                         * | 
|                     </span> | 
|                 </strong> | 
|             ), | 
|         }, | 
|         { | 
|             field: 'disQty', headerName: translate('table.field.qlyIsptItem.disQty'), editable: true, type: 'number', | 
|             renderHeader: () => ( | 
|                 <strong> | 
|                     {translate('table.field.qlyIsptItem.disQty')} | 
|                 </strong> | 
|             ), | 
|         }, | 
|   | 
|         // { | 
|         //     field: 'isptResult', headerName: translate('table.field.qlyIsptItem.isptResult'), width: 150, type: 'singleSelect', | 
|         //     editable: true, valueOptions: [{ value: '1', label: '合格' }, { value: '2', label: '不合格' }, { value: '3', label: '待定' }, { value: '4', label: '部分合格' }], | 
|         // }, | 
|     ]) | 
|   | 
|     const processRowUpdate = (newRow, oldRow) => { | 
|         setTableData((prevData) => | 
|             prevData.map((r) => | 
|                 r.id === newRow.id ? { ...newRow } : r | 
|             ) | 
|         ); | 
|   | 
|         return newRow; | 
|     }; | 
|   | 
|   | 
|     return ( | 
|         <div style={{ height: 400, width: '100%' }}> | 
|             <DataGrid | 
|                 size="small" | 
|                 rows={tableData} | 
|                 columns={columns} | 
|                 checkboxSelection | 
|                 onRowSelectionModelChange={handleSelectionChange} | 
|                 selectionModel={selectedRows} | 
|                 apiRef={apiRef} | 
|                 processRowUpdate={processRowUpdate} | 
|                 editMode="row" | 
|                 disableColumnMenu={true} | 
|                 disableColumnSorting | 
|                 disableMultipleColumnsSorting | 
|                 disableRowSelectionOnClick | 
|                 getRowId={(row) => row.id} | 
|                 sx={{ | 
|                     '& .MuiDataGrid-cell input': { | 
|                         border: '1px solid #ccc' | 
|                     }, | 
|                 }} | 
|             /> | 
|         </div> | 
|     ); | 
| }; |