ChangeAddFlows.jsx 17.3 KB
Newer Older
皮倩雯's avatar
皮倩雯 committed
1 2 3 4
/* eslint-disable no-plusplus */
/* eslint-disable array-callback-return */
/* eslint-disable no-unused-expressions */
/* eslint-disable no-shadow */
5
import React, { useState, useEffect } from 'react';
6
import { Modal, Checkbox, Tabs, Input, Button, Pagination, Empty } from 'antd';
7
import { CM_Event_LoadDepartmentAndRoles } from '@/services/standingBook/api';
皮倩雯's avatar
皮倩雯 committed
8

皮倩雯's avatar
皮倩雯 committed
9
import styles from './incident.less';
皮倩雯's avatar
皮倩雯 committed
10
const CheckboxGroup = Checkbox.Group;
皮倩雯's avatar
皮倩雯 committed
11
const { TabPane } = Tabs;
皮倩雯's avatar
皮倩雯 committed
12
const ChangeAddFlows = props => {
13
  const { callBackSubmit = () => {}, pickItem, visible, filed11, newCheckedList } = props;
皮倩雯's avatar
皮倩雯 committed
14 15 16
  const [value, setValue] = useState('');
  const [checkValue, setCheckValue] = useState([]);
  const [title, setTitle] = useState([]);
17
  const [checkedList, setCheckedList] = useState([]); // 选中复选框内容
皮倩雯's avatar
皮倩雯 committed
18 19 20 21
  const [flagg, setFlagg] = useState(0);
  const [searchWord, setSearchWord] = useState(''); // 关键字
  const { Search } = Input;
  const [total, setTotal] = useState();
皮倩雯's avatar
皮倩雯 committed
22
  const [pageSize, setPageSize] = useState(40);
皮倩雯's avatar
皮倩雯 committed
23
  const [currentPage, setCurrentPage] = useState(1);
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
  const [roleValue, setRoleValue] = useState([]);
  const [checkDataRole, setCheckDataRole] = useState([]);
  const [checkDataPm, setCheckDataPm] = useState([]);
  const [checkDataSite, setCheckDataSite] = useState([]);
  const [filed1, setFiled1] = useState([]); // 角色全部数据
  const [saveCheckValue, setSaveCheckValue] = useState([]); // 保存选中值
  const [isCheck, setIsCheck] = useState(0); // 是否点击了角色复选框下的
  const [isCheck1, setIsCheck1] = useState(0); // 是否点击了部门复选框下的
  const [isCheck2, setIsCheck2] = useState(0); // 是否点击了站点复选框下的
  const [keepOption1, setKeepOption1] = useState([]); // 保存了获取的部门列表数据
  const [keepOption2, setKeepOption2] = useState([]); // 保存了获取的站点列表数据
  const [keepTabKey, setKeepTabKey] = useState(0);
  const [searchFlag, setSearchFlag] = useState(0);
  const [keepSearchList, setkeepSearchList] = useState([]); // 保存搜索后出现的选中列表
  const [list, setList] = useState([]);
  const [listPm, setListPm] = useState([]);
  const [listSite, setListSite] = useState([]);
  const [keyValue, setKeyValue] = useState('0');
42
  const [emptyValue, setEmptyValue] = useState(0);
皮倩雯's avatar
皮倩雯 committed
43

44 45 46 47 48 49 50
  const onSubmit = () => {
    // 没有勾选部门下的选项
    if (saveCheckValue[1] == undefined) {
      let data = [];
      newCheckedList.map(checkItem => {
        if (keepOption1.includes(checkItem)) {
          data.push(checkItem);
皮倩雯's avatar
皮倩雯 committed
51 52
        }
      });
53 54
      console.log(data);
      saveCheckValue[1] = data;
皮倩雯's avatar
皮倩雯 committed
55
    }
56 57 58 59 60 61 62 63
    console.log(saveCheckValue);
    // 没有勾选站点下的选项
    if (saveCheckValue[2] == undefined) {
      let data = [];
      newCheckedList.map(checkItem => {
        if (keepOption2.includes(checkItem)) {
          data.push(checkItem);
        }
皮倩雯's avatar
皮倩雯 committed
64
      });
65 66
      console.log(data);
      saveCheckValue[2] = data;
皮倩雯's avatar
皮倩雯 committed
67
    }
68 69 70 71 72
    console.log(newCheckedList);
    console.log(saveCheckValue);
    let data = [];
    saveCheckValue.map((item, index) => {
      data = data.concat(item);
皮倩雯's avatar
皮倩雯 committed
73
    });
皮倩雯's avatar
皮倩雯 committed
74 75 76 77 78 79 80 81
    callBackSubmit({
      saveCheckValue,
      str: data.join(','),
      pickItem,
      stt: data,
      title,
      filed22: filed1,
    });
82 83 84 85 86
    setIsCheck(0);
    setIsCheck1(0);
    setIsCheck2(0);
    setKeyValue('0');
    setSearchWord('');
皮倩雯's avatar
皮倩雯 committed
87
  };
88

皮倩雯's avatar
皮倩雯 committed
89
  useEffect(() => {
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
    setKeepOption1(filed11['部门']);
    setKeepOption2(filed11['站点']);
    getRoles('角色', '', pageSize, 1);
    setRoleValue(filed1);
    setFlagg(1);
    let arr = Object.keys(filed1);
    setTitle(arr);
    let checkArr = [];
    console.log(newCheckedList); // 已选中数组
    let listdata = [];
    newCheckedList.map(checkItem => {
      if (filed11['角色'].includes(checkItem)) {
        listdata.push(checkItem);
      }
    });
    setCheckDataRole(listdata);
    checkArr[0] = listdata;
    console.log(listdata); // 按对象数组格式
    setSaveCheckValue(checkArr);
    setCheckedList(checkArr);
    let pmdata = [];
    newCheckedList.map(checkItem => {
      if (filed11['部门'].includes(checkItem)) {
        pmdata.push(checkItem);
      }
    });
    console.log(pmdata);
    setCheckDataPm(pmdata);
    let sitedata = [];
    newCheckedList.map(checkItem => {
      if (filed11['站点'].includes(checkItem)) {
        sitedata.push(checkItem);
      }
    });
    console.log(sitedata);
    setCheckDataSite(sitedata);
  }, [visible]);

  const submitSearchUser = () => {
    if (searchWord != '') {
      setSearchFlag(1);
    }

    if (keepTabKey == 0) {
      sreachRoles('角色', searchWord, pageSize, 1);
皮倩雯's avatar
皮倩雯 committed
135
    }
136 137
    if (keepTabKey == 1) {
      sreachRoles('部门', searchWord, pageSize, 1);
皮倩雯's avatar
皮倩雯 committed
138
    }
139 140 141 142
    if (keepTabKey == 2) {
      sreachRoles('站点', searchWord, pageSize, 1);
    }
  };
皮倩雯's avatar
皮倩雯 committed
143

144 145 146 147 148 149 150 151 152 153
  // 获取搜索框的值
  const handleSearch = e => {
    if (e.target.value == '') {
      setSearchFlag(0);
    }
    setSearchWord(e.target.value);
  };

  // 监听分页
  const paginationChange = (page, pageSizes) => {
154 155 156 157 158 159 160 161
    if (keepTabKey == 0) {
      getRoles('角色', searchWord, pageSizes, page);
    }
    if (keepTabKey == 1) {
      getRoles('部门', searchWord, pageSizes, page);
    }
    if (keepTabKey == 2) {
      getRoles('站点', searchWord, pageSizes, page);
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    }
    setCurrentPage(page);
    setPageSize(pageSizes);
  };
  // 搜索时获取数据
  const sreachRoles = (type, keywords, pageSizes, pageIndex) => {
    CM_Event_LoadDepartmentAndRoles({
      type,
      keywords,
      pageSize: pageSizes,
      pageIndex,
    }).then(res => {
      if (res.msg === 'Ok') {
        setRoleValue(formateArrDataA1(res.data, 'groupType'));
        setFiled1(formateArrDataA1(res.data, 'groupType'));
177
        console.log(res.data.count);
178 179
        setCurrentPage(1);
        setTotal(res.data.count);
180 181 182 183 184
        if (res.data.count == 0) {
          setEmptyValue(1);
        } else {
          setEmptyValue(0);
        }
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
      }
    });
  };

  // 重置
  const onReset = () => {
    setSearchFlag(0);
    // setListPm([]);
    if (keepTabKey == 0) {
      getRoles('角色', '', pageSize, 1);
    }
    if (keepTabKey == 1) {
      getRoles('部门', '', pageSize, 1);
    }
    if (keepTabKey == 2) {
      getRoles('站点', '', pageSize, 1);
    }
    setCurrentPage(1);
    setSearchWord('');
  };

  const getRoles = (type, keywords, pageSizes, pageIndex) => {
    CM_Event_LoadDepartmentAndRoles({
      type,
      keywords,
      pageSize: pageSizes,
      pageIndex,
    }).then(res => {
      if (res.msg === 'Ok') {
        setTotal(res.data.count);
        setRoleValue(formateArrDataA1(res.data, 'groupType'));
        setFiled1(formateArrDataA1(res.data, 'groupType'));
217 218 219 220 221
        if (res.data.count == 0) {
          setEmptyValue(1);
        } else {
          setEmptyValue(0);
        }
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
      }
    });
  };

  useEffect(() => {
    let arr = Object.keys(filed1);
    setTitle(arr);
    let checkArr = [];
    console.log(saveCheckValue);
    console.log(newCheckedList); // 已选中数组
    let data = [];
    saveCheckValue.map((item, index) => {
      data = data.concat(item);
    });
    arr.forEach(item => {
      if (item == '角色') {
        checkArr[0] = [];
        checkDataRole.map(checkItem => {
          if (filed1['角色'].includes(checkItem)) {
            checkArr[0].push(checkItem);
皮倩雯's avatar
皮倩雯 committed
242 243
          }
        });
244 245 246 247 248 249 250 251 252
        // setCheckDataRole(checkArr[0]);
        console.log(arr);
        console.log(checkArr); // 按对象数组格式
        setCheckedList(checkArr);
      } else if (item == '部门') {
        checkArr[1] = [];
        checkDataPm.map(checkItem => {
          if (filed1['部门'].includes(checkItem)) {
            checkArr[1].push(checkItem);
皮倩雯's avatar
皮倩雯 committed
253 254
          }
        });
255 256 257 258 259 260 261 262 263 264
        // setCheckDataPm(checkArr[1]);
        console.log(checkArr); // 按对象数组格式
        setCheckedList(checkArr);
      } else if (item == '站点') {
        checkArr[2] = [];
        console.log(checkDataSite);
        checkDataSite.map(checkItem => {
          if (filed1['站点'].includes(checkItem)) {
            checkArr[2].push(checkItem);
          }
皮倩雯's avatar
皮倩雯 committed
265
        });
266 267 268
        // setCheckDataSite(checkArr[2]);
        console.log(checkArr); // 按对象数组格式
        setCheckedList(checkArr);
皮倩雯's avatar
皮倩雯 committed
269
      }
270 271 272 273 274 275 276 277 278 279 280 281 282 283
    });
  }, [filed1]);

  const formateArrDataA1 = (initialArr, name) => {
    console.log(initialArr);
    let aa = [];
    let a1 = [];
    let a2;
    let a3 = [];
    initialArr.data.map(i => {
      a2 = i.groupType;
      a1 = i.root;
      a1.map(j => {
        j.group = a2;
皮倩雯's avatar
皮倩雯 committed
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
      aa.push(i.root);
    });
    aa.map(p => {
      p.map(o => {
        a3.push(o);
      });
    });
    console.log(a3);

    // 判定传参是否符合规则
    if (!(a3 instanceof Array)) {
      return '请传入正确格式的数组';
    }
    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
319 320
      }
    }
321 322 323 324 325 326 327 328 329
    console.log(tempObj);
    for (let key in tempObj) {
      let arr = [];
      tempObj[key].map(item => {
        tempObj[key] = arr;
        arr.push(item.Name);
      });
    }
    return tempObj;
皮倩雯's avatar
皮倩雯 committed
330
  };
皮倩雯's avatar
皮倩雯 committed
331

皮倩雯's avatar
皮倩雯 committed
332
  useEffect(() => {
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    let checkedListArr = [...saveCheckValue];
    setIsCheck(1);
    let data = checkDataRole;
    let DataValue = [];
    checkDataRole.forEach(item => {
      if (roleValue['角色'].indexOf(item) != -1) {
        DataValue.push(item);
      }
    });
    console.log(DataValue);
    // del
    if (DataValue.length > list.length) {
      DataValue.forEach(item => {
        if (list.indexOf(item) == -1) {
          data.splice(data.findIndex(ele => ele === item), 1);
        }
      });
    } else {
      // add
      list.forEach(item => {
        if (data.indexOf(item) == -1) {
          data.push(item);
        }
皮倩雯's avatar
皮倩雯 committed
356
      });
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
    }
    checkedListArr[0] = data;
    setCheckDataRole(data);
    setSaveCheckValue(checkedListArr);
    setCheckedList(checkedListArr);
  }, [list]);

  useEffect(() => {
    let checkedListArr = [...saveCheckValue];
    setIsCheck(1);
    setIsCheck1(1);
    let data = checkDataPm;
    let DataValue = [];
    checkDataPm.forEach(item => {
      if (roleValue['部门'].indexOf(item) != -1) {
        DataValue.push(item);
皮倩雯's avatar
皮倩雯 committed
373
      }
374 375 376 377 378 379 380 381 382
    });
    console.log(DataValue);
    // del
    if (DataValue.length > listPm.length) {
      DataValue.forEach(item => {
        if (listPm.indexOf(item) == -1) {
          data.splice(data.findIndex(ele => ele === item), 1);
        }
      });
皮倩雯's avatar
皮倩雯 committed
383
    } else {
384 385 386 387 388 389
      // add
      listPm.forEach(item => {
        if (data.indexOf(item) == -1) {
          data.push(item);
        }
      });
皮倩雯's avatar
皮倩雯 committed
390
    }
391 392 393 394 395
    checkedListArr[1] = data;
    setCheckDataPm(data);
    setSaveCheckValue(checkedListArr);
    setCheckedList(checkedListArr);
  }, [listPm]);
皮倩雯's avatar
皮倩雯 committed
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
  useEffect(() => {
    setIsCheck(1);
    const checkedListArr = [...saveCheckValue];
    setIsCheck(1);
    let data = checkDataSite;
    let DataValue = [];
    checkDataSite.forEach(item => {
      if (roleValue['站点'].indexOf(item) != -1) {
        DataValue.push(item);
      }
    });
    console.log(DataValue);
    // del
    if (DataValue.length > listSite.length) {
      DataValue.forEach(item => {
        if (listSite.indexOf(item) == -1) {
          data.splice(data.findIndex(ele => ele === item), 1);
        }
      });
    } else {
      // add
      listSite.forEach(item => {
        if (data.indexOf(item) == -1) {
          data.push(item);
        }
      });
    }
    checkedListArr[2] = data;
    setCheckDataSite(data);
    setSaveCheckValue(checkedListArr);
    setCheckedList(checkedListArr);
  }, [listSite]);
  const handleParChange = key => {
    setKeyValue(key);
431
    // setSearchWord('');
432 433 434 435 436 437
    setSearchFlag(0);
    setCurrentPage(1);
    setKeepTabKey(key);
    let value;
    if (key == 0) {
      value = '角色';
438
      getRoles('角色', searchWord, pageSize, 1);
439 440 441
    }
    if (key == 1) {
      value = '部门';
442
      getRoles('部门', searchWord, pageSize, 1);
443 444 445
    }
    if (key == 2) {
      value = '站点';
446
      getRoles('站点', searchWord, pageSize, 1);
皮倩雯's avatar
皮倩雯 committed
447 448
    }
  };
皮倩雯's avatar
皮倩雯 committed
449

450 451
  const onChangeListNew = list => {
    setList(list);
皮倩雯's avatar
皮倩雯 committed
452
  };
453 454 455 456 457
  const onChangeListNew1 = list => {
    setListPm(list);
  };
  const onChangeListNew2 = list => {
    setListSite(list);
皮倩雯's avatar
皮倩雯 committed
458
  };
皮倩雯's avatar
皮倩雯 committed
459 460
  return (
    <Modal
皮倩雯's avatar
皮倩雯 committed
461 462 463 464
      title="权限选择"
      bodyStyle={{ width: '100%', height: '650px', overflowY: 'scorll' }}
      width="1000px"
      style={{ top: '10px' }}
皮倩雯's avatar
皮倩雯 committed
465 466 467 468 469 470 471 472
      destroyOnClose
      centered
      cancelText="取消"
      okText="确认"
      {...props}
      onOk={() => onSubmit()}
      forceRender
      getContainer={false}
473 474 475 476
      afterClose={() => {
        setKeyValue('0');
        setSearchWord('');
      }}
皮倩雯's avatar
皮倩雯 committed
477
    >
478
      <Search
皮倩雯's avatar
皮倩雯 committed
479 480 481 482 483 484 485 486 487 488 489
        style={{
          width: 260,
          marginRight: '20px',
          marginBottom: '10px',
        }}
        placeholder="搜索"
        onSearch={submitSearchUser}
        onChange={e => handleSearch(e)}
        enterButton
        value={searchWord}
      />
490
      <Button type="primary" htmlType="submit" onClick={onReset}>
皮倩雯's avatar
皮倩雯 committed
491
        重置
492
      </Button>
皮倩雯's avatar
皮倩雯 committed
493 494
      {visible && (
        <div className={styles.listCard1}>
皮倩雯's avatar
皮倩雯 committed
495
          <div className={styles.cardItem1}>
皮倩雯's avatar
皮倩雯 committed
496
            <div className={styles.cardContent1}>
497
              <Tabs activeKey={keyValue} className={styles.tab} onChange={handleParChange}>
498 499
                <TabPane tab="角色" key="0">
                  <div className={styles.cardItemData1}>
500 501 502 503 504 505 506
                    {emptyValue == 0 ? (
                      <CheckboxGroup
                        options={roleValue['角色']}
                        value={checkedList[0]}
                        onChange={onChangeListNew}
                      />
                    ) : (
507
                      <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="暂无数据" />
508
                    )}
509 510 511 512
                  </div>
                </TabPane>
                <TabPane tab="部门" key="1">
                  <div className={styles.cardItemData1}>
513 514 515 516
                    {emptyValue == 0 ? (
                      <CheckboxGroup
                        options={roleValue['部门']}
                        value={checkedList[1]}
517
                        onChange={onChangeListNew1}
518 519
                      />
                    ) : (
520
                      <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="暂无数据" />
521
                    )}
522 523 524 525
                  </div>
                </TabPane>
                <TabPane tab="站点" key="2">
                  <div className={styles.cardItemData1}>
526 527 528 529
                    {emptyValue == 0 ? (
                      <CheckboxGroup
                        options={roleValue['站点']}
                        value={checkedList[2]}
530
                        onChange={onChangeListNew2}
531 532
                      />
                    ) : (
533
                      <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="暂无数据" />
534
                    )}
535 536
                  </div>
                </TabPane>
皮倩雯's avatar
皮倩雯 committed
537
              </Tabs>
皮倩雯's avatar
皮倩雯 committed
538 539
            </div>
          </div>
皮倩雯's avatar
皮倩雯 committed
540
          <div className={styles.cardItem11}>
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
            <div className={styles.cardContent1}>
              <Tabs
                // defaultActiveKey="0"
                className={styles.tab}
                activeKey={keyValue}
                onChange={handleParChange}
              >
                <TabPane tab="角色" key="0">
                  {checkDataRole.map((item, index) => (
                    <div
                      key={index}
                      style={{
                        marginBottom: '5px',
                      }}
                    >
                      {item}
                    </div>
                  ))}
                </TabPane>
                <TabPane tab="部门" key="1">
                  {checkDataPm.map((item, index) => (
                    <div
                      key={index}
                      style={{
                        marginBottom: '5px',
                      }}
                    >
                      {item}
                    </div>
                  ))}
                </TabPane>
                <TabPane tab="站点" key="2">
                  {checkDataSite.map((item, index) => (
                    <div
                      key={index}
                      style={{
                        marginBottom: '5px',
                      }}
                    >
                      {item}
                    </div>
                  ))}
                </TabPane>
              </Tabs>
            </div>
皮倩雯's avatar
皮倩雯 committed
586
          </div>
皮倩雯's avatar
皮倩雯 committed
587 588
        </div>
      )}
589
      <Pagination
皮倩雯's avatar
皮倩雯 committed
590 591 592 593 594 595
        total={total}
        showTotal={item => `共 ${item} 条`}
        defaultPageSize={pageSize}
        defaultCurrent={1}
        current={currentPage}
        onChange={paginationChange}
皮倩雯's avatar
皮倩雯 committed
596
        pageSizeOptions={[10, 20, 40, 100]}
皮倩雯's avatar
皮倩雯 committed
597 598 599
        style={{ marginBottom: '10px' }}
        size="small"
        showQuickJumper
600
      />
皮倩雯's avatar
皮倩雯 committed
601 602
    </Modal>
  );
皮倩雯's avatar
皮倩雯 committed
603
};
皮倩雯's avatar
皮倩雯 committed
604
export default ChangeAddFlows;