zhang
2025-05-20 1313906bb1eb983d3beece810035e7fc28d6a92f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import React, { useState, useRef, useEffect, useMemo, useCallback } from "react";
import {
    useTranslate,
    ShowBase,
    useDataProvider,
    useNotify,
    useRecordContext,
    useRedirect,
    useRefresh,
} from 'react-admin';
import {
    Box,
    Button,
    Chip,
    Dialog,
    DialogContent,
    Divider,
    Stack,
    Typography,
    Avatar,
    useTheme,
    TableContainer,
    Table,
    TableBody,
    TableCell,
    TableHead,
    TableRow,
    Paper,
    Toolbar,
    Tooltip,
    Checkbox,
    IconButton,
} from '@mui/material';
import { alpha } from '@mui/material/styles';
import { Link } from 'react-router-dom';
import DialogCloseButton from "../components/DialogCloseButton";
import { format } from 'date-fns';
import ArrowBackIosNewIcon from '@mui/icons-material/ArrowBackIosNew';
import request from '@/utils/request';
 
const MissionResend = ({ open, id }) => {
    const redirect = useRedirect();
    const handleClose = () => {
        redirect('list', 'mission');
    };
 
    return (
        <Dialog
            open={open}
            onClose={handleClose}
            fullWidth
            maxWidth="md"
            sx={{
                '& .MuiDialog-container': {
                    alignItems: 'flex-start',
                },
            }}
        >
            <DialogContent sx={{ padding: 0 }}>
                {!!id ? (
                    <ShowBase id={id}>
                        <MissionResendContent handleClose={handleClose} />
                    </ShowBase>
                ) : null}
            </DialogContent>
        </Dialog>
    )
}
 
const MissionResendContent = ({ handleClose }) => {
    const theme = useTheme();
    const record = useRecordContext();
    const translate = useTranslate();
    if (!record) return null;
 
    return (
        <>
            <DialogCloseButton
                onClose={handleClose}
                top={12}
            />
            <Stack gap={1}>
                <Box display="flex" p={2}>
                    <Box flex="1">
                        <Stack
                            direction="row"
                            justifyContent="space-between"
                            mb={4}
                        >
                            <Stack direction="row" alignItems="center" gap={2}>
                                <Avatar
                                    sx={{
                                        width: 30,
                                        height: 30,
                                        bgcolor: theme.palette.primary.main,
                                    }}
                                >
                                    {record.agv}
                                </Avatar>
                                <Typography variant="h5">
                                    {record.groupNo}
                                </Typography>
                            </Stack>
                            <Stack
                                gap={1}
                                direction="row"
                                pr={6}
                            >
                                <Button
                                    component={Link}
                                    to={`/mission/${record.id}/show`}
                                    size="small"
                                    startIcon={<ArrowBackIosNewIcon />}
                                >
                                    {translate('page.mission.backToMission')}
                                </Button>
                            </Stack>
                        </Stack>
 
                        {!!record.codeList?.length && (
                            <Box m={2} sx={{ whiteSpace: 'pre-line' }}>
                                <Typography
                                    color="textSecondary"
                                    variant="caption"
                                >
                                    {translate('table.field.mission.runPath')}
                                </Typography>
                                <Typography variant="body2" sx={{ whiteSpace: 'normal', overflow: 'hidden', display: 'flex', flexWrap: 'wrap' }}>
                                    [&nbsp;{record.codeList.map((code, index) => (
                                        <span key={index} style={{
                                            color: record.currCode === code ? theme.palette.primary.main : 'inherit',
                                        }}>
                                            {code}{index < record.codeList.length - 1 && ','}&nbsp;
                                        </span>
                                    ))}&nbsp;]
                                </Typography>
                            </Box>
                        )}
 
                        <ActionTable actionIds={record?.actionIds} currCode={record.currCode} handleClose={handleClose} />
                    </Box>
                </Box>
            </Stack>
        </>
    )
};
 
const headCells = [
    {
        id: 'actionType',
        numeric: false,
        disablePadding: true,
        label: 'table.field.action.actionType',
    },
    {
        id: 'code',
        numeric: true,
        disablePadding: false,
        label: 'table.field.action.code',
    },
    {
        id: 'taskId',
        numeric: true,
        disablePadding: false,
        label: 'table.field.action.taskId',
    },
    {
        id: 'val',
        numeric: true,
        disablePadding: false,
        label: 'table.field.action.val',
    },
    {
        id: 'params',
        numeric: true,
        disablePadding: false,
        label: 'table.field.action.params',
    },
];
 
const ActionTable = ({ actionIds, currCode, handleClose }) => {
    const dataProvider = useDataProvider();
    const translate = useTranslate();
    const notify = useNotify();
    const [actions, setActions] = useState([]);
    const [selected, setSelected] = React.useState([]);
 
    useEffect(() => {
        if (actionIds?.length > 0) {
            dataProvider.getMany('action', { ids: actionIds }).then(res => {
                if (res.data?.length > 0) {
                    setActions(res.data);
 
                    // init selected
                    const indexOfCurrCode = res.data.findIndex(item => item.code === currCode);
                    if (indexOfCurrCode !== -1) {
                        const selectedIds = res.data.slice(indexOfCurrCode).map(item => item.id);
                        setSelected(selectedIds);
                    }
                }
            })
        }
    }, [actionIds])
 
    const handleSelectAllClick = (event) => {
        if (event.target.checked) {
            const newSelected = actions.map((n) => n.id);
            setSelected(newSelected);
            return;
        }
        setSelected([]);
    };
 
    const handleClick = (event, id) => {
        const selectedIndex = selected.indexOf(id);
        let newSelected = [];
 
        if (selectedIndex === -1) {
            newSelected = newSelected.concat(selected, id);
        } else if (selectedIndex === 0) {
            newSelected = newSelected.concat(selected.slice(1));
        } else if (selectedIndex === selected.length - 1) {
            newSelected = newSelected.concat(selected.slice(0, -1));
        } else if (selectedIndex > 0) {
            newSelected = newSelected.concat(
                selected.slice(0, selectedIndex),
                selected.slice(selectedIndex + 1),
            );
        }
        setSelected(newSelected);
    };
 
    const resendSubmit = () => {
        if (!!selected?.length) {
            request.post("/mission/resend", selected.map(id => ({ id }))).then(res => {
                const { code, msg, data } = res.data;
                if (code === 200) {
                    notify(msg || 'common.response.success', { type: 'success', messageArgs: { _: msg } });
                    handleClose();
                } else {
                    notify(msg || 'common.response.fail', { type: 'error', messageArgs: { _: msg } });
                }
            }).catch(error => {
                notify(error.message || 'common.response.fail', { type: 'error', messageArgs: { _: error.message } });
            })
        }
    }
 
    return (
        <>
            <Box sx={{ width: '100%' }}>
                <Paper sx={{
                    width: '100%',
                    mb: 2,
                }}>
                    <Toolbar
                        sx={[
                            {
                                pl: { sm: 2 },
                                pr: { xs: 1, sm: 1 },
                                minHeight: '52.5px !important',
                            },
                            selected.length > 0 && {
                                bgcolor: (theme) =>
                                    alpha(theme.palette.primary.main, theme.palette.action.activatedOpacity),
                            },
                        ]}
                    >
                        {selected.length > 0 ? (
                            <Typography
                                sx={{ flex: '1 1 100%' }}
                                color="inherit"
                                variant="subtitle1"
                                component="div"
                            >
                                {selected.length} {translate('common.action.selected')}
                            </Typography>
                        ) : (
                            <Typography
                                sx={{ flex: '1 1 100%' }}
                                variant="subtitle1"
                                id="tableTitle"
                                component="div"
                            >
                                {translate('table.field.mission.actions')}
                            </Typography>
                        )}
                        {selected.length > 0 && (
                            <Tooltip title="Resend">
                                <Button
                                    onClick={resendSubmit}
                                >
                                    {translate('ra.action.confirm')}
                                </Button>
                            </Tooltip>
                        )}
                    </Toolbar>
                    <TableContainer>
                        <Table
                            sx={{ minWidth: 750 }}
                            aria-labelledby="tableTitle"
                            // size={dense ? 'small' : 'medium'}
                            size="small"
                        >
                            <TableHead>
                                <TableRow>
                                    <TableCell padding="checkbox">
                                        <Checkbox
                                            color="primary"
                                            indeterminate={selected.length > 0 && selected.length < actions.length}
                                            checked={actions.length > 0 && selected.length === actions.length}
                                            onChange={handleSelectAllClick}
                                            inputProps={{
                                                'aria-label': 'select all desserts',
                                            }}
                                        />
                                    </TableCell>
                                    {headCells.map((headCell) => (
                                        <TableCell
                                            key={headCell.id}
                                            align={headCell.numeric ? 'right' : 'left'}
                                            padding={headCell.disablePadding ? 'none' : 'normal'}
                                        >
                                            {translate(headCell.label)}
                                        </TableCell>
                                    ))}
                                </TableRow>
                            </TableHead>
                            <TableBody>
                                {actions?.map((row, index) => {
                                    const isItemSelected = selected.includes(row.id);
                                    const labelId = `action-table-checkbox-${index}`;
 
                                    return (
                                        <TableRow
                                            hover
                                            onClick={(event) => handleClick(event, row.id)}
                                            role="checkbox"
                                            aria-checked={isItemSelected}
                                            tabIndex={-1}
                                            key={row.id}
                                            selected={isItemSelected}
                                            sx={{ cursor: 'pointer' }}
                                        >
                                            <TableCell padding="checkbox">
                                                <Checkbox
                                                    color="primary"
                                                    checked={isItemSelected}
                                                    inputProps={{
                                                        'aria-labelledby': labelId,
                                                    }}
                                                />
                                            </TableCell>
                                            <TableCell
                                                component="th"
                                                id={labelId}
                                                scope="row"
                                                padding="none"
                                            >
                                                [ {row.priority} ]
                                                &nbsp;&nbsp;
                                                {row.actionType$}
                                            </TableCell>
                                            <TableCell align="right">{row.code}</TableCell>
                                            <TableCell align="right">{row.taskId$}</TableCell>
                                            <TableCell align="right">{row.val}</TableCell>
                                            <TableCell align="right">{row.params}</TableCell>
                                        </TableRow>
                                    );
                                })}
                            </TableBody>
                        </Table>
                    </TableContainer>
                </Paper>
            </Box>
        </>
    )
}
 
export default MissionResend;