function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var _excluded = ["columns", "search", "fieldNames", "placeholder", "border", "prefixIcon", "addonBefore", "addonAfter", "request", "values", "setValues", "setting", "refresh", "topNode", "defaultAfterChange", "defaultSelectKey", "pagination"],
  _excluded2 = ["onChange", "className", "plain"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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(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(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import { Button, Checkbox, Modal, Space, Table } from 'antd';
import React, { useEffect, useMemo, useState } from 'react';
import CombineSearch from "../CombineSearch";
import Icon from "../Icon";
import RenderContent from "./components/RenderContent";
import { useCombinTableStyles } from "./styles";
var DEFAULT_PAGINATION = {
  hideOnSinglePage: false,
  showSizeChanger: true,
  showQuickJumper: true,
  pageSize: 10
};
var CombinTable = function CombinTable(props) {
  var columns = props.columns,
    _props$search = props.search,
    search = _props$search === void 0 ? true : _props$search,
    _props$fieldNames = props.fieldNames,
    fieldNames = _props$fieldNames === void 0 ? {
      label: 'title',
      value: 'key'
    } : _props$fieldNames,
    placeholder = props.placeholder,
    border = props.border,
    prefixIcon = props.prefixIcon,
    addonBefore = props.addonBefore,
    addonAfter = props.addonAfter,
    request = props.request,
    values = props.values,
    setValues = props.setValues,
    _props$setting = props.setting,
    setting = _props$setting === void 0 ? true : _props$setting,
    _props$refresh = props.refresh,
    refresh = _props$refresh === void 0 ? true : _props$refresh,
    topNode = props.topNode,
    defaultAfterChange = props.defaultAfterChange,
    defaultSelectKey = props.defaultSelectKey,
    paginationProp = props.pagination,
    tableProps = _objectWithoutProperties(props, _excluded);
  var _useCombinTableStyles = useCombinTableStyles(),
    styles = _useCombinTableStyles.styles,
    cx = _useCombinTableStyles.cx;
  var SearchProps = {
    fieldNames: fieldNames,
    placeholder: placeholder,
    border: border,
    prefixIcon: prefixIcon,
    addonBefore: addonBefore,
    addonAfter: addonAfter,
    request: request,
    values: values,
    setValues: setValues,
    defaultAfterChange: defaultAfterChange,
    defaultSelectKey: defaultSelectKey,
    options: columns ? columns : []
  };
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    selectValues = _useState2[0],
    setSelectValues = _useState2[1];
  var _useState3 = useState([]),
    _useState4 = _slicedToArray(_useState3, 2),
    checkboxOptions = _useState4[0],
    setCheckOptions = _useState4[1];
  var _useState5 = useState([]),
    _useState6 = _slicedToArray(_useState5, 2),
    checkValues = _useState6[0],
    setCheckValues = _useState6[1];
  var _useState7 = useState([]),
    _useState8 = _slicedToArray(_useState7, 2),
    selectShowValues = _useState8[0],
    setSelectShowValues = _useState8[1];
  var _useState9 = useState(false),
    _useState10 = _slicedToArray(_useState9, 2),
    showSetting = _useState10[0],
    setShowSetting = _useState10[1];
  //  筛选表头checkbox
  var handleCheckChange = function handleCheckChange(e) {
    setSelectValues(e);
  };
  var handleSelectColumns = function handleSelectColumns() {
    setCheckValues(selectValues);
    setSelectShowValues(selectValues);
    setSelectValues([]);
    setShowSetting(false);
  };
  var resetColumns = useMemo(function () {
    var columsList = [];
    var selecting = [];
    var list = selectShowValues.length ? selectShowValues : [];
    columns === null || columns === void 0 || columns.forEach(function (column) {
      var resetColumns = _objectSpread(_objectSpread({}, column), {}, {
        render: function render(col, record) {
          if (column.dataIndex || column.render) {
            if (column.render) {
              return column.render(col, record, arguments.length <= 2 ? undefined : arguments[2]);
            }
            if (column.dataIndex && record[column.dataIndex]) {
              return record[column.dataIndex];
            }
          }
          return '-';
        }
      });
      if (column.key) {
        if (list.findIndex(function (i) {
          return i === column.key;
        }) === -1 && selectShowValues.length) {} else {
          columsList.push(resetColumns);
          selecting.push(column.key);
        }
      } else {
        if (column.render) {
          columsList.push(resetColumns);
        }
      }
    });
    setCheckValues(selecting);
    if (!search) {
      return columsList;
    }
    return columsList === null || columsList === void 0 ? void 0 : columsList.map(function (item) {
      switch (item.componentType) {
        case 'Select':
        case 'StatusSelect':
        case 'Checkbox':
          return _objectSpread(_objectSpread({}, item), {}, {
            title: /*#__PURE__*/React.createElement(Space, {
              align: "center"
            }, item.title, ' ', /*#__PURE__*/React.createElement(RenderContent, {
              current: item,
              fieldKey: item[fieldNames === null || fieldNames === void 0 ? void 0 : fieldNames.value],
              values: values,
              setValues: setValues
            }))
          });
        default:
          return _objectSpread({}, item);
      }
    });
  }, [search, columns, JSON.stringify(values), selectShowValues]);
  var AfterBaseDom = useMemo(function () {
    var doms = [];
    if (setting) {
      doms.push( /*#__PURE__*/React.createElement(Button, {
        type: "text",
        onClick: function onClick() {
          setShowSetting(true);
        },
        icon: /*#__PURE__*/React.createElement(Icon, {
          name: "v35_Settings"
        })
      }));
    }
    if (refresh) {
      doms.push( /*#__PURE__*/React.createElement(Button, {
        type: "text",
        onClick: function onClick() {
          if (request) {
            request(values, '', '$refresh');
          }
        },
        icon: /*#__PURE__*/React.createElement(Icon, {
          name: "v35_Refresh"
        })
      }));
    }
    return doms;
  }, [setting, refresh, values, search, request]);
  var paginationConfig = useMemo(function () {
    if (paginationProp === false) {
      return false;
    }
    var userPagination = paginationProp === undefined ? DEFAULT_PAGINATION : paginationProp;
    var userOnChange = userPagination.onChange,
      className = userPagination.className,
      plain = userPagination.plain,
      restPagination = _objectWithoutProperties(userPagination, _excluded2);
    return _objectSpread(_objectSpread({}, restPagination), {}, {
      className: cx(!plain && styles.paginationHighlight, className),
      onChange: function onChange(page, size) {
        userOnChange === null || userOnChange === void 0 || userOnChange(page, size, values);
      }
    });
  }, [paginationProp, values, styles.paginationHighlight, cx]);

  // 获取表头筛选项
  useEffect(function () {
    var checkOptions = [];
    var selecting = [];
    columns === null || columns === void 0 || columns.forEach(function (i) {
      if (i.key) {
        checkOptions.push({
          label: i.title,
          value: i.key,
          disabled: i.headerDisabled ? true : false
        });
        selecting.push(i.key);
      }
    });
    setCheckValues(selecting);
    setCheckOptions(checkOptions);
  }, [columns]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(CombineSearch, _extends({}, SearchProps, {
    values: values,
    search: search,
    setValues: setValues,
    addonAfter: /*#__PURE__*/React.createElement(React.Fragment, null, SearchProps.addonAfter, AfterBaseDom)
  })), topNode ? /*#__PURE__*/React.createElement("div", {
    style: {
      marginBottom: '8px'
    }
  }, topNode) : null, /*#__PURE__*/React.createElement(Table, _extends({
    columns: resetColumns
  }, tableProps, {
    pagination: paginationConfig
  })), /*#__PURE__*/React.createElement(Modal, {
    title: localStorage.getItem('i18nextLng') === 'en-US' || localStorage.getItem('language') === 'en-US' ? 'Table Header Setting' : '表头设置',
    destroyOnClose: true,
    open: showSetting,
    onCancel: function onCancel() {
      setShowSetting(false);
    },
    onOk: handleSelectColumns
  }, /*#__PURE__*/React.createElement(Checkbox.Group, {
    options: checkboxOptions,
    onChange: handleCheckChange,
    defaultValue: checkValues
  })));
};
export default CombinTable;