AddFlowsModal.jsx 12 KB
Newer Older
皮倩雯's avatar
皮倩雯 committed
1 2 3 4 5 6 7 8 9
/* eslint-disable indent */
/* eslint-disable react/jsx-no-duplicate-props */
/* eslint-disable no-shadow */
/* eslint-disable no-undef */
/* eslint-disable eqeqeq */
/* eslint-disable array-callback-return */
/* eslint-disable guard-for-in */
/* eslint-disable no-restricted-syntax */
/* eslint-disable camelcase */
皮倩雯's avatar
皮倩雯 committed
10
import React, { useState, useEffect } from 'react';
皮倩雯's avatar
皮倩雯 committed
11 12 13 14 15 16 17 18 19 20 21 22 23
import {
  Modal,
  Form,
  Input,
  notification,
  message,
  Row,
  Col,
  Select,
  Drawer,
  Space,
  Button,
} from 'antd';
皮倩雯's avatar
皮倩雯 committed
24
import { PlusOutlined } from '@ant-design/icons';
皮倩雯's avatar
皮倩雯 committed
25 26 27 28 29 30 31
import ChangeAddFlows from './ChangeAddFlows';
import {
  CM_Event_ReloadFlows,
  CM_Event_LoadDepartmentAndRoles,
  CM_Event_AddEvenFlow,
  CM_Event_EditEvenFlow,
} from '@/services/standingBook/api';
皮倩雯's avatar
皮倩雯 committed
32
import styles from './incident.less';
皮倩雯's avatar
皮倩雯 committed
33 34

const { Option } = Select;
皮倩雯's avatar
皮倩雯 committed
35
const AddFlowsModal = props => {
皮倩雯's avatar
皮倩雯 committed
36 37 38 39 40 41 42 43 44 45 46 47
  const {
    callBackSubmit = () => {},
    type,
    visible,
    onClose,
    formObj,
    obj,
    maxLength,
    record,
  } = props;
  const [form] = Form.useForm();
  const { Item } = Form;
皮倩雯's avatar
皮倩雯 committed
48

皮倩雯's avatar
皮倩雯 committed
49 50 51 52 53 54 55 56 57 58 59
  const [filed1, setFiled1] = useState({}); // 传给子组件列表数据
  const [standingTable, setStandingTable] = useState([]);
  const [visible1, setVisible1] = useState(false);
  const [pickItem, setPickItem] = useState('');
  const [checkedList1, setCheckedList1] = useState([]);
  const [nu1, setNu1] = useState([]);
  const [inputValue, setInputValue] = useState({ Roles: '' });
  const [selectValue, setSelectValue] = useState('');
  const [characterValue, setCharacterValue] = useState('');
  const [Order, setOrder] = useState(''); // 弹窗类型
  const [strr, setStrr] = useState('');
皮倩雯's avatar
皮倩雯 committed
60

皮倩雯's avatar
皮倩雯 committed
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
  useEffect(() => {
    console.log(formObj);
    getRole();
    console.log(obj);
    CM_Event_ReloadFlows({ eventTypeId: obj }).then(res => {
      if (res.msg === 'Ok') {
        console.log(res.data);
        let arr = formateArrDataA(res.data, 'System');
        let newArr = [];
        Object.keys(arr).map((item, index) => {
          newArr.push(item);
        });
        console.log(newArr);
        setStandingTable(newArr);
      }
    });
    if (type === 'add') {
      setInputValue({ Roles: '' });
      setFiled1({});
      form.resetFields();
    } else if (type === 'edit') {
      setStrr(record.FlowRoles);
      console.log(record);
      setInputValue({
        Roles: record.FlowRoles,
      });
      setSelectValue(record.FlowName);
      console.log(selectValue);
    }
  }, [visible]);
  const formateArrDataA = (initialArr, name) => {
    // 判定传参是否符合规则
    if (!(initialArr instanceof Array)) {
      return '请传入正确格式的数组';
    }
    if (!name) {
      return '请传入对象属性';
    }
    // 先获取一下这个数组中有多少个"name"
    let nameArr = [];
    for (let i in initialArr) {
      if (nameArr.indexOf(initialArr[i][`${name}`]) === -1) {
        nameArr.push(initialArr[i][`${name}`]);
      }
    }
    // 新建一个包含多个list的结果对象
    let tempObj = {};
    // 根据不同的"name"生成多个数组
    for (let k in nameArr) {
      for (let j in initialArr) {
        if (initialArr[j][`${name}`] == nameArr[k]) {
          // 每次外循环时新建一个对应"name"的数组, 内循环时当前数组不变
          tempObj[nameArr[k]] = tempObj[nameArr[k]] || [];
          tempObj[nameArr[k]].push(initialArr[j]);
皮倩雯's avatar
皮倩雯 committed
115
        }
皮倩雯's avatar
皮倩雯 committed
116
      }
皮倩雯's avatar
皮倩雯 committed
117
    }
皮倩雯's avatar
皮倩雯 committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    for (let keys in tempObj) {
      let arr = [];
      tempObj[keys].map((item, index) => {
        tempObj[keys] = arr;
        item.key = index;
        arr.push(item);
      });
    }
    return tempObj;
  };
  const formateArrDataA1 = (initialArr, name) => {
    let aa = [];
    let a1 = [];
    let a2;
    let a3 = [];
皮倩雯's avatar
皮倩雯 committed
133
    initialArr.data.map(i => {
皮倩雯's avatar
皮倩雯 committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
      // console.log(i.groupType)
      a2 = i.groupType;
      // console.log(a2)
      // console.log(i.root)
      a1 = i.root;
      a1.map(j => {
        j.group = a2;
        // console.log(j)
      });
      aa.push(i.root);
    });
    // console.log(aa)
    aa.map(p => {
      // console.log(p)
      p.map(o => {
        // console.log(o)
        a3.push(o);
      });
    });
    console.log(a3);
皮倩雯's avatar
皮倩雯 committed
154

皮倩雯's avatar
皮倩雯 committed
155 156 157
    // 判定传参是否符合规则
    if (!(a3 instanceof Array)) {
      return '请传入正确格式的数组';
皮倩雯's avatar
皮倩雯 committed
158
    }
皮倩雯's avatar
皮倩雯 committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    if (!name) {
      return '请传入对象属性';
    }
    // 先获取一下这个数组中有多少个"name"
    let nameArr = [];
    a3.map(i => {
      if (nameArr.indexOf(i.group) === -1) {
        nameArr.push(i.group);
      }
    });
    console.log(nameArr);
    // 新建一个包含多个list的结果对象
    let tempObj = {};
    // 根据不同的"name"生成多个数组
    for (let k in nameArr) {
      for (let j in a3) {
        if (a3[j].group == nameArr[k]) {
          // 每次外循环时新建一个对应"name"的数组, 内循环时当前数组不变
          tempObj[nameArr[k]] = tempObj[nameArr[k]] || [];
          tempObj[nameArr[k]].push(a3[j]);
皮倩雯's avatar
皮倩雯 committed
179
        }
皮倩雯's avatar
皮倩雯 committed
180
      }
皮倩雯's avatar
皮倩雯 committed
181
    }
皮倩雯's avatar
皮倩雯 committed
182 183 184 185 186 187 188
    console.log(tempObj);
    for (let key in tempObj) {
      let arr = [];
      tempObj[key].map(item => {
        tempObj[key] = arr;
        arr.push(item.Name);
      });
皮倩雯's avatar
皮倩雯 committed
189
    }
皮倩雯's avatar
皮倩雯 committed
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
    return tempObj;
  };
  const onSumbit = () => {
    callBackSubmit();
    // setLoading(true);
    let aa = form.getFieldsValue().FlowName;
    console.log(aa);
    console.log(inputValue);
    console.log(strr);
    if (type == 'add') {
      let index = aa.lastIndexOf('\\');
      aa = aa.substring(index + 1, aa.length);
      CM_Event_AddEvenFlow({
        eventTypeId: formObj.ID,
        flowName: aa,
        roles: inputValue.Roles,
        order: maxLength,
      })
        .then(res => {
          // setLoading(false);
          if (res.msg === 'Ok') {
            form.resetFields();
            callBackSubmit();
            notification.success({
              message: '提示',
              duration: 3,
              description: '新增成功',
            });
          } else {
            notification.error({
              message: '提示',
              duration: 3,
              description: res.msg,
            });
          }
225
        })
皮倩雯's avatar
皮倩雯 committed
226 227 228 229 230 231 232 233
        .catch(error => {
          notification.error({
            message: '提示',
            duration: 3,
            description: '新增失败',
          });
          // setLoading(false);
        });
皮倩雯's avatar
皮倩雯 committed
234
    }
皮倩雯's avatar
皮倩雯 committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    if (type == 'edit') {
      CM_Event_EditEvenFlow({
        eventTypeId: formObj.ID,
        eventFlowId: record.ID,
        roles: inputValue.Roles,
      })
        .then(res => {
          // setLoading(false);
          if (res.msg === '') {
            form.resetFields();
            callBackSubmit();
            notification.success({
              message: '提示',
              duration: 3,
              description: '编辑成功',
            });
          } else {
皮倩雯's avatar
皮倩雯 committed
252
            notification.error({
皮倩雯's avatar
皮倩雯 committed
253 254 255
              message: '提示',
              duration: 3,
              description: res.msg,
皮倩雯's avatar
皮倩雯 committed
256
            });
皮倩雯's avatar
皮倩雯 committed
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
          }
        })
        .catch(error => {
          notification.error({
            message: '提示',
            duration: 3,
            description: '编辑失败',
          });
          setLoading(false);
        });
    }
  };

  const getRole = () => {
    CM_Event_LoadDepartmentAndRoles().then(res => {
      if (res.msg === 'Ok') {
        setNu1(res.data);
        setFiled1(formateArrDataA1(res.data, 'groupType'));
        console.log(res.data);
      }
    });
  };
  const changeText = (e, type) => {
    let inputText = { ...inputValue };
    inputText[type] = e.target.value;
    setInputValue(inputText);
  };
  const pickFiled1 = fileds => {
    // setTypes('app')
    let pp = formateArrDataA1(nu1, 'group');
    let ab = inputValue[fileds].split(',');
    console.log(pp);
    let arr = Object.keys(pp);
    let b = [];
    let a = [];
    arr.map((item, index) => {
      pp[item].map((i, j) => {
        b.push(i);
      });
    });
    console.log(ab);
    ab.map((item, index) => {
      if (b.includes(item) == false) {
        if (item == '') {
          a = [];
皮倩雯's avatar
皮倩雯 committed
302
        } else {
皮倩雯's avatar
皮倩雯 committed
303
          a.push(item);
皮倩雯's avatar
皮倩雯 committed
304
        }
皮倩雯's avatar
皮倩雯 committed
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
      }
    });
    if (a.length > 0) {
      pp.外部字段 = a;
      console.log(pp);
      arr.push('外部字段');
    }
    console.log(a);
    console.log(pp);
    setFiled1(pp);
    console.log(fileds);
    setCharacterValue(inputValue[fileds]);
    setCheckedList1(inputValue[fileds].split(','));
    setPickItem(fileds);
    setVisible1(true);
  };
  const onOKk = prop => {
    let { title } = prop;
    let selectData = prop.stt;
    let ff = prop.filed22;
    let aa = [];
    let bb = 0;
    title.map((item, index) => {
      console.log(ff[item]);
      aa = [];
      selectData.map((item1, index1) => {
        console.log(item1);
        if (ff[item].indexOf(item1) != -1) {
          aa.push(item1);
        }
      });
      console.log(aa);
      if (aa.length == 0) {
        bb = 1;
      }
    });
    console.log(aa);
    if (bb == 1) {
      notification.error({
        message: '提示',
        description: '权限角色部门必须都选至少一项',
      });
    } else {
      setVisible1(false);
      let inputText = { ...inputValue };
      inputText[prop.pickItem] = prop.str;
      console.log(prop.str);
      console.log(inputText);
      setStrr(prop.str);
      setCheckedList1(prop.checkedList);
      setInputValue(inputText);
皮倩雯's avatar
皮倩雯 committed
356
    }
皮倩雯's avatar
皮倩雯 committed
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 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
  };
  return (
    <Drawer
      title={type === 'add' ? '添加受理流程' : '编辑受理流程'}
      visible={visible}
      width="600px"
      onClose={onClose}
      destroyOnClose
      footer={
        <Space>
          <Button onClick={onSumbit} type="primary">
            确定
          </Button>
        </Space>
      }
    >
      <Form form={form} labelCol={{ span: 7 }} style={{ overflowY: 'scroll' }}>
        <Row>
          <Col span={23}>
            {type === 'edit' ? (
              <>
                <Item label="受理流程" name="FlowName" labelCol={{ span: 4 }}>
                  <Select
                    placeholder="选择受理流程"
                    placeholder={selectValue}
                    disabled
                  >
                    {standingTable
                      ? standingTable.map((item, index) => (
                          <Option key={index} value={item}>
                            {item}
                          </Option>
                        ))
                      : ''}
                  </Select>
                </Item>
              </>
            ) : (
              <>
                <Item label="受理流程" name="FlowName" labelCol={{ span: 4 }}>
                  <Select placeholder="选择受理流程">
                    {standingTable
                      ? standingTable.map((item, index) => (
                          <Option key={index} value={item}>
                            {item}
                          </Option>
                        ))
                      : ''}
                  </Select>
                </Item>
              </>
            )}
          </Col>
          <Col span={23}>
            <Item label="受理权限" name="Roles" labelCol={{ span: 4 }}>
              <div className={styles.filed_listItem}>
                <Input
                  style={{ width: '83%' }}
                  placeholder="请选择受理权限"
                  onChange={e => changeText(e, 'Roles')}
                  value={inputValue.Roles}
                  allowClear
                />
                <Button
                  type="dashed"
                  onClick={() => pickFiled1('Roles')}
                  icon={<PlusOutlined style={{ marginTop: '5px' }} />}
                  style={{ marginLeft: '0.5rem', width: '4rem' }}
                />
              </div>
            </Item>
          </Col>
        </Row>
      </Form>
      <ChangeAddFlows
        visible={visible1}
        onCancel={() => setVisible1(false)}
        callBackSubmit={onOKk}
        newCheckedList={checkedList1}
        filed1={filed1}
        pickItem={pickItem}
        characterValue={characterValue}
        formObj={formObj}
      />
    </Drawer>
  );
};
export default AddFlowsModal;