import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";

/**
 * tableFilter.jsx
 *
 * @Author: apple
 * @Date: 2018/1/11 11:37
 */
import React from 'react';
import classNames from 'classnames';
import Checkbox, { CheckboxGroup } from '../checkbox';
import Radio, { RadioGroup } from '../radio';
import ToolTip from '../tooltip';
import FilterList from '../icons/internals/FilterList';
import Button from '../button';
/**
 * @ignore
 * */

var TableFilter =
/*#__PURE__*/
function (_React$Component) {
  _inherits(TableFilter, _React$Component);

  function TableFilter(props, context) {
    var _this;

    _classCallCheck(this, TableFilter);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(TableFilter).call(this, props, context));

    _defineProperty(_assertThisInitialized(_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':
          _this.setState({
            initFilters: filterMultiple ? [] : ''
          }, function () {
            return filterMethod([]);
          });

          break;

        default:
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleSelect", function (event, checkedValue) {
      _this.setState({
        initFilters: checkedValue
      });
    });

    _defineProperty(_assertThisInitialized(_this), "handleTableFilterOpen", function () {
      _this.setState({
        tableFilterState: true
      });
    });

    _defineProperty(_assertThisInitialized(_this), "handleTableFilterClose", function (type) {
      _this.setState({
        tableFilterState: false,
        type: type
      });
    });

    _defineProperty(_assertThisInitialized(_this), "handleCloseTooltip", function () {
      var type = _this.state.type;
      if (type) _this.handleFilter(type);
    });

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

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

      var _this$props2 = this.props,
          prefixCls = _this$props2.prefixCls,
          filters = _this$props2.filters,
          filterMultiple = _this$props2.filterMultiple,
          id = _this$props2.id;
      var _this$state = this.state,
          initFilters = _this$state.initFilters,
          tableFilterState = _this$state.tableFilterState;
      var onFiltered = false;

      if (filterMultiple) {
        onFiltered = !!initFilters.length;
      } else {
        onFiltered = !!initFilters;
      }

      return React.createElement(ToolTip, {
        content: React.createElement(FilterList, {
          "data-filter": id,
          className: classNames("".concat(prefixCls, "-icon"), _defineProperty({}, "".concat(prefixCls, "-icon-filtered"), onFiltered))
        }),
        trigger: "click",
        isOpen: tableFilterState,
        onOpen: this.handleTableFilterOpen,
        onClose: this.handleCloseTooltip,
        position: "bottomLeft"
      }, React.createElement("div", {
        className: "".concat(prefixCls, "-filter")
      }, filterMultiple ? React.createElement(CheckboxGroup, {
        value: initFilters,
        onChange: function onChange(event, value) {
          return _this2.handleSelect(event, value);
        },
        className: "".concat(prefixCls, "-filter-body")
      }, filters.map(function (item) {
        var label = item.label,
            value = item.value;
        return React.createElement(Checkbox, {
          key: value,
          label: label,
          value: value
        });
      })) : React.createElement(RadioGroup, {
        value: initFilters,
        onChange: function onChange(event, value) {
          return _this2.handleSelect(event, value);
        },
        className: "".concat(prefixCls, "-filter-body")
      }, filters.map(function (item) {
        var label = item.label,
            value = item.value;
        return React.createElement(Radio, {
          key: value,
          label: label,
          value: value
        });
      })), React.createElement("div", {
        className: "".concat(prefixCls, "-filter-footer")
      }, React.createElement(Button, {
        size: "small",
        hollow: true,
        onClick: function onClick() {
          return _this2.handleTableFilterClose('sure');
        }
      }, "\u786E\u5B9A"), React.createElement(Button, {
        size: "small",
        hollow: true,
        onClick: function onClick() {
          return _this2.handleTableFilterClose('reset');
        }
      }, "\u91CD\u7F6E"))));
    }
  }]);

  return TableFilter;
}(React.Component);

_defineProperty(TableFilter, "propTypes", {});

_defineProperty(TableFilter, "defaultProps", {});

export default TableFilter;