'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = undefined;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _class, _temp, _initialiseProps; /**
                                      * Created by baoyinghai on 1/4/17.
                                      */


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _IconHelper = require('../../framework/utils/IconHelper');

var _CacheService = require('../../framework/service/CacheService');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

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

// 模糊查询  条件之间是或关系
var Search = (_temp = _class = function (_React$Component) {
  _inherits(Search, _React$Component);

  function Search(props) {
    _classCallCheck(this, Search);

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

    _initialiseProps.call(_this);

    var dataSource = _this.props.dataSource;
    var title = [];
    var filterItems = [];
    dataSource.forEach(function (item) {
      title.push(item.displayValue);
      filterItems.push({ fieldName: item.name });
    });
    _this.searchValue = undefined;
    _this.state = {
      title: title,
      filterItems: filterItems,
      value: ''
    };
    return _this;
  }

  _createClass(Search, [{
    key: 'componentDidMount',
    value: function componentDidMount() {}
  }, {
    key: 'render',


    /* eslint-disable */
    value: function render() {
      var _this2 = this;

      var eventHandler = {
        onMouseOut: function onMouseOut(e) {
          var wrapperDiv = e.currentTarget;
          var inputEle = wrapperDiv.childNodes[0];
          var icon = wrapperDiv.childNodes[1];
          _this2._setBlurStyle(wrapperDiv, inputEle, icon);
        },
        onMouseOver: function onMouseOver(e) {
          var wrapperDiv = e.currentTarget;
          var inputEle = wrapperDiv.childNodes[0];
          var icon = wrapperDiv.childNodes[1];
          _this2._setFocusStyle(wrapperDiv, inputEle, icon);
        }
      };
      return _react2.default.createElement(
        'div',
        _extends({}, eventHandler, { className: 'simple-modal-search-bar' }),
        _react2.default.createElement('input', { type: 'text', onChange: this._onChange, placeholder: this._getPlaceHolder(), onBlur: this._inputBlur, onKeyDown: this._onKeyDown }),
        (0, _IconHelper.iconWrapper)('icon-fangdajing', null, null, { onClick: this._onSearch })
      );
    }
  }]);

  return Search;
}(_react2.default.Component), _class.propTypes = {
  dataSource: _react.PropTypes.array,
  onSearch: _react.PropTypes.func
}, _class.defaultProps = {
  dataSource: []
}, _initialiseProps = function _initialiseProps() {
  var _this3 = this;

  this._onChange = function (e) {
    var value = e.target.value;
    var filterItems = _this3.state.filterItems;

    var updated = filterItems.map(function (item) {
      item.value = value;
      return item;
    });
    _this3.setState({ filterItems: updated, value: value });
  };

  this._onSearch = function () {
    if (!_this3.state.filterItems || _this3.state.filterItems.length === 0) return;
    var searchValue = _this3.state.filterItems[0].value;
    if (searchValue === _this3.searchValue) return;
    _this3.searchValue = searchValue;
    _this3.props.onSearch && _this3.props.onSearch(_this3.state.filterItems);
  };

  this._getPlaceHolder = function () {
    return '请输入' + _this3.state.title.join('/');
  };

  this._inputBlur = function (e) {
    var wrapperDiv = e.currentTarget.parentNode;
    var inputEle = e.currentTarget;
    var icon = wrapperDiv.childNodes[1];
    _this3._setBlurStyle(wrapperDiv, inputEle, icon);
  };

  this._setFocusStyle = function (wrapperDiv, inputEle, icon) {
    // const searchInput = document.querySelector('.simple-modal-search-bar input');
    if (!_this3.inputWidth) {
      _this3.inputWidth = 14 * _this3._getPlaceHolder().length + 'px';
      inputEle.style.maxWidth = '400px';
    }
    inputEle.style.width = _this3.inputWidth;
    // inputEle.style.width = '200px';
    inputEle.focus();
    if (wrapperDiv) wrapperDiv.style.border = '1px solid #108ee9';
    if (icon) icon.style.color = '#108ee9';
  };

  this._setBlurStyle = function (wrapperDiv, inputEle, icon) {
    if (!inputEle.value) {
      inputEle.style.width = '0px';
      wrapperDiv.style.border = '';
      if (icon) icon.style.color = '';
    }
  };

  this._onKeyDown = function (e) {
    if (e.keyCode === 13) {
      console.log('keyCode === 13');
      if (parseInt(e.currentTarget.style.width, 10) > 0) {
        _this3._onSearch();
      }
    }
  };
}, _temp);
exports.default = Search;