chen.lin
1 天以前 b3a8cec76cd3d2d3aa6d470e1c28ec161bc1a16b
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
import React, { useState, useEffect } from "react";
import {
    useTranslate,
    Form,
    useNotify,
    useRefresh,
    TextInput,
    SelectInput,
} from 'react-admin';
import {
    Dialog,
    DialogActions,
    DialogContent,
    DialogTitle,
    Grid,
    Box,
    Button,
    Paper,
    TableContainer,
    Table,
    TableHead,
    TableBody,
    TableRow,
    TableCell,
    IconButton,
    MenuItem,
    Select,
    FormControl,
    TextField,
} from '@mui/material';
import DialogCloseButton from "../../components/DialogCloseButton";
import DictionarySelect from "../../components/DictionarySelect";
import SaveIcon from '@mui/icons-material/Save';
import request from '@/utils/request';
import { Add, Delete } from '@mui/icons-material';
import { ReferenceInput, AutocompleteInput } from 'react-admin';
 
const defaultRow = () => ({ deviceSite: '', site: '', target: '' });
 
const InitModal = ({ open, setOpen, initialData = null, onClose }) => {
    const refresh = useRefresh();
    const translate = useTranslate();
    const notify = useNotify();
    const [disabled, setDisabled] = useState(false);
    const [rows, setRows] = useState([defaultRow()]);
    const [stationOptions, setStationOptions] = useState([]);
 
    useEffect(() => {
        if (!open) return;
        request.post('/basStation/list', {})
            .then((res) => {
                if (res?.data?.code === 200 && res?.data?.data) {
                    const list = Array.isArray(res.data.data) ? res.data.data : (res.data.data?.records || []);
                    const opts = list.map((item) => ({ id: item.id, stationName: item.stationName ?? item.name ?? item.id }));
                    setStationOptions(opts);
                    if (initialData?.rows?.length && opts.length) {
                        const resolved = initialData.rows.map((r) => {
                            const deviceSiteId = r.deviceSite || (r.deviceSiteName && opts.find((o) => o.stationName === r.deviceSiteName)?.id);
                            const siteId = r.site || (r.siteName && opts.find((o) => o.stationName === r.siteName)?.id);
                            return {
                                deviceSite: deviceSiteId != null ? String(deviceSiteId) : '',
                                site: siteId != null ? String(siteId) : '',
                                target: r.target ?? '',
                            };
                        });
                        setRows(resolved.length ? resolved : [defaultRow()]);
                    }
                }
            })
            .catch(() => {});
    }, [open, initialData]);
 
    useEffect(() => {
        if (open && !initialData?.rows?.length) {
            setRows([defaultRow()]);
        }
    }, [open, initialData]);
 
    const handleClose = (event, reason) => {
        if (reason !== "backdropClick") {
            setOpen(false);
            if (typeof onClose === 'function') onClose();
        }
    };
 
    const addRow = () => setRows((prev) => [...prev, defaultRow()]);
    const removeRow = (index) => {
        if (rows.length <= 1) return;
        setRows((prev) => prev.filter((_, i) => i !== index));
    };
    const changeRow = (index, field, value) => {
        setRows((prev) => prev.map((r, i) => (i === index ? { ...r, [field]: value } : r)));
    };
 
    const handleSubmit = async (value) => {
        const validRows = rows.filter(
            (r) => (r.deviceSite !== '' && r.deviceSite != null) && (r.site !== '' && r.site != null) && (r.target !== '' && (r.target || '').trim() !== '')
        );
        if (validRows.length === 0) {
            notify('请至少填写一行完整的设备站点、作业站点、目标站点', { type: 'error' });
            return;
        }
        if (!(value.channel != null && String(value.channel).trim() !== '')) {
            notify('巷道不能为空,多个请用英文逗号分隔,如 1,2,3', { type: 'error' });
            return;
        }
        setDisabled(true);
        const payload = {
            ...value,
            // 名称、wcs编号、站点标签 已从界面注释,不再提交
            name: null,
            wcsCode: null,
            label: null,
            rows: validRows.map((r) => ({
                deviceSite: String(r.deviceSite),
                site: String(r.site),
                target: String(r.target || '').trim(),
            })),
        };
        const res = await request.post('/deviceSite/init', payload);
        if (res?.data?.code === 200) {
            setOpen(false);
            refresh();
        } else {
            notify(res?.data?.msg || '初始化失败');
        }
        setDisabled(false);
    };
 
    const formDefaultValues = initialData ? {
        channel: initialData.channel != null ? String(initialData.channel) : '',
        deviceType: initialData.deviceType ?? '',
        // deviceCode 接驳位已注释,不默认填入
        // deviceCode: initialData.deviceCode ?? '',
        areaIdStart: initialData.areaIdStart ?? undefined,
        areaIdEnd: initialData.areaIdEnd ?? undefined,
        flagInit: 0,
        // name、wcsCode、label 已注释,不默认填入
        // name: initialData.name ?? '',
        // wcsCode: initialData.wcsCode ?? '',
        // label: initialData.label ?? '',
        typeIds: Array.isArray(initialData.typeIds) ? initialData.typeIds : (initialData.type != null ? [initialData.type] : undefined),
    } : undefined;
 
    return (
        <Dialog open={open} maxWidth="lg" fullWidth onClose={handleClose}>
            <Form onSubmit={handleSubmit} defaultValues={formDefaultValues} key={open ? (initialData ? 'copy' : 'new') : 'closed'}>
                <DialogCloseButton onClose={handleClose} />
                <DialogTitle>{translate('toolbar.pathInit')}</DialogTitle>
                <DialogContent sx={{ mt: 2 }}>
                    <Box sx={{ display: 'flex', flexDirection: 'column', gap: 3 }}>
                        <Grid container spacing={2}>
                            <Grid item xs={12}>
                                <TableContainer component={Paper} variant="outlined">
                                    <Table size="small">
                                        <TableHead>
                                            <TableRow>
                                                <TableCell>{translate('table.field.deviceSite.deviceSite')}</TableCell>
                                                <TableCell>{translate('table.field.deviceSite.site')}</TableCell>
                                                <TableCell>{translate('table.field.deviceSite.target')}</TableCell>
                                                <TableCell width={80}>操作</TableCell>
                                            </TableRow>
                                        </TableHead>
                                        <TableBody>
                                            {rows.map((r, index) => (
                                                <TableRow key={index}>
                                                    <TableCell>
                                                        <FormControl fullWidth size="small">
                                                            <Select
                                                                displayEmpty
                                                                value={r.deviceSite ?? ''}
                                                                onChange={(e) => changeRow(index, 'deviceSite', e.target.value)}
                                                                renderValue={(v) => {
                                                                    const o = stationOptions.find((s) => String(s.id) === String(v));
                                                                    return o ? o.stationName : (v ? String(v) : '');
                                                                }}
                                                            >
                                                                <MenuItem value="">请选择</MenuItem>
                                                                {stationOptions.map((opt) => (
                                                                    <MenuItem key={opt.id} value={opt.id}>
                                                                        {opt.stationName}
                                                                    </MenuItem>
                                                                ))}
                                                            </Select>
                                                        </FormControl>
                                                    </TableCell>
                                                    <TableCell>
                                                        <FormControl fullWidth size="small">
                                                            <Select
                                                                displayEmpty
                                                                value={r.site ?? ''}
                                                                onChange={(e) => changeRow(index, 'site', e.target.value)}
                                                                renderValue={(v) => {
                                                                    const o = stationOptions.find((s) => String(s.id) === String(v));
                                                                    return o ? o.stationName : (v ? String(v) : '');
                                                                }}
                                                            >
                                                                <MenuItem value="">请选择</MenuItem>
                                                                {stationOptions.map((opt) => (
                                                                    <MenuItem key={opt.id} value={opt.id}>
                                                                        {opt.stationName}
                                                                    </MenuItem>
                                                                ))}
                                                            </Select>
                                                        </FormControl>
                                                    </TableCell>
                                                    <TableCell>
                                                        <TextField
                                                            size="small"
                                                            fullWidth
                                                            placeholder={translate('table.field.deviceSite.target')}
                                                            value={r.target ?? ''}
                                                            onChange={(e) => changeRow(index, 'target', e.target.value)}
                                                        />
                                                    </TableCell>
                                                    <TableCell>
                                                        <IconButton
                                                            size="small"
                                                            onClick={() => removeRow(index)}
                                                            disabled={rows.length <= 1}
                                                        >
                                                            <Delete fontSize="small" />
                                                        </IconButton>
                                                    </TableCell>
                                                </TableRow>
                                            ))}
                                        </TableBody>
                                    </Table>
                                </TableContainer>
                                <Box sx={{ display: 'flex', justifyContent: 'flex-end', mt: 1 }}>
                                    <Button size="small" startIcon={<Add />} onClick={addRow}>
                                        新增一行
                                    </Button>
                                </Box>
                            </Grid>
 
                            {/* 名称、wcs编号、站点标签 已注释,不显示也不默认填入 */}
                            {/* <Grid item xs={4}>
                                <TextInput
                                    source="name"
                                    label="table.field.deviceSite.name"
                                    size="small"
                                    fullWidth
                                />
                            </Grid>
                            <Grid item xs={4}>
                                <TextInput
                                    source="wcsCode"
                                    label="table.field.deviceSite.wcsCode"
                                    size="small"
                                    fullWidth
                                />
                            </Grid>
                            <Grid item xs={4}>
                                <TextInput
                                    source="label"
                                    label="table.field.deviceSite.label"
                                    size="small"
                                    fullWidth
                                />
                            </Grid> */}
 
                            <Grid item xs={4}>
                                <DictionarySelect
                                    label={translate("table.field.deviceSite.type")}
                                    name="typeIds"
                                    dictTypeCode="sys_task_type"
                                    multiple
                                />
                            </Grid>
                            <Grid item xs={4}>
                                <DictionarySelect
                                    label={translate("table.field.deviceSite.device")}
                                    name="deviceType"
                                    dictTypeCode="sys_device_type"
                                />
                            </Grid>
                            {/* 接驳位 deviceCode 已注释 */}
                            {/* <Grid item xs={4}>
                                <TextInput
                                    source="deviceCode"
                                    label="table.field.deviceSite.deviceCode"
                                    size="small"
                                    fullWidth
                                />
                            </Grid> */}
                            <Grid item xs={4}>
                                <TextInput
                                    source="channel"
                                    label="table.field.deviceSite.channel"
                                    size="small"
                                    fullWidth
                                    placeholder="英文逗号分隔多个,如 1,2,3"
                                />
                            </Grid>
                            <Grid item xs={4}>
                                <SelectInput
                                    source="flagInit"
                                    label="table.field.deviceSite.flagInit"
                                    choices={[
                                        { id: 0, name: '否' },
                                        { id: 1, name: '是' },
                                    ]}
                                />
                            </Grid>
                            <Grid item xs={6} display="flex" gap={1}>
                                <ReferenceInput source="areaIdStart" label="table.field.deviceBind.typeId" reference="warehouseAreas" sort={{ field: 'sort', order: 'ASC' }} filter={{}}>
                                    <AutocompleteInput optionValue="id" optionText="name" label={translate('table.field.deviceSite.areaIdStart')} />
                                </ReferenceInput>
                            </Grid>
                            <Grid item xs={6} display="flex" gap={1}>
                                <ReferenceInput source="areaIdEnd" label="table.field.deviceBind.typeId" reference="warehouseAreas" sort={{ field: 'sort', order: 'ASC' }} filter={{}}>
                                    <AutocompleteInput optionValue="id" optionText="name" label={translate('table.field.deviceSite.areaIdEnd')} />
                                </ReferenceInput>
                            </Grid>
                        </Grid>
                    </Box>
                </DialogContent>
                <DialogActions sx={{ position: 'sticky', bottom: 0, backgroundColor: 'background.paper', zIndex: 1000 }}>
                    <Box sx={{ width: '100%', display: 'flex', justifyContent: 'space-between' }}>
                        <Button disabled={disabled} type="submit" variant="contained" startIcon={<SaveIcon />}>
                            {translate('toolbar.confirm')}
                        </Button>
                    </Box>
                </DialogActions>
            </Form>
        </Dialog>
    );
};
 
export default InitModal;