function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/**
 * Created by pzf on 18/9/1.
 *
 */
import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { Input } from 'mkui-fd';
import { buildOnSelectData, getFullHots, removeChoosed, addToChoosed, isChoosed, isSelected, removeChoosedOuter, getSearchResult } from './util';
import noop from '../_util/noop';
var Search = Input.Search;

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

  var _super = _createSuper(Panel);

  /** *******************static************************** */

  /** *******************life cycle************************** */
  function Panel(props) {
    var _this;

    _classCallCheck(this, Panel);

    _this = _super.call(this, props);

    _defineProperty(_assertThisInitialized(_this), "searchHandle", function (e) {
      var keywords = e.target.value;

      if (keywords) {
        _this.search(keywords);
      } else {
        _this.setState({
          searchResult: null,
          keywords: ''
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "selectSearchItemHandle", function (item, e) {
      _this.setState({
        searchResult: null,
        keywords: ''
      });

      _this.selectHandle(item, e);
    });

    _defineProperty(_assertThisInitialized(_this), "renderSearchResultItem", function (feeTypeName, keywords) {
      var feeTypeNameArr = feeTypeName.split(keywords);
      return feeTypeNameArr.map(function (item, index) {
        return index < feeTypeNameArr.length - 1 ? /*#__PURE__*/React.createElement(Fragment, null, item, /*#__PURE__*/React.createElement("strong", null, keywords)) : item;
      });
    });

    _this.state = {
      choosed: [],
      searchResult: null,
      keywords: ''
    };
    return _this;
  }
  /** *******************handle************************** */

  /**
   * 选择费用类型选项后触发
   * @param item
   * @param e
   */


  _createClass(Panel, [{
    key: "selectHandle",
    value: function selectHandle(item, e) {
      e.preventDefault();
      e.stopPropagation();
      this.choosed(item);
    }
  }, {
    key: "search",
    value:
    /** *******************inner method************************** */
    function search(keywords) {
      var data = this.props.data;
      var result = getSearchResult(data, keywords);
      this.setState({
        searchResult: result,
        keywords: keywords
      });
    }
    /**
     * 选中费用类型选项
     * @param feeType
     */

  }, {
    key: "choosed",
    value: function choosed(feeType) {
      var _this$props = this.props,
          multiple = _this$props.multiple,
          data = _this$props.data;
      var choosed = this.state.choosed;
      var exsit = isChoosed(choosed, feeType);

      if (exsit) {
        // 取消选择
        if (multiple) {
          removeChoosed(data, choosed, feeType.feeTypeCode);
          this.setChoosedState(choosed);
          this.fireChange(choosed);
        }
      } else {
        if (multiple) {
          addToChoosed(choosed, feeType, multiple, data);
        } else {
          addToChoosed(choosed, feeType, multiple, data);
        }

        this.setChoosedState(choosed);
        this.fireChange(choosed);
      }
    }
  }, {
    key: "setChoosedState",
    value: function setChoosedState(choosed) {
      this.setState({
        choosed: choosed
      });
    }
  }, {
    key: "fireChange",
    value: function fireChange(choosed) {
      var onSelect = this.props.onSelect;

      if (onSelect) {
        onSelect(buildOnSelectData(choosed));
      }
    }
    /**
     *
     * @param item obj | array
     */

  }, {
    key: "removeChoosed",
    value: function removeChoosed(item) {
      var data = this.props.data;
      var choosed = this.state.choosed;
      removeChoosedOuter(data, choosed, item);
    }
    /** *******************render************************** */

  }, {
    key: "renderCategory",
    value: function renderCategory() {
      var _this2 = this;

      var props = this.props;
      var prefixCls = props.prefixCls;
      var categories = props.data;

      if (categories && categories.length) {
        var categoriesEl = categories.map(function (category) {
          return /*#__PURE__*/React.createElement("div", {
            className: classNames("".concat(prefixCls, "-category")),
            key: category.categoryCode
          }, /*#__PURE__*/React.createElement("div", {
            className: classNames("".concat(prefixCls, "-category-name"))
          }, category.categoryName), /*#__PURE__*/React.createElement("div", {
            className: classNames("".concat(prefixCls, "-category-children-wrapper"))
          }, _this2.renderChildren(category)));
        });
        return /*#__PURE__*/React.createElement("div", {
          className: classNames("".concat(prefixCls, "-category-wrapper"))
        }, categoriesEl);
      }

      return '费用类型不存在！';
    }
  }, {
    key: "renderChildren",
    value: function renderChildren(category) {
      var _this3 = this;

      var _this$props2 = this.props,
          prefixCls = _this$props2.prefixCls,
          selected = _this$props2.selected;

      if (category.children && category.children.length) {
        return category.children.map(function (feeType) {
          return /*#__PURE__*/React.createElement("span", {
            key: "".concat(category.categoryCode, "-").concat(feeType.feeTypeCode),
            className: classNames("".concat(prefixCls, "-item"), {
              selected: isSelected(selected, feeType)
            }),
            onClick: function onClick(e) {
              _this3.selectHandle(feeType, e);
            }
          }, feeType.feeTypeName);
        });
      }
    }
  }, {
    key: "renderHot",
    value: function renderHot() {
      var _this4 = this;

      var _this$props3 = this.props,
          prefixCls = _this$props3.prefixCls,
          hot = _this$props3.hot,
          data = _this$props3.data;

      if (hot && hot.length) {
        var fullHots = getFullHots(hot, data);
        var hostsEl = fullHots.map(function (item) {
          return /*#__PURE__*/React.createElement("span", {
            key: "".concat(item.categoryCode, "-").concat(item.feeTypeCode),
            className: classNames("".concat(prefixCls, "-item"), {
              selected: item.selected
            }),
            onClick: function onClick(e) {
              _this4.selectHandle(item, e);
            }
          }, item.feeTypeName);
        });
        return /*#__PURE__*/React.createElement("div", {
          className: classNames("".concat(prefixCls, "-hot-wrapper"))
        }, /*#__PURE__*/React.createElement("div", {
          className: classNames("".concat(prefixCls, "-hot-title"))
        }, "\u5E38\u7528"), /*#__PURE__*/React.createElement("div", {
          className: classNames("".concat(prefixCls, "-hot-content"))
        }, hostsEl));
      }

      return null;
    }
  }, {
    key: "renderContent",
    value: function renderContent() {
      var prefixCls = this.props.prefixCls;
      return /*#__PURE__*/React.createElement("div", {
        className: classNames("".concat(prefixCls, "-wrapper"))
      }, this.renderHot(), this.renderCategory());
    }
  }, {
    key: "renderSearchResult",
    value: function renderSearchResult() {
      var _this5 = this;

      var prefixCls = this.props.prefixCls;
      var _this$state = this.state,
          searchResult = _this$state.searchResult,
          keywords = _this$state.keywords;
      return searchResult && searchResult.length > 0 ? /*#__PURE__*/React.createElement("ul", {
        className: "".concat(prefixCls, "-search-result")
      }, searchResult.map(function (item) {
        return /*#__PURE__*/React.createElement("li", {
          key: item.feeTypeCode,
          onClick: function onClick(e) {
            _this5.selectSearchItemHandle(item, e);
          }
        }, _this5.renderSearchResultItem(item.feeTypeName, keywords));
      })) : /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-search-empty")
      }, "\u6CA1\u6709\u76F8\u5173\u7ED3\u679C");
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props4 = this.props,
          prefixCls = _this$props4.prefixCls,
          searchPlaceholder = _this$props4.searchPlaceholder;
      var _this$state2 = this.state,
          searchResult = _this$state2.searchResult,
          keywords = _this$state2.keywords;
      return /*#__PURE__*/React.createElement("div", {
        className: classNames("".concat(prefixCls))
      }, /*#__PURE__*/React.createElement("div", {
        className: "".concat(prefixCls, "-search")
      }, /*#__PURE__*/React.createElement(Search, {
        value: keywords,
        className: "".concat(prefixCls, "-search-input"),
        placeholder: searchPlaceholder,
        onChange: this.searchHandle
      })), searchResult ? this.renderSearchResult() : this.renderContent());
    }
  }]);

  return Panel;
}(React.Component);

_defineProperty(Panel, "propTypes", {
  data: PropTypes.array,
  hot: PropTypes.array,
  prefixCls: PropTypes.string,
  onSelect: PropTypes.func,
  multiple: PropTypes.bool,
  selected: PropTypes.array,
  searchPlaceholder: PropTypes.string
});

_defineProperty(Panel, "defaultProps", {
  data: [],
  hot: [],
  prefixCls: 'mkui-fee-type-panel',
  onSelect: noop,
  multiple: false,
  selected: [],
  searchPlaceholder: '请输入关键字搜索...'
});

export default Panel;
//# sourceMappingURL=Panel.js.map
