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

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

  const onSumbit = () => {
    SortSchemePost({ sortNames: flowIDs }).then(res => {
      if (res.code === '0') {
        callBackSubmit();
        onCancel();
        notification.success({
          message: '提示',
          duration: 3,
          description: '调整成功',
        });
      } else {
        notification.error({
          message: '提示',
          duration: 3,
          description: res.msg,
        });
      }
    });
  };

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

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

  // 拖拽回调函数
  const dragCallBack = data => {
    if (data) {
      setOrderTable(data);
    }
  };
  const columns = [
    {
      title: '字段名',
      dataIndex: 'schemename',
      width: 150,
      key: 'schemename',
    },
  ];
  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;
