"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");

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 l = i ? Object.getOwnPropertyDescriptor(e, o) : null;
    l && (l.get || l.set) ? Object.defineProperty(a, o, l) : 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), "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) {
      a.setState({
        current: e,
        pageSize: t
      }, a.reload);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "onPaginationChange", (function(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);
    })), (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;
    })), a.state = {
      selectedRowKeys: [],
      selectedRows: [],
      loading: !1,
      dataList: [],
      size: "middle",
      current: 1,
      pageSize: 10,
      filter: {},
      totals: 1,
      showFilter: !1,
      otherFilter: e.otherFilter
    }, a;
  }
  return (0, _createClass2.default)(r, [ {
    key: "componentDidMount",
    value: function() {
      this.props.callBack(this.reload);
    }
  }, {
    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: "render",
    value: function() {
      var e = this, t = this.state, r = t.loading, a = t.selectedRowKeys, i = t.size, o = this.props, l = o.columns, n = o.isChecked, s = o.operateCol, u = o.isTreeTable, p = o.isPageable, d = o.pagination, c = o.dataList, f = o.totals, h = {
        selectedRowKeys: this.props.selectedRowKeys || a,
        onChange: this.onSelectChange
      };
      h = n ? h : void 0, s && (l[l.length - 1].render = function(t, r) {
        return e.addOperateCol(t, r);
      });
      var _ = !1 !== d && (d || {
        size: "small",
        total: f,
        current: this.state.current,
        pageSize: this.state.pageSize,
        onShowSizeChange: this.onSizeChange,
        onChange: this.onPaginationChange,
        showSizeChanger: !0,
        showTotal: this.showTotal
      }), y = !1 !== p && _;
      return _amosTool.Log.info("the table has pagination", !!y), _react.default.createElement("div", null, _react.default.createElement("div", null, _react.default.createElement("div", null, this.getbtns())), _react.default.createElement("div", {
        style: {
          marginTop: "10px"
        }
      }, _react.default.createElement(_amosAntd.Table, {
        rowKey: u ? "key" : this.generateRowkey,
        loading: r,
        rowSelection: h,
        columns: l,
        dataSource: c,
        pagination: y,
        size: i,
        bordered: !0,
        scroll: {
          y: 450
        }
      })));
    }
  } ]), 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
}, AmosGridTable.defaultProps = {
  operBtns: [],
  dataList: [],
  totals: 1,
  columns: [],
  isChecked: !0,
  isTreeTable: !1,
  otherFilter: ""
};

var _default = AmosGridTable;

exports.default = _default;