广州市综治平台前端
liuwh
6 days ago be89af3bf4d7e591ef085f55f1de7c28fcf5f6a8
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
 * @Company: hugeInfo
 * @Author: ldh
 * @Date: 2022-02-21 17:46:43
 * @LastEditTime: 2023-07-24 18:01:56
 * @LastEditors: lwh
 * @Version: 1.0.0
 * @Description: 新增人员
 */
import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { Form, Input, Select, Button, Divider, Space, TreeSelect, Row, Col, Typography } from 'antd';
import { PlusOutlined } from '@ant-design/icons';
import * as $$ from '../../../utils/utility';
const { Option } = Select;
 
/*
 * isAddOrEditPerson, // 是否显示该页面
 * type, // add: 新增;change: 修改
 * formData, // 修改时的当前选择修改对象
 * selectData, // 角色,组织部门数据
 * post, // 岗位数据
 * handleSubmit, // 表单提交
 * handleAddPost, // 新增岗位
 */
const AddOrEditPerson = ({ isAddOrEditPerson, type, formData, selectData, post, handleSubmit, handleAddPost }) => {
    const [form] = Form.useForm();
 
    // '1':提交;'2':提交并继续
    const [submitType, setSubmitType] = useState('1');
 
    // 表单信息
    const [submitData, setSubmitData] = useState({});
 
    // 部门数据
    const [deptData, setDeptData] = useState([]);
 
    // 岗位value
    const [postName, setPostName] = useState('');
 
    // select下拉框选择时
    function handleValuesChange(type, values) {
        let obj = {};
        if (type === 'unitId') {
            if (values.value) {
                let deptData = values.extra.triggerNode.props.chainList || [];
                setDeptData(deptData);
            } else {
                setDeptData([]);
            }
            form.setFieldsValue({ deptId: undefined });
            obj = { deptName: '', unitName: values.label[0], unitId: values.value };
        } else if (type === 'deptId') {
            obj = { deptName: values.label[0] };
        } else if (type === 'goodFieldOption') {
            let canField = form.getFieldValue('canField') || [];
            let goodField = values[0];
            let goodFieldOption = values[1];
            // 新增时,专长中增加“婚姻”,范围中也增加“婚姻”
            if (!canField.includes(goodField[goodField.length - 1])) {
                form.setFieldsValue({ 'canField': [...canField, goodField[goodField.length - 1]] });
                obj.canField = [...canField, goodField[goodField.length - 1]];
                obj.canFieldOption = [...(submitData.canFieldOption || []), goodFieldOption[goodFieldOption.length - 1]];
            }
            // 删除时,专长删除“婚姻”,如范围也有“婚姻”,则范围删除“婚姻”
            if (goodField.length < submitData.goodField?.length) {
                let delGooField = submitData.goodField[submitData.goodField.length - 1];
                let index = canField.indexOf(delGooField);
                if (index !== -1) {
                    canField.splice(index, 1);
                    let arr = [...(submitData?.canFieldOption || [])];
                    arr.splice(index, 1);
                    form.setFieldsValue({ 'canField': [...canField] });
                    obj.canField = [...canField];
                    obj.canFieldOption = [...arr];
                }
            }
            obj.goodField = goodField;
            obj[type] = goodFieldOption;
        } else if (type === 'canFieldOption') {
            let goodField = form.getFieldValue('goodField') || [];
            let canField = values[0];
            let canFieldOption = values[1];
            // 删除时,范围删除“婚姻”,如专长也有“婚姻”,则专长删除“婚姻”
            if (canField.length < submitData.canField?.length) {
                let delCanField = submitData.canField.filter((item) => !canField.includes(item))[0];
                let index = goodField.indexOf(delCanField);
                if (index !== -1) {
                    goodField.splice(index, 1);
                    let arr = [...(submitData?.goodFieldOption || [])];
                    arr.splice(index, 1);
                    form.setFieldsValue({ 'goodField': [...goodField] });
                    obj.goodField = [...goodField];
                    obj.goodFieldOption = [...arr];
                }
            }
            obj.canField = canField;
            obj[type] = canFieldOption;
        } else {
            obj[type] = values;
        }
        setSubmitData({ ...submitData, ...obj });
    }
 
    // 提交表单
    function handleFinish(submitType) {
        setSubmitType(submitType);
        form.submit();
        form.scrollToField();
    }
 
    // 获取组织下的部门
    function loopDept(data, key) {
        for (let i = 0; i < data.length; i++) {
            if (data[i].value === key) {
                let unit_deptData = data[i].chainList;
                setDeptData(unit_deptData);
                break;
            }
            if (data[i].children && data[i].children.length > 0) {
                loopDept(data[i].children, key);
            }
        }
    }
 
    // 重置
    function handleReset() {
        if (type === 'change') {
            let data = JSON.parse(JSON.stringify({ dispStatus: '1', findStatus: '1', ...formData }));
            data.ctUseroleListId = data.ctUseroleList?.map((x) => x.roleId) || [];
            data.ctUsepostListId = data.ctUsepostList?.map((x) => x.postId) || [];
            data.canField = data.canField ? data.canField.split(',') : [];
            data.goodField = data.goodField ? data.goodField.split(',') : [];
            data.canFieldOption = data.canFieldName
                ? data.canFieldName.split(',').map((x) => {
                        return { children: x };
                  })
                : [];
            data.goodFieldOption = data.goodFieldName
                ? data.goodFieldName.split(',').map((x) => {
                        return { children: x };
                  })
                : [];
            form.setFieldsValue(data);
            setSubmitData(data);
            if (data.unitId) {
                loopDept(selectData.units, data.unitId);
            } else if (selectData.units[0]) {
                setDeptData(selectData.units[0].chainList);
            } else {
                setDeptData([]);
            }
        } else {
            form.resetFields();
            if (selectData.units[0]) {
                // 默认为当前用户组织
                form.setFieldsValue({ unitId: selectData.units[0].value, dispStatus: '1', findStatus: '1' });
                setSubmitData({ unitName: selectData.units[0].label, unitId: selectData.units[0].value });
                setDeptData(selectData.units[0].chainList || []);
            }
        }
    }
 
    useEffect(() => {
        if (form.setFieldsValue) {
            handleReset();
        }
    }, [type, isAddOrEditPerson]);
 
    // 岗位提交后清除input值
    useEffect(() => {
        setPostName('');
    }, [post.length]);
 
    return (
        <div className="personnel" style={{ display: !isAddOrEditPerson ? 'none' : '' }}>
            <div className="personnel-add">
                <Form
                    layout="vertical"
                    form={form}
                    onFinish={(values) => {
                        let submitDataCopy = { ...submitData, ...values };
                        submitDataCopy.ctUseroleList =
                            submitDataCopy.ctUseroleListOption?.map((x) => {
                                return { roleId: x.key, roleName: x.children };
                            }) || submitDataCopy.ctUseroleList;
                        submitDataCopy.ctUsepostList =
                            submitDataCopy.ctUsepostListOption?.map((x) => {
                                return { postId: x.key, postName: x.children };
                            }) || submitDataCopy.ctUsepostList;
                        submitDataCopy.canField = submitDataCopy.canField.join();
                        submitDataCopy.goodField = submitDataCopy.goodField.join();
                        submitDataCopy.canFieldName = submitDataCopy.canFieldOption?.map((x) => x.children)?.join() || submitDataCopy.canFieldName;
                        submitDataCopy.goodFieldName = submitDataCopy.goodFieldOption?.map((x) => x.children)?.join() || submitDataCopy.goodFieldName;
                        delete submitDataCopy.ctUseroleListOption;
                        delete submitDataCopy.ctUsepostListOption;
                        delete submitDataCopy.goodFieldOption;
                        delete submitDataCopy.canFieldOption;
                        delete submitDataCopy.ctUsepostListId;
                        delete submitDataCopy.ctUseroleListId;
                        handleSubmit(submitType, submitDataCopy, form);
                    }}
                    onFinishFailed={({ values, errorFields }) => $$.info({ type: 'error', content: errorFields[0].errors })}
                >
                    <Row gutter={[24, 16]}>
                        <Col span={8}>
                            <Form.Item
                                label="账号"
                                required
                                name="acc"
                                help="账号只可为英文和数字的组合"
                                rules={[{ pattern: $$.accRegExp, required: true, message: '请输入正确的账号' }]}
                            >
                                <Input placeholder="请输入" allowClear />
                            </Form.Item>
                        </Col>
                        <Col span={8}>
                            <Form.Item
                                label="密码"
                                required
                                name="cipher"
                                help="密码长度为8-16的数字或字母或特殊字符(!@#$&_+)"
                                // rules={[{ required: true, pattern: new RegExp('^(?=[0-9]+$)+(?=[a-zA-Z]+$)|[0-9A-Za-z]{8,16}[!@#$&_+*]$', 'g'), message: '请输入正确的密码' }]}
                            >
                                <Input.Password placeholder="请输入" />
                            </Form.Item>
                        </Col>
                        <Col span={8}></Col>
                        <Col span={8}>
                            <Form.Item label="姓名" required name="trueName" rules={[{ required: true }]}>
                                <Input placeholder="请输入" allowClear />
                            </Form.Item>
                        </Col>
                        <Col span={8}>
                            <Form.Item label="手机号码" name="mobile" rules={[{ pattern: $$.mobileRegExp, message: '请输入正确的手机号码' }]}>
                                <Input placeholder="请输入" allowClear />
                            </Form.Item>
                        </Col>
                        <Col span={8}>
                            <Form.Item label="身份证号码" name="idcard" >
                                <Input placeholder="请输入" allowClear />
                            </Form.Item>
                        </Col>
                        <Col span={8}>
                            <Form.Item label="所属组织" name="unitId" help="未选组织则默认添加到当前账号组织下">
                                <TreeSelect
                                    showSearch
                                    placeholder="请选择"
                                    dropdownStyle={{ maxHeight: 500, overflow: 'auto' }}
                                    treeData={selectData.units}
                                    treeDefaultExpandAll
                                    allowClear
                                    filterTreeNode={(inputValue, treeNode) => (treeNode.label.indexOf(inputValue) !== -1 ? true : false)}
                                    onChange={(value, label, extra) => handleValuesChange('unitId', { value, label, extra })}
                                />
                            </Form.Item>
                        </Col>
                        <Col span={8}>
                            <Form.Item label="所属组织下部门" name="deptId">
                                <TreeSelect
                                    disabled={submitData.unitId ? false : true}
                                    showSearch
                                    placeholder={`请选择${!submitData.unitId ? '(请先选组织)' : ''}`}
                                    treeData={deptData}
                                    dropdownStyle={{ maxHeight: 500, overflow: 'auto' }}
                                    treeDefaultExpandAll
                                    allowClear
                                    filterTreeNode={(inputValue, treeNode) => (treeNode.label.indexOf(inputValue) !== -1 ? true : false)}
                                    onChange={(value, label, extra) => handleValuesChange('deptId', { value, label, extra })}
                                />
                            </Form.Item>
                        </Col>
                        <Col span={8}></Col>
                        <Col span={8}>
                            <Form.Item label="角色" required name="ctUseroleListId" rules={[{ required: true }]}>
                                <Select
                                    placeholder="请选择(可多选)"
                                    mode="multiple"
                                    filterOption={(inputValue, option) => (option.children.indexOf(inputValue) !== -1 ? true : false)}
                                    onChange={(value, option) => handleValuesChange('ctUseroleListOption', option)}
                                >
                                    {selectData.roles.map((x, t) => (
                                        <Option key={x.value}>{x.label}</Option>
                                    ))}
                                </Select>
                            </Form.Item>
                        </Col>
                        <Col span={8}>
                            <Form.Item label="岗位" name="ctUsepostListId">
                                <Select
                                    placeholder="请选择(可多选)"
                                    mode="multiple"
                                    allowClear
                                    filterOption={(inputValue, option) => (option.children.indexOf(inputValue) !== -1 ? true : false)}
                                    onChange={(value, option) => handleValuesChange('ctUsepostListOption', option)}
                                    dropdownRender={(menu) => (
                                        <>
                                            {menu}
                                            <Divider style={{ margin: '8px 0' }} />
                                            <Space style={{ padding: '0 8px 4px' }}>
                                                <Input placeholder="请输入新的岗位" allowClear value={postName} onChange={(e) => setPostName(e.target.value)} />
                                                <Typography.Link
                                                    style={{ whiteSpace: 'nowrap' }}
                                                    onClick={() => {
                                                        if (!postName || postName.length > 32) {
                                                            $$.info({ type: 'error', content: '岗位数据无效,请重新输入' });
                                                            return false;
                                                        }
                                                        handleAddPost(postName);
                                                    }}
                                                >
                                                    <PlusOutlined /> 新增
                                                </Typography.Link>
                                            </Space>
                                        </>
                                    )}
                                >
                                    {post?.map((item) => (
                                        <Option key={item.id}>{item.name}</Option>
                                    ))}
                                </Select>
                            </Form.Item>
                        </Col>
                        <Col span={8}></Col>
                        <Col span={8}>
                            <Form.Item label="调解专长" required name="goodField" >
                                <Select
                                    placeholder="请选择(可多选)"
                                    mode="multiple"
                                    filterOption={(inputValue, option) => (option.children.indexOf(inputValue) !== -1 ? true : false)}
                                    onChange={(value, option) => handleValuesChange('goodFieldOption', [value, option])}
                                >
                                    {$$.caseOptions.caseCause.map((x) => (
                                        <Option key={x.value}>{x.label}</Option>
                                    ))}
                                </Select>
                            </Form.Item>
                        </Col>
                        <Col span={8}>
                            <Form.Item label="调解范围" required name="canField" >
                                <Select
                                    placeholder="请选择(可多选)"
                                    mode="multiple"
                                    filterOption={(inputValue, option) => (option.children.indexOf(inputValue) !== -1 ? true : false)}
                                    onChange={(value, option) => handleValuesChange('canFieldOption', [value, option])}
                                >
                                    {$$.caseOptions.caseCause.map((x) => (
                                        <Option key={x.value}>{x.label}</Option>
                                    ))}
                                </Select>
                            </Form.Item>
                        </Col>
                        <Col span={8}></Col>
                        <Col span={8}>
                            <Form.Item label="是否参与自动调度" required name="dispStatus" rules={[{ required: true }]}>
                                <Select placeholder="请选择">
                                    <Option key="1">否</Option>
                                    <Option key="2">是</Option>
                                </Select>
                            </Form.Item>
                        </Col>
                        <Col span={8}>
                            <Form.Item label="是否可以找他调(小程序)" required name="findStatus" rules={[{ required: true }]}>
                                <Select placeholder="请选择">
                                    <Option key="1">否</Option>
                                    <Option key="2">是</Option>
                                </Select>
                            </Form.Item>
                        </Col>
                        <Col span={24}>
                            <Form.Item>
                                <Button type="primary" onClick={() => handleFinish('1')} className="public-buttonMargin">
                                    {type === 'change' ? '保存' : '提交'}
                                </Button>
                                {type !== 'change' && (
                                    <Button onClick={() => handleFinish('2')} className="public-buttonMargin">
                                        提交并继续
                                    </Button>
                                )}
                                <Button onClick={handleReset}>重置</Button>
                            </Form.Item>
                        </Col>
                    </Row>
                </Form>
            </div>
        </div>
    );
};
 
AddOrEditPerson.propTypes = {
    isAddOrEditPerson: PropTypes.bool,
    type: PropTypes.string,
    formData: PropTypes.object,
    selectData: PropTypes.object,
    post: PropTypes.array,
    handleSubmit: PropTypes.func,
    handleAddPost: PropTypes.func,
};
 
export default AddOrEditPerson;