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 { BizDialog, BizGlobalDataContext, BizTable, IconFont, ajax } from '@zgfe/business-lib';
import { Button, Drawer, Spin, message } from 'antd';
import React, { useContext, useEffect, useState } from 'react';
import './index.less';
import apis from '../../../../constants/api';
import { statusNode } from '../../utils';
import { LogView } from '../../../../components/checkLog';
import { ConfirmTip } from '../confirm';
var RunningLog = function RunningLog(props) {
  var classPrefix = 'running-log';
  var logType = props.logType;
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    logList = _useState2[0],
    setLogList = _useState2[1];
  var _useState3 = useState(),
    _useState4 = _slicedToArray(_useState3, 2),
    currentLog = _useState4[0],
    setCurrentLog = _useState4[1];
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    loading = _useState6[0],
    setLoading = _useState6[1];
  var _useState7 = useState(false),
    _useState8 = _slicedToArray(_useState7, 2),
    logLoading = _useState8[0],
    setLogLoading = _useState8[1];
  var _useState9 = useState(false),
    _useState0 = _slicedToArray(_useState9, 2),
    showDetail = _useState0[0],
    setShowDetail = _useState0[1];
  var _useContext = useContext(BizGlobalDataContext),
    currentApp = _useContext.currentApp,
    _useContext$authority = _useContext.authority,
    authority = _useContext$authority === void 0 ? {} : _useContext$authority;
  var columns = [{
    title: '更新完成时间',
    dataIndex: 'endTime',
    width: 200,
    ellipsis: true,
    render: function render(text) {
      return /*#__PURE__*/React.createElement("div", null, text ? text : '-');
    }
  }, {
    title: '状态',
    dataIndex: 'state',
    render: function render(v) {
      return statusNode(v);
    }
  }, {
    title: '操作',
    width: 150,
    render: function render(_, record) {
      return /*#__PURE__*/React.createElement("div", {
        className: "".concat(classPrefix, "-options")
      }, /*#__PURE__*/React.createElement("a", {
        className: (authority[1706261693] || logType === 'datasetLogs') && record.state !== 'RUNNING_EXECUTION' ? '' : 'disabled',
        onClick: function onClick() {
          if (!authority[1706261693] && logType !== 'datasetLogs' || record.state === 'RUNNING_EXECUTION') return;
          fetchLog(record.taskInstanceId);
          setShowDetail(true);
        }
      }, "\u65E5\u5FD7"), logType !== 'datasetLogs' && record.state === 'FAILURE' ? (/*#__PURE__*/React.createElement("a", {
        className: "".concat(classPrefix, "-options-fail"),
        onClick: function onClick() {
          return onRerun(record.id);
        }
      }, "\u5931\u8D25\u91CD\u8DD1")) : null);
    }
  }];
  useEffect(function () {
    if (!props.taskId || !props.open) return;
    fetch();
  }, [props.taskId, props.open]);
  var onRerun = function onRerun(id) {
    ConfirmTip({
      title: '确认提示',
      content: '确认发起失败重跑吗？'
    }, function () {
      setLoading(true);
      ajax(apis.rerunFusion, {
        method: 'post',
        data: {
          id: id,
          taskId: props.taskId,
          appId: currentApp === null || currentApp === void 0 ? void 0 : currentApp.appId
        }
      }).then(function (res) {
        if (!res) throw new Error();
        message.success('重跑成功');
        // 点击重跑，把这条数据立即改为“执行中”
        var _logList = logList.filter(function (item) {
          if (item.id === id) {
            item.state = 'RUNNING_EXECUTION';
          }
          return item;
        });
        setLogList(_logList);
        setLoading(false);
      }).catch(function () {
        setLoading(false);
      });
    });
  };
  var fetch = function fetch() {
    setLoading(true);
    ajax(logType === 'datasetLogs' ? apis.datasetLogs : apis.fusionLogs, {
      method: 'post',
      data: {
        appId: currentApp === null || currentApp === void 0 ? void 0 : currentApp.appId,
        id: props.taskId
      }
    }).then(function (res) {
      if (!res || !res.data) return;
      setLogList(res.data);
    }).finally(function () {
      setLoading(false);
    });
  };
  // 获取日志详情
  var fetchLog = function fetchLog(logId) {
    setLogLoading(true);
    setCurrentLog('');
    ajax(logType === 'datasetLogs' ? apis.datasetLogDetail : apis.fusionLogDetail, {
      method: 'post',
      data: {
        appId: currentApp === null || currentApp === void 0 ? void 0 : currentApp.appId,
        logId: logId,
        id: props.taskId
      }
    }).then(function (res) {
      if (!res || !res.data) return;
      var msg = res.data.message;
      setCurrentLog(msg);
    }).finally(function () {
      setLogLoading(false);
    });
  };
  return /*#__PURE__*/React.createElement(Drawer, {
    className: classPrefix,
    open: props.open,
    width: 520,
    maskClassName: "".concat(classPrefix, "-mask"),
    title: /*#__PURE__*/React.createElement("div", {
      className: "".concat(classPrefix, "-title-panel")
    }, /*#__PURE__*/React.createElement("div", {
      className: "".concat(classPrefix, "-title")
    }, "\u8FD0\u884C\u65E5\u5FD7"), /*#__PURE__*/React.createElement(IconFont, {
      type: "qingchu",
      className: "".concat(classPrefix, "-close"),
      onClick: props.onClose
    })),
    onClose: props.onClose
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-content")
  }, /*#__PURE__*/React.createElement(BizTable, {
    rowKey: "id",
    loading: loading,
    dataSource: logList,
    columns: columns
  })), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-footer")
  }, /*#__PURE__*/React.createElement(Button, {
    type: "primary",
    onClick: props.onClose
  }, "\u786E\u5B9A")), /*#__PURE__*/React.createElement(BizDialog, {
    className: "".concat(classPrefix, "-detail"),
    title: "\u67E5\u770B\u65E5\u5FD7",
    open: showDetail,
    width: 720,
    closable: true,
    footer: null,
    onCancel: function onCancel() {
      return setShowDetail(false);
    }
  }, function () {
    if (logLoading) {
      return /*#__PURE__*/React.createElement(Spin, {
        className: "".concat(classPrefix, "-detail-loading")
      });
    }
    return /*#__PURE__*/React.createElement(LogView, {
      log: currentLog
    });
  }()));
};
export default RunningLog;