'use strict';

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

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; /**
                    * Created by baoyinghai on 12/27/16.
                    */


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _moment = require('moment');

var _moment2 = _interopRequireDefault(_moment);

var _mxa = require('../mxa');

var _RangeInput = require('./RangeInput');

var _RangeInput2 = _interopRequireDefault(_RangeInput);

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 Option = _mxa.Select.Option;
var RangePicker = _mxa.DatePicker.RangePicker;
var SearchInput = (_temp = _class = function (_React$Component) {
  _inherits(SearchInput, _React$Component);

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

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

    _this._transformDataType = function (data) {
      // if (this.props.dataType === 'Integer' || this.props.dataType === 'Long' || this.props.dataType === 'Double' || this.props.dataType === 'BigDecimal') {
      //  if (!isNaN(data)) {
      //    return Number(data);
      //  }
      // }
      // if (this.props.dataType === 'String') {
      //   return data;
      // }
      return data;
    };

    _this._handleDateChange = function (dates, dateStrings) {
      // dates: [moment, moment], dateStrings: [string, string]
      console.log('_handleDateChange ===> ', dates, dateStrings);
      var value = [_this._handleMomentToDate(dates[0]), _this._handleMomentToDate(dates[1])];
      _this.props.onChange && _this.props.onChange({
        fieldName: _this.props.code,
        value: value
      });
    };

    _this._handleRangeChange = function (left, right) {
      // console.log('_handleRangeChange ===> left: ', left, ', right: ', right);
      _this.props.onChange && _this.props.onChange({
        fieldName: _this.props.code,
        value: [left, right] //  Issue #25
      });
    };

    _this._handleSelectChange = function (value) {
      // console.log('_handleSelectChange ===> ', value);
      _this.props.onChange && _this.props.onChange({
        fieldName: _this.props.code,
        value: value //  Issue #25
      });
    };

    _this._handleInputChange = function (e) {
      var value = _this._transformDataType(e.target.value);
      _this.setState({ value: value });
      _this.props.onChange && _this.props.onChange({
        fieldName: _this.props.code,
        value: value //  Issue #25
      });
    };

    _this._getPlaceHolder = function () {
      return '请输入' + _this.props.title;
    };

    _this._renderInput = function () {
      var prefixCls = _this.props.prefixCls;

      if (_this.props.type === 'DATEPICKER') {
        return _react2.default.createElement(RangePicker, {
          size: _this.props.size,
          onChange: _this._handleDateChange,
          className: prefixCls,
          value: _this.props.value && [(0, _moment2.default)(_this.props.value[0]), (0, _moment2.default)(_this.props.value[1])],
          placeholder: ['开始日期', '结束日期']
        });
      } else if (_this.props.type === 'INPUT' && _this.props.operatorType === 'BETWEEN') {
        return _react2.default.createElement(_RangeInput2.default, {
          placeholder: ['', ''],
          className: prefixCls,
          value: _this.props.value,
          onChange: _this._handleRangeChange
        });
      } else if (_this.props.type === 'SELECT') {
        return _react2.default.createElement(
          _mxa.Select,
          {
            showSearch: true,
            style: { minWidth: '100%', borderRadius: '0px' },
            className: prefixCls,
            value: _this.props.value
            // placeholder="Select a person"
            , optionFilterProp: 'children',
            onChange: _this._handleSelectChange,
            placeholder: _this._getPlaceHolder()
          },
          _this.props.extra.map(function (item) {
            return _react2.default.createElement(
              Option,
              { key: item.code, value: item.code },
              item.value
            );
          })
        );
      }

      return _react2.default.createElement(_mxa.Input, {
        size: _this.props.size,
        className: prefixCls
        // placeholder={this.props.placeholder}
        , value: _this.props.value,
        onChange: _this._handleInputChange,
        placeholder: _this._getPlaceHolder(),
        disabled: _this.props.disabled
      });
    };

    _this.state = {
      fieldName: _this.props.code,
      value: _this.props.value,
      minValue: '',
      maxValue: ''
    };
    return _this;
  }

  _createClass(SearchInput, [{
    key: '_handleMomentToDate',
    value: function _handleMomentToDate(date) {
      if (date && date.toDate) {
        return date.toDate().getTime();
      }
      return date;
    }
  }, {
    key: 'render',
    value: function render() {
      var prefixCls = this.props.prefixCls;

      return _react2.default.createElement(
        'div',
        { className: prefixCls + '-wrapper' },
        _react2.default.createElement(
          'div',
          { className: 'search-input-col search-input-col-label' },
          _react2.default.createElement(
            'span',
            { className: prefixCls + '-title' },
            this.props.title
          )
        ),
        _react2.default.createElement(
          'div',
          { className: 'search-input-col search-input-col-comp' },
          this._renderInput()
        )
      );
    }
  }]);

  return SearchInput;
}(_react2.default.Component), _class.propTypes = {
  prefixCls: _react.PropTypes.string,
  // placeholder: PropTypes.string,
  code: _react.PropTypes.string,
  title: _react.PropTypes.string,
  type: _react.PropTypes.string,
  extra: _react.PropTypes.any,
  operatorType: _react.PropTypes.oneOf(['LIKE', 'BETWEEN', 'EQ']),
  size: _react.PropTypes.oneOf(['small', 'large', 'default']),
  onChange: _react.PropTypes.func
}, _class.defaultProps = {
  prefixCls: 'mx-search-input',
  placeholder: '',
  type: 'INPUT',
  size: 'default'
}, _temp);
exports.default = SearchInput;