"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"), _typeof = require("@babel/runtime/helpers/typeof");

Object.defineProperty(exports, "__esModule", {
  value: !0
}), exports.default = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")), _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")), _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")), _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")), _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")), _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")), _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")), _react = _interopRequireWildcard(require("react")), _propTypes = _interopRequireDefault(require("prop-types")), _amosAntd = require("amos-antd"), _amosTool = require("amos-tool"), _AmosFilter = _interopRequireDefault(require("./../filter/AmosFilter")), _SearchBtn = _interopRequireDefault(require("./../btns/SearchBtn"));

function _getRequireWildcardCache(e) {
  if ("function" != typeof WeakMap) return null;
  var t = new WeakMap, r = new WeakMap;
  return (_getRequireWildcardCache = function(e) {
    return e ? r : t;
  })(e);
}

function _interopRequireWildcard(e, t) {
  if (!t && e && e.__esModule) return e;
  if (null === e || "object" !== _typeof(e) && "function" != typeof e) return {
    default: e
  };
  var r = _getRequireWildcardCache(t);
  if (r && r.has(e)) return r.get(e);
  var a = {}, i = Object.defineProperty && Object.getOwnPropertyDescriptor;
  for (var o in e) if ("default" !== o && Object.prototype.hasOwnProperty.call(e, o)) {
    var n = i ? Object.getOwnPropertyDescriptor(e, o) : null;
    n && (n.get || n.set) ? Object.defineProperty(a, o, n) : a[o] = e[o];
  }
  return a.default = e, r && r.set(e, a), a;
}

function _createSuper(e) {
  var t = _isNativeReflectConstruct();
  return function() {
    var r, a = (0, _getPrototypeOf2.default)(e);
    if (t) {
      var i = (0, _getPrototypeOf2.default)(this).constructor;
      r = Reflect.construct(a, arguments, i);
    } else r = a.apply(this, arguments);
    return (0, _possibleConstructorReturn2.default)(this, r);
  };
}

function _isNativeReflectConstruct() {
  if ("undefined" == typeof Reflect || !Reflect.construct) return !1;
  if (Reflect.construct.sham) return !1;
  if ("function" == typeof Proxy) return !0;
  try {
    return Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], (function() {}))), 
    !0;
  } catch (e) {
    return !1;
  }
}

require("./style/amosGrid.scss");

var AmosGridTable = function(e) {
  (0, _inherits2.default)(r, e);
  var t = _createSuper(r);
  function r(e) {
    var a;
    return (0, _classCallCheck2.default)(this, r), a = t.call(this, e), (0, _defineProperty2.default)((0, 
    _assertThisInitialized2.default)(a), "onWindowResize", (function(e) {
      a.getHeightOffset();
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "onSelectChange", (function(e, t) {
      a.setState({
        selectedRowKeys: e,
        selectedRows: t
      }, a.props.getSelectedRows(t, e));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "onSizeChange", (function(e, t) {
      var r = a.props.setPageConfig;
      r && r({
        current: e,
        pageSize: t
      }), a.setState({
        current: e,
        pageSize: t
      }, a.reload);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "onPaginationChange", (function(e) {
      var t = a.props.setPageConfig;
      t && t({
        current: e
      }), a.setState({
        current: e
      }, a.reload);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "getbtns", (function() {
      var e = [];
      return a.props.operBtns.map((function(t, r) {
        e.push(_react.default.createElement("div", {
          key: "btn".concat(r),
          className: "amos-grid-oper-btn"
        }, t));
      })), e;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "changeFilters", (function(e) {
      var t = e, r = {};
      Object.keys(t).map((function(e) {
        var a = t[e];
        a && "" !== a && (r[e] = a, "enabled" === e && (r[e] = "启用" === a));
      })), a.setState({
        filter: r
      }, a.reload);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "openFilter", (function() {
      var e = a.state.showFilter;
      a.setState({
        showFilter: !e
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "reload", (function() {
      var e = a.state, t = e.current, r = e.pageSize, i = e.filter, o = e.otherFilter;
      a.props.getTableDataAction({
        current: t - 1,
        pageSize: r
      }, JSON.stringify(i), o), a.getHeightOffset();
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "addOperateCol", (function(e, t) {
      var r = (0, a.props.operateCol)(e, t);
      return _react.default.createElement("div", {
        className: "operation-buttons"
      }, r);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "showTotal", (function(e) {
      return "共 ".concat(e, " 条");
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "generateRowkey", (function(e, t) {
      return e.id ? e.id : t + (a.state.current - 1) * a.state.pageSize;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "getHeightOffset", (function() {
      var e = a.props.getPanelHeight, t = void 0 === e ? 450 : e();
      t && a.setState({
        maxHeight: t
      });
    })), a.state = {
      selectedRowKeys: [],
      selectedRows: [],
      loading: !1,
      dataList: [],
      size: "middle",
      current: e.defaultPageConfig ? e.defaultPageConfig.current : 1,
      pageSize: e.defaultPageConfig ? e.defaultPageConfig.pageSize : 10,
      filter: {},
      totals: 1,
      showFilter: !0,
      otherFilter: e.otherFilter,
      maxHeight: 450
    }, a;
  }
  return (0, _createClass2.default)(r, [ {
    key: "componentDidMount",
    value: function() {
      this.props.callBack(this.reload), this.getHeightOffset(), window.addEventListener("resize", this.onWindowResize);
    }
  }, {
    key: "componentWillReceiveProps",
    value: function(e) {
      e.url !== this.props.url && this.reload(), e.otherFilter && e.otherFilter !== this.props.otherFilter && this.setState({
        otherFilter: e.otherFilter
      }, this.reload);
    }
  }, {
    key: "componentWillUnmount",
    value: function() {
      window.removeEventListener("resize", this.onWindowResize);
    }
  }, {
    key: "isVisableSearch",
    value: function(e) {
      var t = !1;
      return e.map((function(e) {
        if (e.query) return t = !0, !0;
      })), t;
    }
  }, {
    key: "render",
    value: function() {
      var e = this, t = this.state, r = t.loading, a = t.selectedRowKeys, i = t.size, o = t.maxHeight, n = this.props, s = n.columns, l = n.isChecked, u = n.operateCol, p = n.isTreeTable, f = n.isPageable, d = n.pagination, c = n.dataList, h = n.totals, _ = n.rowClassName, g = {
        selectedRowKeys: this.props.selectedRowKeys || a,
        onChange: this.onSelectChange
      };
      g = l ? g : void 0, u && (s[s.length - 1].render = function(t, r) {
        return e.addOperateCol(t, r);
      });
      var y = !1 !== d && (d || {
        size: "small",
        total: h,
        current: this.state.current,
        pageSize: this.state.pageSize,
        onShowSizeChange: this.onSizeChange,
        onChange: this.onPaginationChange,
        showSizeChanger: !0,
        showTotal: this.showTotal
      }), m = !1 !== f && y;
      _amosTool.Log.info("the table has pagination", !!m);
      var b = this.state.showFilter ? "other-table-bar" : "table-bar", T = {
        display: this.state.showFilter ? "block" : "none"
      };
      return _react.default.createElement("div", {
        className: "grid-content"
      }, _react.default.createElement("div", {
        className: "oper-bar"
      }, _react.default.createElement("div", {
        className: "amos-grid-oper-btns"
      }, this.getbtns())), _react.default.createElement("div", {
        className: b
      }, _react.default.createElement(_amosAntd.Table, {
        title: function() {
          return _react.default.createElement("div", {
            style: T
          }, _react.default.createElement(_AmosFilter.default, {
            filterSearchModel: s,
            closeFilter: e.openFilter,
            changeFilters: e.changeFilters
          }));
        },
        rowKey: p ? "key" : this.generateRowkey,
        loading: r,
        rowSelection: g,
        columns: s,
        dataSource: c,
        pagination: m,
        size: i,
        bordered: !0,
        scroll: {
          y: o
        },
        rowClassName: _
      })));
    }
  } ]), r;
}(_react.Component);

AmosGridTable.displayName = "AmosGridTable", AmosGridTable.propTypes = {
  url: _propTypes.default.string,
  columns: _propTypes.default.array,
  getSelectedRows: _propTypes.default.func,
  operBtns: _propTypes.default.array,
  isChecked: _propTypes.default.bool,
  operateCol: _propTypes.default.func,
  callBack: _propTypes.default.func,
  otherFilter: _propTypes.default.string,
  dataList: _propTypes.default.array,
  getTableDataAction: _propTypes.default.func,
  selectedRowKeys: _propTypes.default.array,
  totals: _propTypes.default.number,
  pagination: _propTypes.default.any,
  isPageable: _propTypes.default.bool,
  isTreeTable: _propTypes.default.bool,
  rowClassName: _propTypes.default.func,
  setPageConfig: _propTypes.default.func,
  defaultPageConfig: _propTypes.default.object,
  getPanelHeight: _propTypes.default.func
}, AmosGridTable.defaultProps = {
  operBtns: [],
  dataList: [],
  totals: 1,
  columns: [],
  isChecked: !0,
  isTreeTable: !1,
  otherFilter: ""
};

var _default = AmosGridTable;

exports.default = _default;