import React, { useState, useEffect, useCallback } from "react"; 
 | 
import { 
 | 
    Dialog, 
 | 
    DialogActions, 
 | 
    DialogContent, 
 | 
    DialogTitle, 
 | 
    Stack, 
 | 
    Grid, 
 | 
    TextField, 
 | 
    Box, 
 | 
    Paper, 
 | 
    styled, 
 | 
    RadioGroup, 
 | 
    Radio, 
 | 
    FormControlLabel, 
 | 
} from '@mui/material'; 
 | 
import DialogCloseButton from "../../components/DialogCloseButton"; 
 | 
import { useTranslate, useNotify, useRefresh , Button} from 'react-admin'; 
 | 
import request from '@/utils/request'; 
 | 
import { DataGrid, useGridApiRef } from '@mui/x-data-grid'; 
 | 
import SaveIcon from '@mui/icons-material/Save'; 
 | 
  
 | 
const AsnSelModal = (props) => { 
 | 
    const { open, setOpen } = props; 
 | 
  
 | 
    const translate = useTranslate(); 
 | 
    const notify = useNotify(); 
 | 
    const refresh = useRefresh(); 
 | 
  
 | 
    const handleClose = (event, reason) => { 
 | 
        if (reason !== "backdropClick") { 
 | 
            setOpen(false); 
 | 
        } 
 | 
    }; 
 | 
  
 | 
    const [formData, setFormData] = useState({ 
 | 
        asnCode: '' 
 | 
    }); 
 | 
  
 | 
    const [tableData, setTableData] = useState([]); 
 | 
    const [selectedRows, setSelectedRows] = useState([]); 
 | 
  
 | 
    const handleChange = (e) => { 
 | 
        const { name, value } = e.target; 
 | 
        setFormData((prevData) => ({ 
 | 
            ...prevData, 
 | 
            [name]: value 
 | 
        })); 
 | 
    }; 
 | 
  
 | 
    const reset = () => { 
 | 
        setFormData({ 
 | 
            asnCode: '' 
 | 
        }) 
 | 
    } 
 | 
  
 | 
    const handleSubmit = async () => { 
 | 
        const { data: { code, data, msg } } = await request.post(`/qlyInspect/selected`, { ids: selectedRows }); 
 | 
  
 | 
        if (code === 200) { 
 | 
            notify(msg); 
 | 
            refresh() 
 | 
        } else { 
 | 
            notify(msg); 
 | 
        } 
 | 
        setOpen(false); 
 | 
        reset(); 
 | 
    }; 
 | 
  
 | 
    const getData = async () => { 
 | 
        const res = await request.post(`/qlyInspect/asn/list`, { asnCode: formData.asnCode }); 
 | 
        if (res?.data?.code === 200) { 
 | 
            setTableData(res.data.data); 
 | 
        } else { 
 | 
            notify(res.data.msg); 
 | 
        } 
 | 
    }; 
 | 
  
 | 
    useEffect(() => { 
 | 
        getData(); 
 | 
    }, [open]); 
 | 
  
 | 
    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 
 | 
            }}> 
 | 
                {/* 选择asn单 */} 
 | 
                <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.warehouseAreasItem.asnCode')} 
 | 
                                name="asnCode" 
 | 
                                value={formData.asnCode} 
 | 
                                onChange={handleChange} 
 | 
                                size="small" 
 | 
                            /> 
 | 
                        </Grid> 
 | 
                        <Grid item md={7} sx={{margin: 'auto'}}> <Button variant="contained" onClick={handleSearch} label={'toolbar.query'}></Button></Grid> 
 | 
                    </Grid> 
 | 
                </Box> 
 | 
                <Box sx={{ mt: 2, height: 400, width: '100%' }}> 
 | 
                    <AsnSelModalTable 
 | 
                        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 />} label={'toolbar.confirm'}> 
 | 
                    </Button> 
 | 
                </Box> 
 | 
            </DialogActions> 
 | 
        </Dialog> 
 | 
    ); 
 | 
}; 
 | 
  
 | 
export default AsnSelModal; 
 | 
  
 | 
const AsnSelModalTable = ({ 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: 'asnCode', headerName: translate('table.field.warehouseAreasItem.asnCode'), width: 180 }, 
 | 
        // { field: 'areaId', headerName: translate('table.field.warehouseAreasItem.type') }, 
 | 
        { field: 'areaName', headerName: translate('table.field.warehouseAreasItem.areaName'), width: 200 }, 
 | 
        { field: 'anfme', headerName: translate('table.field.warehouseAreasItem.anfme') }, 
 | 
        { field: 'qty', headerName: translate('table.field.warehouseAreasItem.qty') }, 
 | 
        { field: 'unit', headerName: translate('table.field.warehouseAreasItem.unit') }, 
 | 
    ]) 
 | 
  
 | 
  
 | 
    return ( 
 | 
        <div style={{ height: 400, width: '100%' }}> 
 | 
            <DataGrid 
 | 
                size="small" 
 | 
                rows={tableData} 
 | 
                columns={columns} 
 | 
                checkboxSelection 
 | 
                onRowSelectionModelChange={handleSelectionChange} 
 | 
                selectionModel={selectedRows} 
 | 
                apiRef={apiRef} 
 | 
                disableColumnMenu={true} 
 | 
                disableColumnSorting 
 | 
                disableMultipleColumnsSorting 
 | 
            /> 
 | 
        </div> 
 | 
    ); 
 | 
}; 
 |