/*
 * @Description:
 * @Author: leizhe
 * @Date: 2022-01-13 10:47:32
 * @LastEditTime: 2022-07-22 10:46:13
 * @LastEditors: leizhe
 */
/* eslint-disable array-callback-return */
/* eslint-disable no-plusplus */
import React, { useState, useEffect } from 'react';
import { Modal, notification } from 'antd';
import { CM_Feedback_ChangPatrolOrder } from '@/services/PatrolFeedback/api';
import styles from './patrolFeedback.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 = () => {
    console.log(flowIDs);
    CM_Feedback_ChangPatrolOrder(flowIDs.toString()).then(res => {
      if (res.code === 0) {
        callBackSubmit();
        onCancel();
        notification.success({
          message: '提示',
          duration: 3,
          description: '调整成功',
        });
      } else {
        notification.error({
          message: '提示',
          duration: 3,
          description: res.msg,
        });
      }
    });
    // callBackSubmit({ str: flowIDs });
  };

  // 根据orderTable值改变flowIDs
  useEffect(() => {
    let ids = [];
    orderTable.map(item => {
      ids.push(item.id);
    });
    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: 'layerName',
      width: 150,
      key: 'layerName',
    },
  ];
  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;
