import "antd-mobile/es/search-bar/style";
import _SearchBar from "antd-mobile/es/search-bar";
import "antd-mobile/es/list/style";
import _List from "antd-mobile/es/list";

function _typeof(obj) { 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 _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

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

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

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 _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; }

import React, { PureComponent, Component } from 'react';
import PropTypes from 'prop-types';
var Item = _List.Item;
var prefixCls = 'eh-select-project';

var SelectProject =
/*#__PURE__*/
function (_ref) {
  _inherits(SelectProject, _ref);

  function SelectProject() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, SelectProject);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(SelectProject)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "state", {
      value: '',
      focus: false
    });

    _defineProperty(_assertThisInitialized(_this), "onChange", function (value) {
      _this.setState({
        value: value
      });
    });

    _defineProperty(_assertThisInitialized(_this), "onFocus", function () {
      _this.setState({
        focus: true
      });
    });

    _defineProperty(_assertThisInitialized(_this), "onBlur", function () {
      _this.setState({
        focus: false
      });
    });

    _defineProperty(_assertThisInitialized(_this), "getContent", function () {
      var _this$state = _this.state,
          value = _this$state.value,
          focus = _this$state.focus;
      var _this$props = _this.props,
          projectList = _this$props.projectList,
          nameKey = _this$props.nameKey,
          valueKey = _this$props.valueKey;
      var dom = null;
      var reg = /^\s*$/; // 搜索框失去焦点，显示所有数据

      if (!focus) {
        dom = React.createElement(_List, null, projectList.map(function () {
          var project = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
          return React.createElement(Item, {
            key: project[valueKey],
            onClick: function onClick() {
              return _this.saveData(project);
            }
          }, project[nameKey] || '');
        }));
      } // 搜索框有焦点
      else {
          // 没有搜索内容，显示默认界面
          if (reg.test(value)) {
            dom = React.createElement("div", {
              className: "".concat(prefixCls, "-text")
            }, "\u8BF7\u8F93\u5165\u5173\u952E\u5B57\u67E5\u8BE2");
          } else {
            var searchProjects = [];

            for (var i = 0; i < projectList.length; i++) {
              // const { projectName = '' } = projectList[i];
              if (projectList[i][nameKey].indexOf(value) !== -1) {
                searchProjects.push(projectList[i]);
              }
            }

            if (searchProjects.length > 0) {
              dom = React.createElement(_List, null, searchProjects.map(function () {
                var project = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
                return React.createElement(Item, {
                  key: project[valueKey],
                  onClick: function onClick() {
                    return _this.saveData(project);
                  }
                }, project[nameKey] || '');
              }));
            } else {
              dom = React.createElement("div", {
                className: "".concat(prefixCls, "-text")
              }, "\u627E\u4E0D\u5230\u66F4\u591A\u7ED3\u679C");
            }
          }
        }

      return dom;
    });

    _defineProperty(_assertThisInitialized(_this), "saveData", function (project) {
      var onChose = _this.props.onChose;
      sessionStorage.setItem('selectedManagementProject', JSON.stringify(project));
      onChose && onChose();
    });

    return _this;
  }

  _createClass(SelectProject, [{
    key: "render",
    value: function render() {
      var value = this.state.value;
      return React.createElement("div", {
        className: prefixCls
      }, React.createElement(_SearchBar, {
        value: value,
        placeholder: "\u641C\u7D22",
        onChange: this.onChange,
        onBlur: this.onBlur,
        onFocus: this.onFocus
      }), React.createElement("div", {
        className: "".concat(prefixCls, "-content-wrap")
      }, this.getContent()));
    }
  }]);

  return SelectProject;
}(PureComponent || Component);

SelectProject.propTypes = {
  projectList: PropTypes.array,
  onChose: PropTypes.func,
  // name的key值，决定从数据中获取哪个字段作为name，默认为name
  nameKey: PropTypes.string,
  // value的key值，决定从数据中获取哪个字段作为value，默认为id
  valueKey: PropTypes.string
};
SelectProject.defaultProps = {
  nameKey: 'projectName',
  valueKey: 'projectId'
};
export default SelectProject;