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 { Checkbox, Table } from 'antd';
import React, { useContext, useEffect, useState } from 'react';
import apis from '../../constants/api';
import TaskContext from '../../modules/createJob/context';
import request from '../../utils/ajax';
import ColumnRuleList from './columnRuleList';
import './styles/columnSelect.less';
export var SimpleColumnTable = function SimpleColumnTable(props) {
  var readonly = props.readonly,
    columnList = props.columnList,
    chosenColumnList = props.chosenColumnList,
    className = props.className,
    hideSelector = props.hideSelector;
  var _useState = useState(chosenColumnList || []),
    _useState2 = _slicedToArray(_useState, 2),
    rowList = _useState2[0],
    setRowList = _useState2[1];
  useEffect(function () {
    if (props.onSelectColumn) props.onSelectColumn(rowList);
  }, [rowList]);
  var tableColumns = [{
    title: '字段名称',
    dataIndex: 'name',
    width: 150,
    render: function render(v, data) {
      return "".concat(v).concat(data.isPrimaryKey ? '(主键)' : '');
    }
  }, {
    title: '字段类型',
    dataIndex: 'webType',
    width: 150
  }, {
    title: '字段备注',
    dataIndex: 'comment'
  }];
  var rowSelection = {
    type: 'checkbox',
    hideSelectAll: readonly,
    defaultSelectedRowKeys: chosenColumnList ? chosenColumnList.map(function (column) {
      return column.name;
    }) : undefined,
    onChange: function onChange(selected, rows) {
      setRowList(rows);
    }
  };
  if (readonly) {
    rowSelection.renderCell = function (checked) {
      return /*#__PURE__*/React.createElement(Checkbox, {
        defaultChecked: checked,
        disabled: true
      });
    };
  }
  return /*#__PURE__*/React.createElement(Table, {
    dataSource: columnList,
    columns: tableColumns,
    rowSelection: hideSelector ? undefined : rowSelection,
    className: className,
    rowKey: "name",
    pagination: false,
    scroll: {
      y: 450
    }
  });
};
var CdpColumnSelect = function CdpColumnSelect(props) {
  var dataSourceId = props.dataSourceId,
    tableName = props.tableName,
    task = props.task;
  var _useContext = useContext(TaskContext),
    mode = _useContext.mode;
  var classPrefix = 'cdp-column-select';
  var _useState3 = useState([]),
    _useState4 = _slicedToArray(_useState3, 2),
    columnList = _useState4[0],
    setColumnList = _useState4[1];
  var _useState5 = useState([]),
    _useState6 = _slicedToArray(_useState5, 2),
    rules = _useState6[0],
    setRules = _useState6[1];
  var _useState7 = useState([]),
    _useState8 = _slicedToArray(_useState7, 2),
    rowList = _useState8[0],
    setRowList = _useState8[1];
  var _useState9 = useState(true),
    _useState10 = _slicedToArray(_useState9, 2),
    loading = _useState10[0],
    setLoading = _useState10[1];
  useEffect(function () {
    if (!dataSourceId || !tableName && props.dbType !== 'api') {
      setColumnList([]);
      setLoading(false);
      return;
    }
    setLoading(true);
    /**
     * 新建模式下清空选中列表；编辑模式下保持初始化状态不变
     */
    if (mode === 'edit') {
      setRowList(task.columns);
    } else {
      setRowList([]);
    }
    setColumnList([]);
    request(apis.queryTableColumns, {
      method: 'get',
      params: {
        id: dataSourceId,
        tableName: tableName
      }
    }).then(function (res) {
      if (!res) return;
      setColumnList(res.data);
    }).catch(function (err) {
      console.error('获取表结构失败', err);
    }).finally(function () {
      setLoading(false);
    });
  }, [dataSourceId, tableName, props.dbType]);
  useEffect(function () {
    if (props.onChange) props.onChange({
      columns: columnList.filter(function (item) {
        for (var i = 0; i < rowList.length; i++) {
          if (item.name === rowList[i].name) return true;
        }
        return false;
      }),
      columnRules: rules
    }, loading);
  }, [rowList, rules, loading]);
  return /*#__PURE__*/React.createElement("div", {
    className: classPrefix
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-title")
  }, "\u5B57\u6BB5\u5217\u8868", /*#__PURE__*/React.createElement("span", {
    className: "".concat(classPrefix, "-desc")
  }, loading ? '选择数据表后自动展示' : "\u5171\u8BA1".concat(columnList.length, "\u6761,\u5DF2\u9009\u62E9").concat(rowList.length, "\u6761"))), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-content")
  }, /*#__PURE__*/React.createElement(SimpleColumnTable, {
    readonly: mode === 'edit',
    columnList: columnList,
    className: "".concat(classPrefix, "-table"),
    chosenColumnList: task === null || task === void 0 ? void 0 : task.columns,
    onSelectColumn: setRowList
  }), /*#__PURE__*/React.createElement(ColumnRuleList, {
    loading: task && loading,
    columnList: columnList,
    rules: task === null || task === void 0 ? void 0 : task.columnRules,
    dbType: props.dbType,
    onChange: setRules,
    onFail: props.onFail
  })));
};
export default CdpColumnSelect;