EditModal.tsx
6.93 KB
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
import React, { useState, useEffect, useRef } from 'react';
import { Modal, Button, message, Form, Input, Switch, Select, Tag } from 'antd';
import {useStore} from '../index';
import { authShopRoleListApi } from '@/common/api';
import { List, saveStandGroupApi } from '../api';
import { debounce } from 'lodash';
import ShopSelect from '@/components/ShopSelect';
const Option = Select.Option;
interface RoleShop {
roleCode?: string // 角色编码
roleName?: string // 角色名称
shopId?: number // 门店id
shopName?: string // 门店名称
}
export default function DetailModal() {
const { visible, setVisible, shopList, roleList, setLoading: setPageLoading, currentItem, setCurrentItem } = useStore();
const [loading, setLoading] = useState<boolean>(false);
const [staffList, setStaffList] = useState<any>([]);
const [roleShop, setRoleShop] = useState<RoleShop>({});
const [form] = Form.useForm();
const resetRef = useRef<any>(true);
// const [shopValue, setShopValue] = useState<any>();
useEffect(() => {
if (visible && currentItem.id) {
const _staff = currentItem?.staffList?.map((i:any) => ({value: i.staffId, label: i.staffName}));
// setShopValue([{value: currentItem.shopId, label: currentItem.shopName}]);
form.setFieldsValue({
name: currentItem.name,
roleCode: currentItem.roleCode,
roleName: currentItem.roleName,
shop: [{value: currentItem.shopId, label: currentItem.shopName}],
staff: _staff,
});
setRoleShop({roleCode: currentItem.roleCode, roleName: currentItem.roleName, shopId: currentItem.shopId, shopName: currentItem.shopName});
}
}, [visible]);
useEffect(() => {
if (roleShop.roleCode && roleShop.shopId) {
getStaffList(roleShop);
// 编辑首次进入不重置表单人员
if (!(currentItem.id && resetRef.current)) {
form.setFieldsValue({staff: []});
}
resetRef.current = false;
} else {
setStaffList([]);
form.setFieldsValue({staff: []});
}
}, [roleShop.roleCode, roleShop.shopId]);
// 获取员工列表
function getStaffList(params: any) {
authShopRoleListApi({rangeValue: params.shopId, roleCode: params.roleCode})
.then(res => {
setStaffList(res.data || []);
})
.catch(e => message.error(e.message));
}
function handleCancel() {
setVisible(false);
resetStatus();
}
// 重置页面数据
function resetStatus() {
form.resetFields();
setStaffList([]);
setRoleShop({});
setCurrentItem({});
resetRef.current = true;
}
async function onFinish() {
const params = await form.validateFields();
setLoading(true);
const _staff = params.staff.map((v:any) => {
const k = {
staffId: v.value,
staffName: v.label
};
return k;
});
const _params: List = {
shopId: Array.isArray(params.shop)? params.shop[0].value: params.shop.value,
shopName: Array.isArray(params.shop)? params.shop[0].label: params.shop.label,
roleCode: params.roleCode,
roleName: roleShop.roleName,
staffList: _staff,
name: params.name
};
if (currentItem.id) {
_params.id = currentItem.id;
}
saveStandGroupApi(_params)
.then(res => {
message.success(res.result);
setLoading(false);
setVisible(false);
resetStatus();
setPageLoading(true);
})
.catch(e => {
message.error(e.message);
setLoading(false);
});
}
return (
<Modal
destroyOnClose
forceRender
title={currentItem.id ? "编辑站岗小组" : "新增站岗小组"}
visible={visible}
maskClosable={false}
onCancel={handleCancel}
footer={[
<Button key="cancel" onClick={handleCancel} style={{ marginLeft: 10 }}>
取消
</Button>,
<Button
key="submit"
onClick={debounce(() => onFinish(), 380)}
type="primary"
htmlType="submit"
loading={loading}
>
确认
</Button>,
]}
>
<Form form={form} labelCol={{ span: 6 }} wrapperCol={{ span: 18 }}>
<Form.Item
label="小组名称"
rules={[{ required: true, message: "请输入小组名称!" }]}
name="name"
>
<Input placeholder="请输入小组名称" allowClear />
</Form.Item>
<Form.Item
label="门店"
name="shop"
rules={[{ required: true, message: "请选择门店!" }]}
>
{/* <Select
placeholder="门店"
allowClear
showSearch
optionFilterProp="children"
onChange={(value, option: any) => setRoleShop({...roleShop, shopId: value || undefined, shopName: option.children || undefined})}
>
{shopList.length && shopList.map(item => <Option key={item.id} value={item.id}>{item.name}</Option>)}
</Select> */}
<ShopSelect
labelInValue
onChange={(item: any) => setRoleShop({
...roleShop,
shopId: item.value || undefined,
shopName: item.label || undefined,
})}
// value={shopValue}
/>
</Form.Item>
<Form.Item
label="角色名称"
name="roleName"
rules={[{ required: true, message: "请选择角色!" }]}
>
<Select
placeholder="角色编码"
allowClear
showSearch
optionFilterProp="children"
onChange={(value, option: any) => {
setRoleShop({
...roleShop,
roleCode: value || undefined,
roleName: option.children || undefined,
});
form.setFieldsValue({ roleCode: value || undefined });
}}
>
{roleList.length &&
roleList.map((item) => (
<Option key={item.roleCode} value={item.roleCode}>
{item.roleName}
</Option>
))}
</Select>
</Form.Item>
<Form.Item
label="角色码"
rules={[{ required: true, message: "请选择角色!" }]}
name="roleCode"
>
<Input placeholder="请选择角色" allowClear disabled />
</Form.Item>
<Form.Item
label="员工"
name="staff"
rules={[{ required: true, message: "请选择员工!" }]}
>
<Select
placeholder="请选择员工"
allowClear
showSearch
mode="multiple"
showArrow={false}
optionFilterProp="children"
labelInValue
>
{staffList.length &&
staffList.map((item: any) => (
<Option key={item.userId} value={item.userId}>
{item.userName}
</Option>
))}
</Select>
</Form.Item>
</Form>
</Modal>
);
}