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 { Drawer, Space, Table, Empty } from 'antd';
import React, { useContext, useEffect } from 'react';
import moment from 'moment';
import { BizGlobalDataContext, IconFont, ajax } from '@zgfe/business-lib';
import './styles/index.less';
import CdpCheckTable from '../checkTable';
import DatasetRelationMap from './relationMap';
import CheckRelationListModal from './checkRelationList';
import EffectDatasetDialog from './effectDialog';
import apis from '../../constants/api';
export var classPrefix = 'dataset-manage';
var CdpDatasetManage = function CdpDatasetManage(_ref) {
  var jobCallback = _ref.jobCallback;
  var _React$useState = React.useState([]),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    data = _React$useState2[0],
    setData = _React$useState2[1];
  var _React$useState3 = React.useState(''),
    _React$useState4 = _slicedToArray(_React$useState3, 2),
    checkTableName = _React$useState4[0],
    setCheckTableName = _React$useState4[1];
  // 查看数据血缘
  var _React$useState5 = React.useState(),
    _React$useState6 = _slicedToArray(_React$useState5, 2),
    checkRelation = _React$useState6[0],
    setCheckRelation = _React$useState6[1];
  // 查看关联任务
  var _React$useState7 = React.useState(),
    _React$useState8 = _slicedToArray(_React$useState7, 2),
    checkRelationList = _React$useState8[0],
    setCheckRelationList = _React$useState8[1];
  var _React$useState9 = React.useState(),
    _React$useState10 = _slicedToArray(_React$useState9, 2),
    toDel = _React$useState10[0],
    setToDel = _React$useState10[1];
  var _React$useState11 = React.useState(false),
    _React$useState12 = _slicedToArray(_React$useState11, 2),
    loading = _React$useState12[0],
    setLoading = _React$useState12[1];
  var _useContext = useContext(BizGlobalDataContext),
    currentProject = _useContext.currentProject,
    authority = _useContext.authority;
  var columns = [{
    title: '数据集名称',
    dataIndex: 'name',
    key: 'name'
  }, {
    title: '表名',
    dataIndex: 'tableName',
    key: 'tableName'
  }, {
    title: '创建人',
    dataIndex: 'creator',
    key: 'creator'
  }, {
    title: '创建时间',
    dataIndex: 'createTime',
    key: 'createTime',
    // 支持排序
    sorter: function sorter(a, b) {
      return moment(a.createTime).valueOf() - moment(b.createTime).valueOf();
    },
    defaultSortOrder: 'descend',
    render: function render(value) {
      return moment(value).format('YYYY/MM/DD HH:mm:ss');
    }
  }, {
    title: '关联任务',
    dataIndex: 'jobCount',
    key: 'jobCount',
    render: function render(value, record) {
      return /*#__PURE__*/React.createElement(Space, {
        align: "baseline",
        className: "".concat(classPrefix, "-btn"),
        onClick: setCheckRelationList.bind(undefined, record)
      }, /*#__PURE__*/React.createElement(IconFont, {
        type: "maidianfanganguanli"
      }), value);
    }
  }, {
    title: '数据血缘',
    dataIndex: 'id',
    key: 'id',
    render: function render(value, record) {
      return /*#__PURE__*/React.createElement(Space, {
        align: "baseline",
        className: "".concat(classPrefix, "-btn"),
        onClick: function onClick() {
          setCheckRelation(record);
        }
      }, /*#__PURE__*/React.createElement(IconFont, {
        type: "a-yanjingkai"
      }), "\u67E5\u770B");
    }
  }, {
    title: '表结构',
    dataIndex: 'tableName',
    key: 'tableName',
    render: function render(value) {
      return /*#__PURE__*/React.createElement(Space, {
        align: "baseline",
        className: "".concat(classPrefix, "-btn"),
        onClick: function onClick() {
          setCheckTableName(value);
        }
      }, /*#__PURE__*/React.createElement(IconFont, {
        type: "a-yanjingkai"
      }), "\u67E5\u770B");
    }
  }, {
    title: '操作',
    dataIndex: 'id',
    key: 'id',
    fixed: 'right',
    render: function render(value, record) {
      return authority[1706261686] ? /*#__PURE__*/React.createElement("a", {
        className: "".concat(classPrefix, "-btn-del"),
        onClick: setToDel.bind(undefined, record)
      }, "\u5220\u9664") : '-';
    }
  }];
  useEffect(function () {
    if (authority[1706261697]) {
      queryList();
    }
  }, [authority]);
  function queryList() {
    setLoading(true);
    ajax(apis.queryDatasetList, {
      method: 'get',
      params: {
        projectId: currentProject === null || currentProject === void 0 ? void 0 : currentProject.projectId
      }
    }).then(function (res) {
      if (!res) return;
      setData(res.data);
    }).catch(function (err) {
      console.error('查询数据集列表失败', err);
    }).finally(function () {
      setLoading(false);
    });
  }
  function onDel(success) {
    ajax(apis.delDataset, {
      method: 'get',
      params: {
        datasetId: toDel === null || toDel === void 0 ? void 0 : toDel.id
      }
    }).then(function (res) {
      if (!res) return;
      success();
      setData(function (pre) {
        return pre.filter(function (item) {
          return item.id !== (toDel === null || toDel === void 0 ? void 0 : toDel.id);
        });
      });
    }).catch(function (err) {
      console.error('删除数据集失败', err);
    }).finally(function () {
      setToDel(undefined);
    });
  }
  return /*#__PURE__*/React.createElement("div", {
    className: classPrefix
  }, function () {
    if (!authority[1706261697]) {
      return /*#__PURE__*/React.createElement(Empty, {
        image: Empty.PRESENTED_IMAGE_SIMPLE,
        description: "\u6CA1\u6709\u5217\u8868\u67E5\u8BE2\u6743\u9650"
      });
    }
    return /*#__PURE__*/React.createElement(Table, {
      rowKey: "id",
      columns: columns,
      dataSource: data,
      className: "".concat(classPrefix, "-list"),
      scroll: {
        x: true
      },
      loading: loading
    });
  }(), checkTableName && /*#__PURE__*/React.createElement(Drawer, {
    className: "".concat(classPrefix, "-table"),
    width: 1220,
    open: true,
    title: "\u67E5\u770B\u8868\u7ED3\u6784",
    maskStyle: {
      background: 'rgba(0,0,0,0.8)'
    },
    onClose: setCheckTableName.bind(undefined, '')
  }, /*#__PURE__*/React.createElement(CdpCheckTable, {
    tableName: checkTableName
  })), checkRelation && /*#__PURE__*/React.createElement(DatasetRelationMap, {
    datasetId: checkRelation.id,
    onClose: function onClose() {
      setCheckRelation(undefined);
    }
  }), checkRelationList && /*#__PURE__*/React.createElement(CheckRelationListModal, {
    datasetId: checkRelationList.id,
    onClose: setCheckRelationList.bind(undefined, undefined),
    onCheckDesc: jobCallback === null || jobCallback === void 0 ? void 0 : jobCallback.onCheckDesc,
    onEdit: jobCallback === null || jobCallback === void 0 ? void 0 : jobCallback.onEdit
  }), toDel && /*#__PURE__*/React.createElement(EffectDatasetDialog, {
    mode: "del",
    datasetId: toDel.id,
    onCancel: setToDel.bind(undefined, undefined),
    onOk: onDel
  }));
};
export default CdpDatasetManage;