import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
var _jsxFileName = 'src/table/TableFilter.jsx';
/**
 * tableFilter.jsx
 *
 * @Author: apple
 * @Date: 2018/1/11 11:37
 */

import React from 'react';
import { Checkbox, CheckboxGroup } from '../checkbox';
import Radio, { RadioGroup } from '../radio';
import ToolTip from '../tooltip';
import Icon from '../icon';
import classNames from 'classnames';
import Button from '../button';

/**
 * @ignore
 * */

var TableFilter = function (_React$Component) {
    _inherits(TableFilter, _React$Component);

    function TableFilter(props, context) {
        _classCallCheck(this, TableFilter);

        var _this = _possibleConstructorReturn(this, (TableFilter.__proto__ || Object.getPrototypeOf(TableFilter)).call(this, props, context));

        _this.handleFilter = function (type) {
            var _this$props = _this.props,
                filterMethod = _this$props.filterMethod,
                filters = _this$props.filters,
                filterMultiple = _this$props.filterMultiple;
            var initFilters = _this.state.initFilters;

            switch (type) {
                case 'sure':
                    filterMultiple ? filterMethod(filters.filter(function (item) {
                        var equal = false;
                        initFilters.forEach(function (id) {
                            if (item.value === id) {
                                equal = true;
                            }
                        });
                        return equal;
                    })) : filterMethod(filters.filter(function (item) {
                        return initFilters === item.value;
                    }));
                    break;
                case 'reset':
                    var resetValue = filterMultiple ? [] : '';
                    _this.setState({
                        initFilters: resetValue
                    }, function () {
                        return filterMethod([]);
                    });
                    break;
                default:
                    return;
            }
        };

        _this.handleSelect = function (event, checkedValue) {
            _this.setState({
                initFilters: checkedValue
            });
        };

        _this.handleTableFilterOpen = function () {
            _this.setState({
                tableFilterState: true
            });
        };

        _this.handleTableFilterClose = function (type) {
            _this.setState({
                tableFilterState: false,
                type: type
            });
        };

        _this.handleCloseTooltip = function () {
            var type = _this.state.type;
            type && _this.handleFilter(type);
        };

        _this.state = {
            initFilters: _this.props.filterMultiple ? [] : '',
            tableFilterState: false,
            type: ''
        };
        return _this;
    }

    _createClass(TableFilter, [{
        key: 'render',
        value: function render() {
            var _this2 = this;

            var _props = this.props,
                prefixCls = _props.prefixCls,
                filters = _props.filters,
                filterMultiple = _props.filterMultiple,
                id = _props.id;
            var _state = this.state,
                initFilters = _state.initFilters,
                tableFilterState = _state.tableFilterState;

            var onFiltered = false;
            if (filterMultiple) {
                onFiltered = !!initFilters.length;
            } else {
                onFiltered = !!initFilters;
            }
            return React.createElement(
                ToolTip,
                {
                    content: React.createElement(Icon, { type: 'filter', 'data-filter': id, className: classNames({ 'icon-filtered': onFiltered }), __source: {
                            fileName: _jsxFileName,
                            lineNumber: 101
                        },
                        __self: this
                    }),
                    trigger: 'click',
                    isOpen: tableFilterState,
                    onOpen: this.handleTableFilterOpen,
                    onClose: this.handleCloseTooltip,
                    position: 'bottomLeft', __source: {
                        fileName: _jsxFileName,
                        lineNumber: 99
                    },
                    __self: this
                },
                React.createElement(
                    'div',
                    { className: prefixCls + '-filter', __source: {
                            fileName: _jsxFileName,
                            lineNumber: 108
                        },
                        __self: this
                    },
                    filterMultiple ? React.createElement(
                        CheckboxGroup,
                        {
                            value: initFilters,
                            onChange: function onChange(event, value) {
                                return _this2.handleSelect(event, value);
                            },
                            className: prefixCls + '-filter-body', __source: {
                                fileName: _jsxFileName,
                                lineNumber: 110
                            },
                            __self: this
                        },
                        filters.map(function (item) {
                            var label = item.label,
                                value = item.value;

                            return React.createElement(Checkbox, { key: value, label: label, value: value, __source: {
                                    fileName: _jsxFileName,
                                    lineNumber: 116
                                },
                                __self: _this2
                            });
                        })
                    ) : React.createElement(
                        RadioGroup,
                        {
                            value: initFilters,
                            onChange: function onChange(event, value) {
                                return _this2.handleSelect(event, value);
                            },
                            className: prefixCls + '-filter-body', __source: {
                                fileName: _jsxFileName,
                                lineNumber: 120
                            },
                            __self: this
                        },
                        filters.map(function (item) {
                            var label = item.label,
                                value = item.value;

                            return React.createElement(Radio, { key: value, label: label, value: value, __source: {
                                    fileName: _jsxFileName,
                                    lineNumber: 126
                                },
                                __self: _this2
                            });
                        })
                    ),
                    React.createElement(
                        'div',
                        { className: prefixCls + '-filter-footer', __source: {
                                fileName: _jsxFileName,
                                lineNumber: 130
                            },
                            __self: this
                        },
                        React.createElement(
                            Button,
                            { size: 'small', hollow: true, onClick: function onClick() {
                                    return _this2.handleTableFilterClose('sure');
                                }, __source: {
                                    fileName: _jsxFileName,
                                    lineNumber: 131
                                },
                                __self: this
                            },
                            '\u786E\u5B9A'
                        ),
                        React.createElement(
                            Button,
                            { size: 'small', hollow: true, onClick: function onClick() {
                                    return _this2.handleTableFilterClose('reset');
                                }, __source: {
                                    fileName: _jsxFileName,
                                    lineNumber: 134
                                },
                                __self: this
                            },
                            '\u91CD\u7F6E'
                        )
                    )
                )
            );
        }
    }]);

    return TableFilter;
}(React.Component);

TableFilter.propTypes = {};
TableFilter.defaultProps = {};


export default TableFilter;