/*
 * @Description:
 * @Author: leizhe
 * @Date: 2022-01-13 17:26:13
 * @LastEditTime: 2023-02-08 15:43:35
 * @LastEditors: dengchao 754083046@qq.com
 */
import React, { useState, useEffect } from 'react';
import { flowReOrder } from '@/services/flow/flow';
import { Modal, notification } from 'antd';
import DragTable from '@/components/DragTable/DragTable';
const Order = props => {
  const { visible, handleCancel, tableData, submitCallBack, processData } = props;
  const [orderTable, setOrderTable] = useState([]);
  const [flowIDs, setFlowIDs] = useState('');
  // 页面弹出的时候初始化拖拽数据
  useEffect(() => {
    if (visible) {
      console.log(tableData);
      console.log(processData);
      let list = [];
      processData.map(i => {
        i.children.map(j => {
          list.push(j);
        });
      });
      console.log(list, 'list');
      setOrderTable(list);
    }
  }, [visible]);
  // 根据orderTable值改变flowIDs
  useEffect(() => {
    let ids = '';
    orderTable.forEach((item, index) => {
      if (index === orderTable.length - 1) {
        ids += `${item.ExtendID}`;
      } else {
        ids += `${item.ExtendID},`;
      }
    });
    setFlowIDs(ids);
  }, [orderTable]);
  // 提交表单
  const onSubmit = () => {
    flowReOrder({ flowIDs }).then(res => {
      if (res.code === 0) {
        notification.success({
          message: '提示',
          duration: 3,
          description: '保存成功',
        });
        submitCallBack();
      } else {
        notification.error({
          message: '提示',
          duration: 3,
          description: res.msg,
        });
      }
    });
  };
  // 拖拽回调函数
  const dragCallBack = data => {
    if (data) {
      setOrderTable(data);
    }
  };
  const columns = [
    {
      title: '序号',
      dataIndex: 'Order',
      width: 50,
      key: 'Order',
    },
    {
      title: '分组名',
      dataIndex: 'Type',
      width: 150,
      key: 'Type',
    },
    {
      title: '流程名称',
      dataIndex: 'FlowName',
      width: 150,
      key: 'FlowName',
    },
  ];
  return (
    <Modal
      title="调整顺序"
      width="600px"
      visible={visible}
      onOk={onSubmit}
      onCancel={handleCancel}
      maskClosable={false}
      destroyOnClose
    >
      <div style={{ minHeight: '200px' }}>
        <DragTable
          bordered
          style={{ marginBottom: '10px' }}
          rowKey={record => record.Code}
          columns={columns}
          dataSource={orderTable}
          // showHeader={false}
          pagination={false}
          size="small"
          dragCallBack={dragCallBack}
          ItemTypes="flowOrder"
          scroll={{
            y: 500,
          }}
        />
      </div>
    </Modal>
  );
};
export default Order;
