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, 
 | 
    useListContext, 
 | 
    useRefresh, 
 | 
} from 'react-admin'; 
 | 
import { 
 | 
    Dialog, 
 | 
    DialogActions, 
 | 
    DialogContent, 
 | 
    DialogTitle, 
 | 
    Stack, 
 | 
    Grid, 
 | 
    TextField, 
 | 
    Box, 
 | 
    Button, 
 | 
    Paper, 
 | 
    TableContainer, 
 | 
    Table, 
 | 
    TableHead, 
 | 
    TableBody, 
 | 
    TableRow, 
 | 
    TableCell, 
 | 
    Tooltip, 
 | 
    IconButton, 
 | 
    styled, 
 | 
    Select, 
 | 
    MenuItem 
 | 
} from '@mui/material'; 
 | 
import DialogCloseButton from "../../components/DialogCloseButton"; 
 | 
import ConfirmButton from "../../components/ConfirmButton"; 
 | 
import MatnrInfoModal from "./MatnrInfoModal"; 
 | 
import SaveIcon from '@mui/icons-material/Save'; 
 | 
import request from '@/utils/request'; 
 | 
import { Add, Edit, Delete } from '@mui/icons-material'; 
 | 
import { DataGrid, useGridApiRef } from '@mui/x-data-grid'; 
 | 
import DictionarySelect from "../../components/DictionarySelect"; 
 | 
import DictSelect from "../../components/DictSelect"; 
 | 
  
 | 
const SelectMatnrModal = (props) => { 
 | 
    const { open, setOpen, asnId, billReload } = props; 
 | 
  
 | 
    const translate = useTranslate(); 
 | 
    const notify = useNotify(); 
 | 
    const refresh = useRefresh(); 
 | 
    const [disabled, setDisabled] = useState(false) 
 | 
    const [createDialog, setCreateDialog] = useState(false); 
 | 
  
 | 
    const tableRef = useRef(); 
 | 
  
 | 
    useEffect(() => { 
 | 
        if (open && asnId !== 0) { 
 | 
            requestGetHead() 
 | 
            requestGetBody() 
 | 
        } 
 | 
        setDisabled(false) 
 | 
    }, [open]) 
 | 
  
 | 
    const handleClose = (event, reason) => { 
 | 
        if (reason !== "backdropClick") { 
 | 
            setOpen(false); 
 | 
            refresh(); 
 | 
            setFormData({ type: '', wkType: '' }) 
 | 
            setTableData([]) 
 | 
        } 
 | 
    }; 
 | 
  
 | 
    const [formData, setFormData] = useState({ 
 | 
        type: '', 
 | 
        wkType: '7', 
 | 
        checkType: '0', 
 | 
        poCode: '', 
 | 
        logisNo: '', 
 | 
        arrTime: '' 
 | 
    }); 
 | 
  
 | 
    const [tabelData, setTableData] = useState([]); 
 | 
  
 | 
    const handleChange = (value, name) => { 
 | 
        setFormData((prevData) => ({ 
 | 
            ...prevData, 
 | 
            [name]: value 
 | 
        })); 
 | 
    }; 
 | 
  
 | 
    const resetData = () => { 
 | 
        setFormData({ 
 | 
            type: '', 
 | 
            wkType: '7', 
 | 
            checkType: '0', 
 | 
            poCode: '', 
 | 
            logisNo: '', 
 | 
            arrTime: '' 
 | 
        }) 
 | 
        setTableData([]) 
 | 
    } 
 | 
  
 | 
    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 (asnId === 0) { 
 | 
            const parmas = { 
 | 
                "orders": formData, 
 | 
                "items": tabelData, 
 | 
            } 
 | 
            const res = await request.post(`/check/items/save`, parmas); 
 | 
            if (res?.data?.code === 200) { 
 | 
                setOpen(false); 
 | 
                refresh(); 
 | 
                resetData() 
 | 
            } else { 
 | 
                notify(res.data.msg); 
 | 
            } 
 | 
        } else { 
 | 
            const parmas = { 
 | 
                "orders": formData, 
 | 
                "items": tabelData, 
 | 
            } 
 | 
            const res = await request.post(`/check/items/update`, parmas); 
 | 
            if (res?.data?.code === 200) { 
 | 
                setOpen(false); 
 | 
                refresh(); 
 | 
                resetData() 
 | 
            } else { 
 | 
                notify(res.data.msg); 
 | 
            } 
 | 
        } 
 | 
        setDisabled(false) 
 | 
  
 | 
    }; 
 | 
  
 | 
  
 | 
    const handleDelete = async () => { 
 | 
        const res = await request.post(`/check/remove/${asnId}`); 
 | 
        if (res?.data?.code === 200) { 
 | 
            setOpen(false); 
 | 
            refresh(); 
 | 
        } else { 
 | 
            notify(res.data.msg); 
 | 
        } 
 | 
    }; 
 | 
  
 | 
    const requestGetHead = async () => { 
 | 
        const res = await request.get(`/check/${asnId}`); 
 | 
        if (res?.data?.code === 200) { 
 | 
            setFormData(res.data.data) 
 | 
        } else { 
 | 
            notify(res.data.msg); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    const requestGetBody = async () => { 
 | 
        const res = await request.post(`/checkItem/page`, { orderId: asnId }); 
 | 
        if (res?.data?.code === 200) { 
 | 
            setTableData(res.data.data.records) 
 | 
        } else { 
 | 
            notify(res.data.msg); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    const [selectedRows, setSelectedRows] = useState([]); 
 | 
  
 | 
    const handleDeleteItem = () => { 
 | 
        const newTableData = _.filter(tabelData, (item) => !selectedRows.includes(item.matnrId)); 
 | 
        setTableData(newTableData); 
 | 
    } 
 | 
  
 | 
    return ( 
 | 
        <> 
 | 
            <Dialog 
 | 
                open={open} 
 | 
                onClose={handleClose} 
 | 
                aria-labelledby="form-dialog-title" 
 | 
                aria-hidden 
 | 
                fullWidth 
 | 
                disableRestoreFocus 
 | 
                maxWidth="xl"   // 'xs' | 'sm' | 'md' | 'lg' | 'xl' 
 | 
            > 
 | 
                <DialogTitle id="form-dialog-title" sx={{ 
 | 
                    position: 'sticky', 
 | 
                    top: 0, 
 | 
                    backgroundColor: 'background.paper', 
 | 
                    zIndex: 1000 
 | 
                }}> 
 | 
                    {translate('create.title')} 
 | 
                    <Box sx={{ position: 'absolute', top: 8, right: 8, zIndex: 1001 }}> 
 | 
                        <DialogCloseButton onClose={handleClose} /> 
 | 
                    </Box> 
 | 
                </DialogTitle> 
 | 
                <DialogContent sx={{ mt: 2 }}> 
 | 
                    <Box sx={{ display: 'flex', flexDirection: 'column', gap: 3 }}> 
 | 
                        <Form defaultValues={formData}> 
 | 
                            <Grid container spacing={2}> 
 | 
                                <Grid item md={2}> 
 | 
                                    <DictSelect 
 | 
                                        label={translate("table.field.checkOrder.type")} 
 | 
                                        value={formData.wkType} 
 | 
                                        variant="filled" 
 | 
                                        onChange={(e) => handleChange(e.target.value, 'wkType')} 
 | 
                                        dictTypeCode="sys_check_order_type" 
 | 
                                        required 
 | 
                                    /> 
 | 
                                </Grid> 
 | 
                                <Grid item md={2}> 
 | 
                                    <DictSelect 
 | 
                                        label={translate("table.field.checkOrder.checkType")} 
 | 
                                        value={formData.checkType} 
 | 
                                        variant="filled" 
 | 
                                        onChange={(e) => handleChange(e.target.value, 'checkType')} 
 | 
                                        dictTypeCode="sys_check_type" 
 | 
                                        required 
 | 
                                    /> 
 | 
                                </Grid> 
 | 
                                <Grid item md={2}> 
 | 
                                    <DateInput 
 | 
                                        source="arrTime" 
 | 
                                        label="table.field.outStock.arrTime" 
 | 
                                        size='small' 
 | 
                                        variant="filled" 
 | 
                                        value={formData.arrTime} 
 | 
                                        onChange={(e) => handleChange(e.target.value, 'arrTime')} 
 | 
                                    /> 
 | 
                                </Grid> 
 | 
                            </Grid> 
 | 
                        </Form> 
 | 
                    </Box> 
 | 
                    <Box sx={{ mt: 2 }}> 
 | 
                        <Stack direction="row" spacing={2}> 
 | 
                            <Button variant="contained" onClick={() => setCreateDialog(true)} > 
 | 
                                {translate('common.action.newAddMats')} 
 | 
                            </Button> 
 | 
                            <ConfirmButton label={"toolbar.delete"} variant="outlined" color="error" onConfirm={handleDeleteItem} /> 
 | 
                        </Stack> 
 | 
                    </Box> 
 | 
                    <Box sx={{ mt: 2 }}> 
 | 
                        <AsnOrderModalTable tabelData={tabelData} setTableData={setTableData} asnId={asnId} selectedRows={selectedRows} setSelectedRows={setSelectedRows} tableRef={tableRef}></AsnOrderModalTable> 
 | 
                    </Box> 
 | 
                </DialogContent> 
 | 
                <DialogActions sx={{ position: 'sticky', bottom: 0, backgroundColor: 'background.paper', zIndex: 1000 }}> 
 | 
                    <Toolbar sx={{ width: '100%', justifyContent: 'space-between' }}  > 
 | 
                        <Button disabled={disabled} onClick={handleSubmit} variant="contained" startIcon={<SaveIcon />}> 
 | 
                            {translate('toolbar.confirm')} 
 | 
                        </Button> 
 | 
                    </Toolbar> 
 | 
                </DialogActions> 
 | 
            </Dialog> 
 | 
            <MatnrInfoModal 
 | 
                open={createDialog} 
 | 
                setOpen={setCreateDialog} 
 | 
                data={tabelData} 
 | 
                setData={setTableData} 
 | 
            /> 
 | 
        </> 
 | 
    ) 
 | 
} 
 | 
  
 | 
export default SelectMatnrModal; 
 | 
  
 | 
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 AsnOrderModalTable = ({ tabelData, setTableData, asnId, selectedRows, setSelectedRows, tableRef }) => { 
 | 
    const translate = useTranslate(); 
 | 
    const notify = useNotify(); 
 | 
    const [check, setCheck] = 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.checkOrder.anfme') + "*", 
 | 
            type: 'number', 
 | 
            minWidth: 120, 
 | 
            flex: 1, 
 | 
            editable: true, 
 | 
            valueFormatter: (val) => val < 0 ? 0 : val, 
 | 
            headerClassName: "custom", 
 | 
        }, 
 | 
        { 
 | 
            field: 'splrCode', 
 | 
            headerName: translate('table.field.outStockItem.splrCode') + "*", 
 | 
            minWidth: 100, 
 | 
            flex: 1, 
 | 
            editable: true, 
 | 
            renderEditCell: (params) => ( 
 | 
                <SelectInputSplrCodeEditCell {...params} /> 
 | 
            ), 
 | 
            headerClassName: "custom", 
 | 
        }, 
 | 
        { 
 | 
            field: 'splrName', 
 | 
            headerName: translate('table.field.outStockItem.splrName') + "*", 
 | 
            minWidth: 100, 
 | 
            flex: 1, 
 | 
            editable: true, 
 | 
            renderEditCell: (params) => ( 
 | 
                <SelectInputSplrNameEditCell {...params} /> 
 | 
            ), 
 | 
            headerClassName: "custom", 
 | 
        }, 
 | 
        { 
 | 
            field: 'splrBatch', 
 | 
            headerName: translate('table.field.outStockItem.splrBatch'), 
 | 
            minWidth: 100, 
 | 
            flex: 1, 
 | 
            editable: true, 
 | 
        }, 
 | 
        { 
 | 
            field: 'poCode', 
 | 
            headerName: translate('table.field.outStockItem.poDetlCode'), 
 | 
            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 (check == null || check.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, 
 | 
                key: el.id, 
 | 
                headerName: el.fieldsAlise, 
 | 
                minWidth: 100, 
 | 
                flex: 1, 
 | 
                editable: false 
 | 
            })) 
 | 
            setCheck(cols); 
 | 
            setColumns([...columns, ...cols, action]) 
 | 
        } 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(); 
 | 
  
 | 
    return ( 
 | 
        <div style={{ height: 400, width: '100%' }}> 
 | 
            <DataGrid 
 | 
                apiRef={tableRef} 
 | 
                rows={tabelData} 
 | 
                columns={columns} 
 | 
                disableRowSelectionOnClick 
 | 
                getRowId={(row) => row.matnrId ? row.matnrId : row.id} 
 | 
                disableColumnFilter 
 | 
                disableColumnSelector 
 | 
                disableColumnSorting 
 | 
                disableMultipleColumnsSorting 
 | 
                processRowUpdate={processRowUpdate} 
 | 
                initialState={{ 
 | 
                    pagination: { 
 | 
                        paginationModel: { 
 | 
                            pageSize: 25, 
 | 
                        }, 
 | 
                    }, 
 | 
                }} 
 | 
                pageSizeOptions={[15, 25, 50, 100]} 
 | 
                editMode="row" 
 | 
                checkboxSelection 
 | 
                onRowSelectionModelChange={handleSelectionChange} 
 | 
                selectionModel={selectedRows} 
 | 
                sx={{ 
 | 
                    '& .MuiDataGrid-cell input': { 
 | 
                        border: '1px solid #ccc' 
 | 
                    }, 
 | 
                }} 
 | 
            /> 
 | 
        </div> 
 | 
    ); 
 | 
}; 
 |