import { __assign, __extends, __rest } from "tslib";
import React, { Component } from 'react';
import isEqual from 'lodash/isEqual';
import Table from './table';
//遗留代码
var shallowEqual = function (objA, objB) {
    if (objA === objB) {
        return true;
    }
    if (typeof objA !== 'object' ||
        objA === null ||
        typeof objB !== 'object' ||
        objB === null) {
        return false;
    }
    var keysA = Object.keys(objA);
    var keysB = Object.keys(objB);
    if (keysA.length !== keysB.length) {
        return false;
    }
    // Test for A's keys different from B.
    var bHasOwnProperty = Object.prototype.hasOwnProperty.bind(objB);
    for (var i = 0; i < keysA.length; i += 1) {
        if (!bHasOwnProperty(keysA[i]) || objA[keysA[i]] !== objB[keysA[i]]) {
            return false;
        }
    }
    return true;
};
var AsyncTable = /** @class */ (function (_super) {
    __extends(AsyncTable, _super);
    function AsyncTable(props) {
        var _this = _super.call(this, props) || this;
        _this.getFilter = function (prevProps) {
            var isChange = !isEqual(prevProps.extraFilter, _this.props.extraFilter);
            return isChange;
        };
        _this.reset = function () {
            _this.setState({
                pageNo: 1
            }, function () {
                _this.getData();
            });
        };
        _this.state = {
            pageNo: 1,
            pageSize_: props.pageSize ? props.pageSize : 10,
            dataSource: [],
            total: 0,
            loading: true
        };
        return _this;
    }
    AsyncTable.prototype.componentDidMount = function () {
        this.getData();
    };
    AsyncTable.prototype.shouldComponentUpdate = function (nextProps, nextState) {
        // state.data deepEqual 效率可能比较低
        return (!isEqual(this.props, nextProps) || !shallowEqual(this.state, nextState));
    };
    AsyncTable.prototype.componentDidUpdate = function (prevProps, prevState) {
        var _this = this;
        // 表格是否需要重新获取数据 取决于
        // extraFilter 载体数据
        // fetchData 获取数据的方法
        // pageNo 当前页
        // pageSize 页大小
        if (this.getFilter(prevProps) ||
            !isEqual(prevProps.reloadKey, this.props.reloadKey) ||
            prevState.pageSize_ !== this.state.pageSize_) {
            this.setState({
                pageNo: 1
            }, function () {
                _this.getData();
            });
        }
        if (prevState.pageNo !== this.state.pageNo) {
            this.getData();
        }
    };
    AsyncTable.prototype.onPaginationChange = function (current) {
        this.setState({
            pageNo: current
        });
    };
    AsyncTable.prototype.getData = function () {
        var _this = this;
        var _a = this.state, pageNo = _a.pageNo, pageSize_ = _a.pageSize_;
        var extraFilter = this.props.extraFilter;
        this.setState({
            loading: true
        }, function () {
            _this.props
                .fetchData(__assign({ PageNo: pageNo, PageSize: pageSize_ }, extraFilter))
                .then(function (res) {
                // 如果某一页的数据为空，则跳转回第一页
                if ((!res.dataSource || res.dataSource.length <= 0) && pageNo > 1) {
                    _this.setState({ pageNo: 1 });
                }
                else {
                    _this.setState({
                        dataSource: res.dataSource,
                        total: res.total,
                        loading: false
                    });
                }
            })
                .catch(function () {
                _this.setState({ loading: false });
            });
        });
    };
    AsyncTable.prototype.handlePageSizeChange = function (size) {
        this.setState({
            pageSize_: size,
            pageNo: 1
        });
    };
    AsyncTable.prototype.render = function () {
        var _this = this;
        var _a = this.props, pagination = _a.pagination, styleName = _a.styleName, columns = _a.columns, fetchData = _a.fetchData, _b = _a.extraFilter, extraFilter = _b === void 0 ? {} : _b, onPageChange = _a.onPageChange, reloadKey = _a.reloadKey, other = __rest(_a, ["pagination", "styleName", "columns", "fetchData", "extraFilter", "onPageChange", "reloadKey"]);
        var _c = this.state, dataSource = _c.dataSource, loading = _c.loading, total = _c.total, pageSize_ = _c.pageSize_, pageNo = _c.pageNo;
        return (React.createElement(Table, __assign({}, other, { className: styleName, columns: columns, dataSource: dataSource, isLoading: loading, isZebra: false, hasBorder: false, pagination: __assign({ showTotal: true, pageSizePosition: 'start', onPageSizeChange: function (size) {
                    _this.handlePageSizeChange(size);
                    _this.props.onPageSizeChange && _this.props.onPageSizeChange(size);
                }, onChange: function (id) {
                    _this.onPaginationChange(id);
                    _this.props.onPageChange && _this.props.onPageChange(id);
                }, current: pageNo, total: total, pageSize: pageSize_ }, pagination) })));
    };
    return AsyncTable;
}(Component));
export default AsyncTable;
