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, { useState, useEffect } from 'react';
import { Tabs, Table, Button, Result, Spin } from 'antd';
import { BizDetailLayout } from '@zgfe/business-lib';
import apis from '../../constants/dataoutputApi';
import request from '../../utils/ajax';
import './styles/index.less';
var DataPreviewTabs = function DataPreviewTabs(_ref) {
  var onBack = _ref.onBack,
    tableName = _ref.tableName;
  // const { tableName } = useContext(TaskContext);
  var classPrefix = 'cdp-dataOutput-table';
  var _useState = useState(true),
    _useState2 = _slicedToArray(_useState, 2),
    loading = _useState2[0],
    setLoading = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    failed = _useState4[0],
    setFailed = _useState4[1];
  var _useState5 = useState({
      columns: [],
      data: []
    }),
    _useState6 = _slicedToArray(_useState5, 2),
    dataSource = _useState6[0],
    setDataSource = _useState6[1];
  var _useState7 = useState([]),
    _useState8 = _slicedToArray(_useState7, 2),
    dataTable = _useState8[0],
    setDataTable = _useState8[1];
  useEffect(function () {
    query();
    queryTable();
  }, []);
  var tableColumns = [{
    title: '字段名称',
    dataIndex: 'name',
    width: 150
  }, {
    title: '字段类型',
    dataIndex: 'type',
    width: 150
  }, {
    title: '字段备注',
    dataIndex: 'comment',
    width: 150
  }];
  function query() {
    setLoading(true);
    setFailed(false);
    request(apis.queryTaskDataview, {
      method: 'get',
      params: {
        tableName: tableName
      }
    }).then(function (res) {
      if (!res || !res.data) {
        console.error('请求失败或数据无效');
        setDataSource({
          columns: [],
          data: []
        });
        return;
      }
      var rawData = res.data;
      // 检查 rawData 是否存在且不为空数组
      if (!Array.isArray(rawData) || rawData.length === 0) {
        console.warn('返回的数据为空');
        setDataSource({
          columns: [],
          data: []
        });
        return;
      }
      // 提取列头和数据行
      var headers = rawData[0];
      var dataRows = rawData.slice(1); // 显式定义 dataRows 为二维数组
      // 确保 headers 存在且为数组且不为空
      if (!Array.isArray(headers) || headers.length === 0 || !headers.every(function (header) {
        return typeof header === 'string';
      })) {
        console.error('无效的表头数据');
        setDataSource({
          columns: [],
          data: []
        });
        return;
      }
      // 转换列配置
      var columns = headers.map(function (header) {
        return {
          title: header,
          dataIndex: header,
          key: header,
          width: 150
        };
      });
      // 转换数据行
      var data = dataRows.map(function (row, index) {
        var item = {
          key: index.toString()
        };
        for (var i = 0; i < headers.length; i++) {
          item[headers[i]] = row[i] || '';
        }
        return item;
      });
      var resultData = {
        columns: columns,
        data: data
      };
      setDataSource(resultData);
    }).catch(function (err) {
      console.error('查询表结构失败', err);
      setFailed(true);
    }).finally(function () {
      setLoading(false);
    });
  }
  function queryTable() {
    setLoading(true);
    setFailed(false);
    request(apis.queryTasktableInfo, {
      method: 'get',
      params: {
        tableName: tableName
      }
    }).then(function (res) {
      if (!res || !res.data) return;
      setDataTable(res.data);
    }).catch(function (err) {
      console.error('查询表结构失败', err);
      setFailed(true);
    }).finally(function () {
      setLoading(false);
    });
  }
  return /*#__PURE__*/React.createElement(BizDetailLayout, {
    onBack: onBack,
    className: "".concat(classPrefix),
    width: "100%"
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-title")
  }, "\u6570\u636E\u96C6\u8868\u7ED3\u6784: ", tableName), /*#__PURE__*/React.createElement(Tabs, {
    defaultActiveKey: "1",
    style: {
      padding: '0 16px'
    }
  }, /*#__PURE__*/React.createElement(Tabs.TabPane, {
    tab: "\u6570\u636E\u9884\u89C8",
    key: "1"
  }, function () {
    if (loading) return /*#__PURE__*/React.createElement(Spin, {
      className: "".concat(classPrefix, "-loading")
    });
    if (failed) return /*#__PURE__*/React.createElement(Result, {
      status: "error",
      extra: /*#__PURE__*/React.createElement(Button, {
        type: "primary",
        onClick: query
      }, "\u91CD\u65B0\u52A0\u8F7D")
    });
    return /*#__PURE__*/React.createElement(Table, {
      columns: dataSource.columns,
      dataSource: dataSource.data,
      bordered: true,
      pagination: false,
      scroll: {
        y: 240,
        x: 200
      }
    });
  }()), /*#__PURE__*/React.createElement(Tabs.TabPane, {
    tab: "\u8868\u7ED3\u6784",
    key: "2"
  }, /*#__PURE__*/React.createElement(Table, {
    columns: tableColumns,
    dataSource: dataTable,
    bordered: true,
    pagination: false,
    scroll: {
      y: 240,
      x: 400
    }
  }))));
};
export default DataPreviewTabs;