From e5ff577d91179d9f0ae1f689d182bb2e0465fca1 Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@163.com>
Date: 星期五, 08 十一月 2024 13:48:12 +0800
Subject: [PATCH] #

---
 zy-acs-flow/src/page/loc/BulkUpdateButton.jsx |  190 +++++++++++++++++++++++++++++------------------
 1 files changed, 117 insertions(+), 73 deletions(-)

diff --git a/zy-acs-flow/src/page/loc/BulkUpdateButton.jsx b/zy-acs-flow/src/page/loc/BulkUpdateButton.jsx
index 15cf7f9..0bb42eb 100644
--- a/zy-acs-flow/src/page/loc/BulkUpdateButton.jsx
+++ b/zy-acs-flow/src/page/loc/BulkUpdateButton.jsx
@@ -1,94 +1,138 @@
-// src/components/BulkUpdateButton.js
 import React, { useState } from 'react';
-import { useUpdateMany, useRefresh, useNotify, useUnselectAll, Button as RaButton } from 'react-admin';
-import { Dialog, DialogActions, DialogContent, DialogTitle, TextField, Select, MenuItem } from '@mui/material';
+import {
+    useUpdateMany,
+    useRefresh,
+    useNotify,
+    useListContext,
+    useUnselectAll,
+    Button as RaButton,
+    Form,
+    ReferenceInput,
+    AutocompleteInput,
+    NumberInput,
+    TextInput,
+    useTranslate,
+} from 'react-admin';
+import { Dialog, DialogActions, DialogContent, DialogTitle, Grid, Stack, Divider } from '@mui/material';
 import UpdateIcon from '@mui/icons-material/Update';
+import MemoInput from "../components/MemoInput";
+import ContentSave from '@mui/icons-material/Save';
+import CloseIcon from '@mui/icons-material/Close';
 
-const BulkUpdateButton = ({ selectedIds, resource, label }) => {
-    console.log('BulkUpdateButton rendered with selectedIds:', selectedIds);
-
+const BulkUpdateButton = ({ resource, label = 'ra.action.update', ...rest }) => {
     const [open, setOpen] = useState(false);
-    const [formData, setFormData] = useState({});
 
-    // Hooks 璋冪敤椤哄簭璋冩暣
     const refresh = useRefresh();
     const notify = useNotify();
+    const translate = useTranslate();
     const unselectAll = useUnselectAll(resource);
-
-    const [updateMany, { loading }] = useUpdateMany(
-        resource,
-        { ids: selectedIds, data: formData },
-        {
-            onSuccess: () => {
-                setOpen(false);
-                refresh();
-                notify('鎵归噺鏇存柊鎴愬姛', { type: 'success' });
-                unselectAll();
-            },
-            onError: (error) => {
-                notify(`鎵归噺鏇存柊澶辫触: ${error.message}`, { type: 'warning' });
-            }
-        }
-    );
+    const { selectedIds } = useListContext();
 
     const handleOpen = () => setOpen(true);
     const handleClose = () => setOpen(false);
 
-    const handleChange = (e) => {
-        const { name, value } = e.target;
-        setFormData({
-            ...formData,
-            [name]: value,
-        });
-    };
+    const [updateMany, { loading }] = useUpdateMany(
+        resource,
+        { ids: selectedIds, data: {} },
+        {
+            onSuccess: () => {
+                setOpen(false);
+                refresh();
+                notify('common.response.success', { type: 'info' });
+                unselectAll();
+            },
+            onError: (error) => {
+                notify(error.msg || 'common.response.fail', { type: 'error' });
+            }
+        }
+    );
 
-    const handleSubmit = () => {
-        updateMany();
+    const handleSubmit = (data) => {
+        const filteredData = Object.fromEntries(
+            Object.entries(data).filter(([key, value]) => value !== null && value !== undefined)
+        );
+        delete filteredData['memoWrap'];
+        if (Object.keys(filteredData).length > 0) {
+            console.log(filteredData);
+            updateMany({
+                data: filteredData,
+            });
+        } else {
+            notify('common.response.fail', { type: 'warning' });
+        }
     };
 
     return (
         <>
-            <RaButton onClick={handleOpen} startIcon={<UpdateIcon />} label={label || '鎵归噺淇敼'}>
-                {/* 濡傛灉浣跨敤 React-Admin 鐨� Button锛屽彲浠ョ渷鐣ュ瓙鑺傜偣 */}
-            </RaButton>
+            <RaButton onClick={handleOpen} startIcon={<UpdateIcon />} label={label} />
             <Dialog open={open} onClose={handleClose}>
-                <DialogTitle>鎵归噺淇敼</DialogTitle>
-                <DialogContent>
-                    {/* 浣跨敤 Select 缁勪欢闄愬埗鐘舵�佸�� */}
-                    <Select
-                        margin="dense"
-                        name="status"
-                        label="鐘舵��"
-                        fullWidth
-                        variant="standard"
-                        onChange={handleChange}
-                        defaultValue=""
-                    >
-                        <MenuItem value="">
-                            <em>鏃�</em>
-                        </MenuItem>
-                        <MenuItem value="1">鍚敤</MenuItem>
-                        <MenuItem value="0">绂佺敤</MenuItem>
-                    </Select>
-                    <TextField
-                        margin="dense"
-                        name="memo"
-                        label="澶囨敞"
-                        type="text"
-                        fullWidth
-                        variant="standard"
-                        onChange={handleChange}
-                    />
-                </DialogContent>
-                <DialogActions>
-                    <RaButton onClick={handleClose} color="primary">
-                        鍙栨秷
-                    </RaButton>
-                    <RaButton onClick={handleSubmit} color="primary" disabled={loading}>
-                        纭
-                    </RaButton>
-                </DialogActions>
-            </Dialog>
+                <Form onSubmit={handleSubmit}>
+                    <DialogTitle>{translate('ra.action.update')}</DialogTitle>
+                    <DialogContent>
+                        <Grid container rowSpacing={2} columnSpacing={2}>
+                            <Grid item xs={6} display="flex" gap={1}>
+                                <ReferenceInput
+                                    source="code"
+                                    reference="code"
+                                >
+                                    <AutocompleteInput
+                                        label="table.field.loc.code"
+                                        optionText="data"
+                                        filterToQuery={(val) => ({ data: val })}
+                                    />
+                                </ReferenceInput>
+                            </Grid>
+                            <Grid item xs={6} display="flex" gap={1}>
+                                <ReferenceInput
+                                    source="locSts"
+                                    reference="locSts"
+                                >
+                                    <AutocompleteInput
+                                        label="table.field.loc.locSts"
+                                        optionText="name"
+                                        filterToQuery={(val) => ({ name: val })}
+                                    />
+                                </ReferenceInput>
+                            </Grid>
+                            <Grid item xs={6} display="flex" gap={1}>
+                                <NumberInput
+                                    label="table.field.loc.offset"
+                                    source="offset"
+                                />
+                            </Grid>
+                            <Grid item xs={12} display="flex" gap={1}>
+                                <Stack direction="column" spacing={1} width={'100%'}>
+                                    <MemoInput />
+                                </Stack>
+                            </Grid>
+                        </Grid>
+                    </DialogContent>
+                    <Divider sx={{ mb: 1 }} />
+                    <DialogActions sx={{ mb: 1 }}>
+                        <RaButton
+                            onClick={handleClose}
+                            variant="contained"
+                            color="inherit"
+                            size='large'
+                            label='ra.action.cancel'
+                            sx={{ mr: 1 }}
+                        >
+                            <CloseIcon />
+                        </RaButton>
+                        <RaButton
+                            type="submit"
+                            variant="contained"
+                            color="primary"
+                            size='large'
+                            label='ra.action.save'
+                            disabled={loading}
+                            sx={{ mr: 1 }}
+                        >
+                            <ContentSave />
+                        </RaButton>
+                    </DialogActions>
+                </Form>
+            </Dialog >
         </>
     );
 };

--
Gitblit v1.9.1