function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
import React, { useContext, useEffect, useState } from 'react';
import './style/index.less';
import { BizGlobalDataContext, BizSearchInput, BizTable, IconFont, ajax } from '@zgfe/business-lib';
import { Button, message, Empty } from 'antd';
import apis from '../../constants/api';
import { useColumns } 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 _useContext = useContext(BizGlobalDataContext),
    currentApp = _useContext.currentApp,
    authority = _useContext.authority,
    basicConfig = _useContext.basicConfig;
  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([]),
    _useState0 = _slicedToArray(_useState9, 2),
    taskList = _useState0[0],
    setTaskList = _useState0[1];
  // 当前任务
  var _useState1 = useState(),
    _useState10 = _slicedToArray(_useState1, 2),
    currentTask = _useState10[0],
    setCurrentTask = _useState10[1];
  var _useState11 = useState(0),
    _useState12 = _slicedToArray(_useState11, 2),
    timer = _useState12[0],
    setTimer = _useState12[1];
  var _useState13 = useState(false),
    _useState14 = _slicedToArray(_useState13, 2),
    open = _useState14[0],
    setOpen = _useState14[1];
  useEffect(function () {
    if (timer) {
      clearTimeout(timer);
    }
    if (authority[1706261698]) {
      setTimer(setTimeout(function () {
        fetch();
      }, 400));
    }
  }, [tableSearch, pageData.current, pageData.pageSize, authority]);
  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();
    });
  };
  var _useColumns = useColumns(authority, onChangeParams, onChangeStatus, onDelete, onCheckLog, onUpdate, onEdit),
    columns = _useColumns.columns;
  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\u6570\u636E\u96C6\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[1706261688],
    onClick: onCreate,
    icon: /*#__PURE__*/React.createElement(IconFont, {
      type: "tianjia1",
      size: 16
    })
  }, "\u521B\u5EFA\u4EFB\u52A1")), function () {
    if (!authority[1706261698]) {
      return /*#__PURE__*/React.createElement(Empty, {
        image: Empty.PRESENTED_IMAGE_SIMPLE,
        description: "\u6CA1\u6709\u5217\u8868\u67E5\u8BE2\u6743\u9650"
      });
    }
    return /*#__PURE__*/React.createElement(BizTable, {
      rowKey: "id",
      loading: loading,
      dataSource: taskList,
      columns: columns,
      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;