/*
 * @Description:
 * @Author: leizhe
 * @Date: 2022-01-13 10:47:32
 * @LastEditTime: 2022-03-31 17:37:04
 * @LastEditors: leizhe
 */
/* eslint-disable array-callback-return */
/* eslint-disable no-plusplus */
import React, { useState, useEffect } from 'react';
import { Modal } from 'antd';

import Sortable from 'sortablejs';
import styles from './incident.less';
import DragTable from '@/components/DragTable/DragTable';
const SortModal = props => {
  const { callBackSubmit = () => {}, title, visible, onCancel, sortData1, keepIdValue } = props;
  const [orderTable, setOrderTable] = useState([]);
  const [flowIDs, setFlowIDs] = useState([]);

  const onSumbit = () => {
    callBackSubmit({ str: flowIDs });
  };

  // 根据orderTable值改变flowIDs
  useEffect(() => {
    let ids = [];
    orderTable.map(item => {
      ids.push(item.ID);
    });
    setFlowIDs(ids);
  }, [orderTable]);

  useEffect(() => {
    if (visible) {
      let aa = [];
      keepIdValue.map(i => {
        i.root.map(j => {
          aa.push(j);
        });
      });
      setOrderTable(aa);
    }
  }, [visible]);

  // 拖拽回调函数
  const dragCallBack = data => {
    if (data) {
      setOrderTable(data);
    }
  };

  const columns = [
    {
      title: '序号',
      dataIndex: 'order',
      width: 50,
      key: 'order',
    },
    {
      title: '业务类型',
      dataIndex: 'businessType',
      width: 150,
      key: 'businessType',
    },
    {
      title: '事件类型',
      dataIndex: 'name',
      width: 150,
      key: 'name',
    },
  ];

  return (
    <Modal
      title={title}
      width="600px"
      visible={visible}
      onCancel={onCancel}
      onOk={onSumbit}
      okText="确认"
      cancelText="取消"
    >
      <div className={styles.cardContent} style={{ width: '90%', marginLeft: '24px' }}>
        <div style={{ minHeight: '200px' }}>
          <DragTable
            bordered
            style={{ marginBottom: '10px' }}
            rowKey={record => record.extendID}
            columns={columns}
            dataSource={orderTable}
            // showHeader={false}
            pagination={false}
            size="small"
            dragCallBack={dragCallBack}
            ItemTypes="flowOrder"
            scroll={{
              y: 500,
            }}
          />
        </div>
      </div>
    </Modal>
  );
};
export default SortModal;
