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 from 'react';
import { BizGlobalDataContext, util } from '@zgfe/business-lib';
import { Drawer, Table, Button, Modal } from 'antd';
import CheckTaskLog from '../../components/checkLog';
import apis from '../../constants/dataoutputApi';
import request from '../../utils/ajax';
import './styles/index.less';
// 状态枚举
var StatusEnum;
(function (StatusEnum) {
  StatusEnum["RUNNING_EXECUTION"] = "RUNNING_EXECUTION";
  StatusEnum["FAILURE"] = "FAILURE";
  StatusEnum["SUCCESS"] = "SUCCESS";
})(StatusEnum || (StatusEnum = {}));
var StatusDrawer = function StatusDrawer(_ref) {
  var visible = _ref.visible,
    onClose = _ref.onClose,
    dataSource = _ref.dataSource,
    currentTask = _ref.currentTask;
  var _React$useState = React.useState(false),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    viewLogVisible = _React$useState2[0],
    setViewLogVisible = _React$useState2[1];
  var _React$useState3 = React.useState(''),
    _React$useState4 = _slicedToArray(_React$useState3, 2),
    currentLog = _React$useState4[0],
    setCurrentLog = _React$useState4[1];
  var _React$useState5 = React.useState(false),
    _React$useState6 = _slicedToArray(_React$useState5, 2),
    showLog = _React$useState6[0],
    setShowLog = _React$useState6[1];
  var _React$useContext = React.useContext(BizGlobalDataContext),
    currentApp = _React$useContext.currentApp;
  var _React$useState7 = React.useState(10),
    _React$useState8 = _slicedToArray(_React$useState7, 2),
    pageSize = _React$useState8[0],
    setPageSize = _React$useState8[1];
  var _React$useState9 = React.useState(1),
    _React$useState10 = _slicedToArray(_React$useState9, 2),
    pageNum = _React$useState10[0],
    setPageNum = _React$useState10[1];
  var _React$useState11 = React.useState(0),
    _React$useState12 = _slicedToArray(_React$useState11, 2),
    count = _React$useState12[0],
    setCount = _React$useState12[1];
  var _React$useState13 = React.useState(true),
    _React$useState14 = _slicedToArray(_React$useState13, 2),
    loading = _React$useState14[0],
    setLoading = _React$useState14[1];
  var _React$useState15 = React.useState([]),
    _React$useState16 = _slicedToArray(_React$useState15, 2),
    dataSelist = _React$useState16[0],
    setDatasetList = _React$useState16[1]; // 修改此处
  var classPrefix = 'dataset-list';
  var LogView = function LogView(_ref2) {
    var log = _ref2.log;
    function logFormat(log) {
      if (!log) return '暂无日志';
      return log.split(/\n/g).map(function (line, i) {
        return /*#__PURE__*/React.createElement("div", {
          className: "cdp-task-log-line",
          key: i
        }, util.setHighLight(line, /\t/g, function (val, key) {
          return /*#__PURE__*/React.createElement("span", {
            className: "tab",
            key: key
          });
        }));
      });
    }
    return /*#__PURE__*/React.createElement("div", {
      className: "cdp-task-log"
    }, logFormat(log));
  };
  function queryProcessList() {
    setLoading(true);
    request(apis.processList, {
      method: 'get',
      params: {
        taskId: currentTask.taskId,
        pageSize: pageSize,
        pageNo: pageNum
      }
    }).then(function (res) {
      var _res$data, _res$data$data;
      if (!res || !res.data || !Array.isArray(res.data.data)) return;
      var _ref3 = res.data || {},
        data = _ref3.data,
        total = _ref3.total;
      var processedData = (_res$data = res.data) === null || _res$data === void 0 ? void 0 : (_res$data$data = _res$data.data) === null || _res$data$data === void 0 ? void 0 : _res$data$data.map(function (item) {
        return {
          id: item.id,
          endTime: item.endTime,
          state: item.state,
          log: item.log || '',
          isRerun: item.isRerun || 0
        };
      });
      setDatasetList(processedData || []);
      setCount(total !== null && total !== void 0 ? total : 0);
    }).catch(function (err) {
      console.error('获取数据集列表失败', err);
    }).finally(function () {
      setLoading(false);
    });
  }
  function queryProcessLog(logId) {
    request(apis.processLog, {
      method: 'get',
      params: {
        taskId: currentTask.taskId,
        logId: logId
      }
    }).then(function (res) {
      if (!res) throw new Error();
      setCurrentLog(res.data);
    }).catch(function (err) {
      console.error('获取日志失败', err);
      setCurrentLog('获取日志失败，请稍后再试');
    });
  }
  function queryReturnLog(logId) {
    request(apis.returnLog, {
      method: 'get',
      params: {
        taskId: currentTask.taskId,
        logId: logId
      }
    }).then(function (res) {
      if (!res) throw new Error();
      setCurrentLog(res.data);
      queryProcessList();
    }).catch(function (err) {
      console.error('获取日志失败', err);
      setCurrentLog('获取日志失败，请稍后再试');
    });
  }
  // 查看日志
  var handleViewLog = function handleViewLog(record) {
    queryProcessLog(Number(record.id));
    setViewLogVisible(true);
  };
  // 重跑任务
  var handleRerun = function handleRerun(record) {
    queryReturnLog(Number(record.id));
  };
  React.useEffect(function () {
    if (visible) {
      // 当抽屉可见时调用 queryProcessList
      queryProcessList();
    }
  }, [visible, pageNum, pageSize]);
  var columns = [{
    title: '更新完成时间',
    dataIndex: 'endTime',
    key: 'endTime'
  }, {
    title: '状态',
    dataIndex: 'state',
    key: 'state',
    render: function render(v) {
      switch (v) {
        case StatusEnum.FAILURE:
          return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", {
            className: "".concat(classPrefix, "-state fail")
          }), "\u5931\u8D25");
        case StatusEnum.SUCCESS:
          return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", {
            className: "".concat(classPrefix, "-state Success")
          }), "\u6210\u529F");
        case StatusEnum.RUNNING_EXECUTION:
          return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", {
            className: "".concat(classPrefix, "-state Updating")
          }), "\u8FD0\u884C\u4E2D");
        default:
          return /*#__PURE__*/React.createElement(React.Fragment, null, "-");
      }
    }
  }, {
    title: '操作',
    key: 'action',
    render: function render(_, record) {
      return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Button, {
        type: "link",
        onClick: function onClick() {
          return handleViewLog(record);
        }
      }, "\u67E5\u770B\u65E5\u5FD7"), record.isRerun === 1 && /*#__PURE__*/React.createElement(Button, {
        type: "link",
        danger: true,
        onClick: function onClick() {
          return handleRerun(record);
        }
      }, "\u91CD\u8DD1"));
    }
  }];
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Drawer, {
    title: "\u4EFB\u52A1\u8BE6\u60C5",
    width: 800,
    onClose: onClose,
    open: visible,
    destroyOnClose: true
  }, /*#__PURE__*/React.createElement(Table, {
    rowKey: "id",
    loading: loading,
    columns: columns,
    dataSource: dataSelist,
    pagination: {
      pageSize: pageSize,
      total: count,
      current: pageNum,
      showTotal: function showTotal(total) {
        return /*#__PURE__*/React.createElement(React.Fragment, null, "\u5171", /*#__PURE__*/React.createElement("span", {
          className: "".concat(classPrefix, "-total")
        }, total), "\u6761");
      },
      onChange: function onChange(page, pageSize) {
        setPageNum(page);
        setPageSize(pageSize);
      }
    }
  })), currentTask && showLog ? /*#__PURE__*/React.createElement(CheckTaskLog, {
    taskId: currentTask.id,
    onClose: function onClose() {
      setShowLog(false);
    }
  }) : null, /*#__PURE__*/React.createElement(Modal, {
    title: "\u67E5\u770B\u65E5\u5FD7",
    open: viewLogVisible,
    onCancel: function onCancel() {
      return setViewLogVisible(false);
    },
    footer: null,
    destroyOnClose: true
  }, /*#__PURE__*/React.createElement("pre", {
    style: {
      whiteSpace: 'pre-wrap'
    }
  }, ' ', /*#__PURE__*/React.createElement(LogView, {
    log: currentLog
  }))));
};
export default StatusDrawer;