"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var antd_1 = require("antd");
var dva_1 = require("dva");
var utils_1 = require("../../utils");
var TableHeader_1 = require("./TableHeader");
var React = require("react");
var _ = require("lodash");
var DefaultPageSize = 10;
/**
 * TableLayout 是深度定制的 table：主要统一了一些元素：
 * 1. 统一使用 order by 进行排序
 * 2. 默认每页显示10条记录
 * 3. 默认使用小号的table
 * 4. 默认 rowKey 为 ‘id’
 * 5. 支持过滤、搜索等条件搜索
 * 6. 使用fetch进行查询
 * 7. 支持主动刷新等等。
 *
*/
var TableLayout = /** @class */ (function (_super) {
    __extends(TableLayout, _super);
    function TableLayout() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.rowKey = 'id';
        _this.sorter = {};
        _this.pageChanged = true;
        _this.queryFetch = '';
        _this.state = {
            selectedRowKeys: [],
            selectObjects: [],
            current: 1,
            pageSize: _this.props.pageSize || DefaultPageSize,
        };
        _this.query = {
            start_: 0,
            count_: _this.props.pageSize || DefaultPageSize,
            order_: _this.rowKey,
            by_: 'desc',
        };
        _this.reflush = function (param) {
            if (param === void 0) { param = {}; }
            _this.onQuery(_.extend(_this.query, { start_: 0 }, param));
        };
        return _this;
    }
    TableLayout.prototype.getQuery = function () {
        return this.query;
    };
    TableLayout.prototype.componentDidMount = function () {
        if (!_.has(this.props, 'namespace')) {
            if (!_.has(this.props, 'fetch') || !_.has(this.props, 'total') || !_.has(this.props, 'dataSource')) {
                console.error('参数 namespace 或 fetch/total/dataSource 缺失');
            }
        }
        this.queryFetch = this.props.fetch ? this.props.fetch : (this.props.namespace + '/_query');
        this.rowKey = this.props.rowKey || 'id';
        this.query.order_ = this.rowKey;
        if (_.has(this.props, 'sort')) {
            this.query.order_ = this.props.sort.order;
            this.query.by_ = this.props.sort.by;
        }
        var param = {
            type: this.queryFetch,
            query: _.extend({}, this.props.default, this.query)
        };
        // this.props.dispatch()
        // @ts-ignore
        this.props.dispatch(param);
    };
    TableLayout.prototype.onQuery = function (param) {
        _.extend(this.query, param, { start_: 0 });
        this.setState({
            current: 1,
            selectedRowKeys: [],
            selectedRows: [],
        });
        // @ts-ignore
        this.props.dispatch({
            type: this.queryFetch,
            query: _.extend({}, this.props.default, this.query)
        });
    };
    TableLayout.prototype.search = function (pagination, filters, sorter) {
        var query = __assign({}, this.query, { start_: (pagination.current - 1) * pagination.pageSize, count_: pagination.pageSize });
        this.query.count_ = pagination.pageSize;
        var state = {};
        if (!_.isEqual(this.sorter, sorter)) {
            query.start_ = 0;
            state.current = 1;
            this.setState({
                current: 1,
            });
            this.sorter = sorter;
        }
        else {
            state.pageSize = pagination.pageSize;
            state.current = pagination.current;
        }
        this.setState(__assign({}, state));
        if (_.has(sorter, 'field')) {
            query.order_ = sorter.field;
            query.by_ = sorter.order == 'ascend' ? 'asc' : 'desc';
        }
        else {
            query.order_ = this.rowKey;
            query.by_ = 'desc';
        }
        // @ts-ignore
        this.props.dispatch({
            type: this.queryFetch,
            query: _.extend({}, this.props.default, query)
        });
    };
    TableLayout.prototype.onSelect = function (selectedRowKeys, selectObjects) {
        this.setState({
            selectedRowKeys: selectedRowKeys, selectObjects: selectObjects
        });
    };
    TableLayout.prototype.render = function () {
        var _this = this;
        var params = {};
        if (!_.has(this.props, 'size')) {
            params.size = 'default';
        }
        if (!_.has(this.props, 'rowKey')) {
            params.rowKey = this.rowKey;
        }
        var total = 0;
        if (_.has(this.props, 'total')) {
            total = this.props.total;
        }
        else {
            total = this.props[this.props.namespace].total;
        }
        if (!_.has(this.props, 'dataSource')) {
            params.dataSource = this.props[this.props.namespace].list;
        }
        // let model = this.props.namespace;
        //  //选择功能，跨页记忆选择, 有BUG，暂时取消此功能
        // if (this.props.select) {
        //     params.rowSelection = {
        //         selectedRowKeys: this.state.selectedRowKeys,
        //         onChange: (selectedRowKeys) => this.onSelect(selectedRowKeys),
        //     }
        // }
        //去掉被设置为false的列
        params.columns = _.filter(this.props.columns, function (item) { return item.show === undefined || item.show == true; });
        var selectedRowKeys = [];
        var selectObjects = [];
        if (this.props.select) {
            (this.props.dataSource || params.dataSource).map(function (item) {
                if (_.indexOf(_this.state.selectedRowKeys, item[_this.rowKey]) >= 0) {
                    selectedRowKeys.push(item[_this.rowKey]);
                    selectObjects.push(item);
                }
            });
            params.rowSelection = {
                selectedRowKeys: selectedRowKeys,
                onChange: function (selectedRowKeys, selectObjects) { return _this.onSelect(selectedRowKeys, selectObjects); },
                getCheckboxProps: function (record) { return ({
                    disabled: !_.has(record, _this.rowKey)
                }); },
            };
        }
        return React.createElement(antd_1.Layout, null,
            React.createElement(TableHeader_1.default, __assign({ className: "table-header" }, this.props, { onQuery: function (param) { return _this.onQuery(param); }, selected: selectedRowKeys, selectObjects: selectObjects })),
            React.createElement(antd_1.Layout.Content, null,
                React.createElement(antd_1.Table, __assign({}, this.props, params, { rowClassName: function (item) { return _.has(item, '__EFFECT') ? 'row-high-' + item['__EFFECT'] : ''; }, onChange: function (pagination, filters, sorter) { return _this.search(pagination, filters, sorter); }, pagination: {
                        current: this.state.current,
                        total: total,
                        showQuickJumper: true,
                        showSizeChanger: true,
                        hideOnSinglePage: false,
                        pageSize: this.state.pageSize,
                        pageSizeOptions: utils_1.Const.PAGE_OPTIONS,
                    } }))));
    };
    return TableLayout;
}(React.PureComponent));
var state = function (props) { return props; };
var dispatch = undefined;
var merge = undefined;
var options = { withRef: true };
var tableLayout = dva_1.connect(state, dispatch, merge, options)(TableLayout);
tableLayout.prototype.reflush = function (param) {
    if (param === void 0) { param = {}; }
    this.getWrappedInstance().reflush(param);
};
tableLayout.prototype.getQuery = function () {
    return this.getWrappedInstance().getQuery();
};
exports.default = tableLayout;
