function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useContext, useEffect, useState } from 'react';
import './style/index.less';
import { BizGlobalDataContext, BizSearchInput, BizTable, IconFont, ajax } from '@zgfe/business-lib';
import { Button, message } from 'antd';
import apis from '../../constants/api';
import { getColumns } from './utils';
import RunningLog from './components/log';
import { ConfirmTip } from './components/confirm';
import _ from 'lodash';
export var classPrefix = 'data-fusion';
var DataFusion = function DataFusion(_ref) {
  var onCreate = _ref.onCreate,
    onEdit = _ref.onEdit;
  var _useState = useState({}),
    _useState2 = _slicedToArray(_useState, 2),
    tableSearch = _useState2[0],
    setTableSearch = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    loading = _useState4[0],
    setLoading = _useState4[1];
  var _useState5 = useState(0),
    _useState6 = _slicedToArray(_useState5, 2),
    total = _useState6[0],
    setTotal = _useState6[1];
  var _useState7 = useState({
      current: 1,
      pageSize: 10
    }),
    _useState8 = _slicedToArray(_useState7, 2),
    pageData = _useState8[0],
    setPageData = _useState8[1];
  var _useState9 = useState([]),
    _useState10 = _slicedToArray(_useState9, 2),
    taskList = _useState10[0],
    setTaskList = _useState10[1];
  // 当前任务
  var _useState11 = useState(),
    _useState12 = _slicedToArray(_useState11, 2),
    currentTask = _useState12[0],
    setCurrentTask = _useState12[1];
  var _useState13 = useState(0),
    _useState14 = _slicedToArray(_useState13, 2),
    timer = _useState14[0],
    setTimer = _useState14[1];
  var _useState15 = useState(false),
    _useState16 = _slicedToArray(_useState15, 2),
    open = _useState16[0],
    setOpen = _useState16[1];
  var _useContext = useContext(BizGlobalDataContext),
    currentApp = _useContext.currentApp,
    authority = _useContext.authority;
  useEffect(function () {
    if (timer) {
      clearTimeout(timer);
    }
    setTimer(setTimeout(function () {
      fetch();
    }, 400));
  }, [tableSearch, pageData.current, pageData.pageSize]);
  var fetch = function fetch() {
    setLoading(true);
    var params = _.cloneDeep(tableSearch);
    if (params.datasetType === 0) {
      delete params.datasetType;
    }
    if (params.state === 'all') {
      delete params.state;
    }
    ajax(apis.queryFusionList, {
      method: 'post',
      data: _objectSpread({
        appId: currentApp === null || currentApp === void 0 ? void 0 : currentApp.appId,
        platform: (currentApp === null || currentApp === void 0 ? void 0 : currentApp.platform) || 0,
        pageNo: pageData.current,
        pageSize: pageData.pageSize
      }, params)
    }).then(function (res) {
      if (!res || !res.data) {
        setTaskList([]);
        setTotal(0);
        return;
      }
      var data = res.data;
      setTaskList(data.results);
      setTotal(data.total);
    }).finally(function () {
      setLoading(false);
    });
  };
  // 监听分页变化
  var onChangeTable = function onChangeTable(pagination) {
    setPageData({
      pageSize: pagination.pageSize,
      current: pagination.current
    });
  };
  // 改变启用状态
  var onChangeStatus = function onChangeStatus(id, status) {
    setTaskList(function (list) {
      return list.map(function (item) {
        if (item.id === id) {
          item.status = status;
        }
        return _objectSpread({}, item);
      });
    });
  };
  // 点击删除
  var onDelete = function onDelete(data) {
    ConfirmTip({
      title: '风险提示',
      content: '删除后任务将不可恢复'
    }, function () {
      setLoading(true);
      ajax(apis.removeJob, {
        method: 'post',
        data: {
          id: data === null || data === void 0 ? void 0 : data.id,
          appId: currentApp === null || currentApp === void 0 ? void 0 : currentApp.appId
        }
      }).then(function (res) {
        if (!res) throw new Error();
        message.success('删除成功');
        fetch();
      }).catch(function () {
        setLoading(false);
      });
    });
  };
  // 查看日志
  var onCheckLog = function onCheckLog(data) {
    setCurrentTask(data);
    setOpen(true);
  };
  // 数据集类型/状态改变
  var onChangeParams = function onChangeParams(data, type) {
    setTableSearch(function (search) {
      var searchData = _.cloneDeep(search);
      searchData[type] = data;
      return searchData;
    });
  };
  // 手动更新
  var onUpdate = function onUpdate(id) {
    setLoading(true);
    ajax(apis.updateFusion, {
      method: 'post',
      data: {
        appId: currentApp === null || currentApp === void 0 ? void 0 : currentApp.appId,
        platform: (currentApp === null || currentApp === void 0 ? void 0 : currentApp.platform) || 0,
        id: id
      }
    }).then(function (res) {
      if (!res) {
        setLoading(false);
        return;
      }
      fetch();
    });
  };
  return /*#__PURE__*/React.createElement("div", {
    className: classPrefix
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-panel"),
    id: "sessionManageTable"
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-panel-search")
  }, /*#__PURE__*/React.createElement(BizSearchInput, {
    placeholder: "\u8BF7\u8F93\u5165\u4EFB\u52A1\u540D\u79F0",
    onChange: function onChange(txt) {
      return onChangeParams(txt, 'datasetName');
    }
  }), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-panel-search-txt")
  }, "\u5171\u641C\u7D22\u5230", /*#__PURE__*/React.createElement("span", {
    className: "".concat(classPrefix, "-panel-search-num")
  }, total), "\u6761\u6570\u636E")), /*#__PURE__*/React.createElement(Button, {
    type: "primary",
    disabled: !authority[1705040889],
    onClick: onCreate,
    icon: /*#__PURE__*/React.createElement(IconFont, {
      type: "tianjia1",
      size: 16
    })
  }, "\u521B\u5EFA\u4EFB\u52A1")), /*#__PURE__*/React.createElement(BizTable, {
    rowKey: "id",
    loading: loading,
    dataSource: taskList,
    columns: getColumns(authority, onChangeParams, onChangeStatus, onDelete, onCheckLog, onUpdate, onEdit),
    total: total,
    onChange: onChangeTable,
    scroll: {
      x: 800
    }
  }), /*#__PURE__*/React.createElement(RunningLog, {
    open: open,
    taskId: currentTask === null || currentTask === void 0 ? void 0 : currentTask.id,
    onClose: function onClose() {
      return setOpen(false);
    }
  }));
};
export default DataFusion;