"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);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __values = (this && this.__values) || function (o) {
    var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
    if (m) return m.call(o);
    return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var index_1 = require("../index");
var DateRange_1 = require("./DateRange");
var _ = require("lodash");
var TableHeader = /** @class */ (function (_super) {
    __extends(TableHeader, _super);
    function TableHeader(props) {
        var _this = _super.call(this, props) || this;
        _this.state = {
            showSearch: false,
        };
        _this.FilterMap = {};
        _this.SearchMap = {};
        _this.SearchKey = {};
        if (_.isArray(_this.props.searchs)) {
            _this.props.searchs.map(function (search) {
                _this.SearchKey[search.field] = search.name;
            });
        }
        return _this;
    }
    /**
     * 条件过滤
     *
    */
    TableHeader.prototype.doFilter = function (filter, value) {
        var query = {};
        if (filter.type == 'checkbox') {
            this.FilterMap[filter.field] = value;
            query[filter.field] = value.join(',');
        }
        else if (filter.type == 'daterange') {
            var _a = __read(filter.field.split(','), 2), start = _a[0], end = _a[1];
            var _b = __read(value.split(','), 2), startime = _b[0], endtime = _b[1];
            if (startime) {
                query[start] = startime;
            }
            else {
                query[start] = null;
                // _.unset(query,start);
            }
            if (endtime) {
                query[end] = endtime;
            }
            else {
                query[end] = null;
                // _.unset(query,end);
            }
        }
        else if (filter.type == 'select') {
            this.FilterMap[filter.field] = value;
            query[filter.field] = value;
        }
        else {
            this.FilterMap[filter.field] = value.target.value;
            query[filter.field] = value.target.value;
        }
        this.props.onQuery(query);
    };
    TableHeader.prototype.checkBox = function (filter, checked) {
        if (checked) {
            this.doFilter(filter, filter.options.map(function (item) { return item.value; }));
        }
        else {
            this.doFilter(filter, []);
        }
    };
    /**
     * 构造过滤UI
     *
    */
    TableHeader.prototype.filters = function (filters) {
        var _this = this;
        var radioGroup = function (filter) {
            var def = filter.options.length > 0 ? filter.options[0].value : '';
            var checked = _.find(filter.options, { checked: true });
            if (checked) {
                //@ts-ignore
                def = checked.value;
            }
            return React.createElement(index_1.Radio.Group, { onChange: function (value) { return _this.doFilter(filter, value); }, defaultValue: def }, filter.options.map(function (option) { return React.createElement(index_1.Radio, { key: filter.field + '-' + option.value, value: option.value }, option.name); }));
        };
        var checkboxGroup = function (filter) {
            var def = [];
            filter.options.map(function (option) {
                if (option.checked)
                    def.push(option.value);
            });
            return React.createElement(index_1.Checkbox.Group, { onChange: function (value) { return _this.doFilter(filter, value); }, defaultValue: def, value: _this.FilterMap[filter.field] }, filter.options.map(function (option) { return React.createElement(index_1.Checkbox, { key: filter.field + '-' + option.value, value: option.value }, option.name); }));
        };
        var dateRangeGroup = function (filter) {
            return React.createElement(DateRange_1.default, { showTime: true, onChange: function (start, end) { return _this.doFilter(filter, start + ',' + end); } });
        };
        var selectGroup = function (filter) {
            return React.createElement(index_1.Select, { onChange: function (value) { return _this.doFilter(filter, value); }, defaultValue: "" },
                React.createElement(index_1.Select.Option, { value: "" }, "\u8BF7\u9009\u62E9" + filter.name),
                filter.options.map(function (option) { return React.createElement(index_1.Select.Option, { key: filter.field + '-' + option.value, value: option.value }, option.name); }));
        };
        var gloupFilters = function (filters) {
            var e_1, _a;
            var filterGroup = [];
            var breaked = false;
            try {
                for (var filters_1 = __values(filters), filters_1_1 = filters_1.next(); !filters_1_1.done; filters_1_1 = filters_1.next()) {
                    var filter = filters_1_1.value;
                    var group = [];
                    if (!breaked) {
                        filterGroup.push(group);
                    }
                    else {
                        group = filterGroup[filterGroup.length - 1];
                    }
                    group.push(filter);
                    if (filter.inline) {
                        breaked = true;
                    }
                    else {
                        breaked = false;
                    }
                }
            }
            catch (e_1_1) { e_1 = { error: e_1_1 }; }
            finally {
                try {
                    if (filters_1_1 && !filters_1_1.done && (_a = filters_1.return)) _a.call(filters_1);
                }
                finally { if (e_1) throw e_1.error; }
            }
            return filterGroup;
        };
        var displayFilters = gloupFilters(filters);
        var buildFilter = function (filter) {
            switch (filter.type) {
                case 'checkbox':
                    return checkboxGroup(filter);
                case 'daterange':
                    return dateRangeGroup(filter);
                case 'select':
                    return selectGroup(filter);
                default:
                    return radioGroup(filter);
            }
        };
        return displayFilters.map(function (filterGroup, i) {
            return React.createElement(index_1.Row, { key: 'filter-group-' + i, type: "flex", className: "table-filter " + (i == (displayFilters.length - 1) ? 'table-filter-end' : '') },
                " ",
                filterGroup.map(function (filter) {
                    var checkedStatus = 0;
                    var checked = 0;
                    if (filter.type == 'checkbox') {
                        filter.options.map(function (option) {
                            if (_.includes(_this.FilterMap[filter.field], option.value)) {
                                checked++;
                            }
                        });
                        checkedStatus = checked == 0 ? 0 : (checked == filter.options.length ? 1 : 2);
                    }
                    return React.createElement(index_1.Col, { key: filter.field + '-col-' + filter.type, className: (filter.inline ? "table-filter-liline" : "") + (filter.className ? " " + filter.className : '') },
                        React.createElement("span", { className: "table-filter-label" },
                            filter.name,
                            filter.type == 'checkbox' && React.createElement("a", { title: checkedStatus == 1 ? '全部取消' : '全选', onClick: function () { return _this.checkBox(filter, checkedStatus !== 1); } },
                                React.createElement(index_1.Icon, { type: 'check-square', theme: checkedStatus == 1 ? 'filled' : 'outlined', style: {
                                        cursor: 'pointer',
                                        lineHeight: '48px',
                                        marginLeft: 2,
                                        marginRight: 2,
                                        fontSize: 11
                                    } })),
                            " :"),
                        buildFilter(filter));
                }));
        });
    };
    TableHeader.prototype.clearSearch = function (key) {
        // @ts-ignore   
        this.refs[key].value = '';
        this.SearchMap[key] = '';
        this.props.onQuery(this.SearchMap);
    };
    TableHeader.prototype.toogleSearch = function (show, exe) {
        var _this = this;
        if (exe === void 0) { exe = false; }
        this.setState({ showSearch: show });
        this.SearchMap = {};
        if (exe) {
            _.keys(this.refs).map(function (key) {
                // @ts-ignore
                var val = _.trim(_this.refs[key].value);
                if (val) {
                    _this.SearchMap[key] = val;
                }
                else {
                    _this.SearchMap[key] = '';
                }
            });
            this.props.onQuery(this.SearchMap);
        }
    };
    /**
     * 构造搜索UI
     *
    */
    TableHeader.prototype.searchs = function (searchs) {
        var _this = this;
        var searchItem = function (search) {
            return React.createElement(index_1.Form.Item, { key: '-search' + search.field, label: search.name, labelCol: { span: 8 }, wrapperCol: { span: 14 } },
                React.createElement("input", { placeholder: search.name, ref: search.field, className: "ant-input" }));
        };
        var searchGroup = function (searchs) {
            return React.createElement(index_1.Card, { className: "table-search-panel" },
                searchs.map(function (search) { return searchItem(search); }),
                React.createElement("div", { style: { textAlign: 'right', paddingRight: '24px' } },
                    React.createElement(index_1.Button, { onClick: function () { return _this.toogleSearch(false, true); } }, "\u641C\u7D22")));
        };
        return React.createElement(index_1.Dropdown, { overlay: searchGroup(searchs), placement: "bottomLeft", onVisibleChange: function (show) { return _this.toogleSearch(show); }, visible: this.state.showSearch, trigger: ['click'] },
            React.createElement(index_1.Button, { style: { marginLeft: 8 }, size: "default" },
                "\u66F4\u591A\u6761\u4EF6 ",
                React.createElement(index_1.Icon, { type: "down" })));
    };
    TableHeader.prototype.actions = function (actions) {
        var _this = this;
        var btn = function (action) {
            if (action.upload) {
                if (_.isString(action.upload)) {
                    return React.createElement(index_1.FileUpload.Single, { key: 'table-header-' + action.name, text: action.name, icon: action.icon || 'upload', url: action.upload, callback: action.action });
                }
                else {
                    return React.createElement(index_1.FileUpload.Single, { key: 'table-header-' + action.name, text: action.name, icon: action.icon || 'upload', callback: action.action });
                }
            }
            var param = {};
            if (action.icon) {
                param.icon = action.icon;
            }
            if (action.type) {
                param.type = action.type;
            }
            if (action.action) {
                param.onClick = function () { return action.action(_this.props.selected, _this.props.selectObjects); };
            }
            var name = action.name;
            if (action.mustSelect) {
                if (_this.props.selected.length == 0) {
                    param.disabled = "disabled";
                }
                else {
                    name = action.name + "  (" + _this.props.selected.length + ")";
                }
            }
            if (action.className) {
                param.className = action.className;
            }
            return React.createElement(index_1.Button, __assign({ key: 'table-header-' + action.name, size: "default" }, param), name);
        };
        return React.createElement("div", null, actions.map(function (action) { return btn(action); }));
    };
    TableHeader.prototype.tags = function () {
        var _this = this;
        return _.keys(this.SearchMap).map(function (key) {
            if (_this.SearchMap[key]) {
                return React.createElement(index_1.Tag, { closable: true, onClose: function () { return _this.clearSearch(key); } },
                    _this.SearchKey[key],
                    " : ",
                    _this.SearchMap[key]);
            }
        });
    };
    TableHeader.prototype.render = function () {
        var _a = __read([[], this.props.searchs || [], []], 3), filters = _a[0], searchs = _a[1], actions = _a[2];
        if (_.isArray(this.props.filters) && this.props.filters.length > 0) {
            filters = _.filter(this.props.filters, function (item) { return item.show === undefined || item.show == true; });
        }
        if (_.isArray(this.props.actions) && this.props.actions.length > 0) {
            actions = _.filter(this.props.actions, function (item) { return item.show === undefined || item.show == true; });
        }
        // let show = _.isArray(this.props.filters) || _.isArray(this.props.filters) || _.isArray(this.props.filters);
        if (filters.length > 0 || searchs.length > 0 || actions.length > 0) {
            return React.createElement(index_1.Layout.Header, { className: "table-header" },
                filters.length > 0 && this.filters(filters),
                (searchs.length > 0 || actions.length > 0) &&
                    React.createElement(index_1.Row, { type: "flex", className: "table-action" },
                        React.createElement(index_1.Col, { className: "table-action-search" }, searchs.length > 0 && this.searchs(searchs)),
                        React.createElement(index_1.Col, null, this.tags()),
                        React.createElement(index_1.Col, { className: "table-action-group" }, actions.length > 0 && this.actions(actions))));
        }
        return [];
    };
    return TableHeader;
}(React.Component));
exports.default = TableHeader;
