/*
 * @Description:
 * @Author: leizhe
 * @Date: 2022-01-13 10:47:32
 * @LastEditTime: 2022-07-20 14:16:47
 * @LastEditors: leizhe
 */
/* eslint-disable array-callback-return */
/* eslint-disable no-plusplus */
import React, { useState, useEffect } from 'react';
import { Modal, notification } from 'antd';
import { SetIntegratedloginSettingSort } from '@/services/integratedLogin/api';

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

  const onSumbit = () => {
    SetIntegratedloginSettingSort({ names: flowIDs }).then(res => {
      if (res.code === 0) {
        notification.success({
          message: '提示',
          duration: 3,
          description: '调整成功',
        });
        callBackSubmit();
      } else {
        notification.error({
          message: '提示',
          duration: 3,
          description: res.msg,
        });
      }
    });
    // console.log(flowIDs);
    // callBackSubmit({ str: flowIDs });
  };

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

  useEffect(() => {
    console.log(sortData);
    if (visible) {
      setOrderTable(sortData);
    }
  }, [visible]);

  // 拖拽回调函数
  const dragCallBack = data => {
    console.log(data);
    if (data) {
      setOrderTable(data);
    }
  };
  const columns = [
    {
      title: '名称',
      dataIndex: 'SystemName',
      width: 150,
      key: 'SystemName',
    },
  ];
  return (
    <Modal
      title={title}
      visible={visible}
      onCancel={onCancel}
      onOk={onSumbit}
      okText="确认"
      cancelText="取消"
    >
      <div
        className={styles.cardContent}
        style={{ width: '26rem', marginLeft: '24px', maxHeight: '400px', overflow: 'auto' }}
      >
        <div className={styles.doctorTable}>
          <DragTable
            bordered
            style={{ marginBottom: '10px' }}
            rowKey={record => record.id}
            columns={columns}
            dataSource={orderTable}
            showHeader={false}
            pagination={false}
            size="small"
            dragCallBack={dragCallBack}
            ItemTypes="flowOrder"
          />
        </div>
      </div>
    </Modal>
  );
};
export default SortModal;
