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 _class, _temp;

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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

// require PrototypeDate.js和PrototypeString.js
import React, { Component, Fragment } from 'react';
import PropTypes from 'prop-types';
import InputText from './../InputText';
import PickerDate from './../PickerDate';

var InputDate = (_temp = _class = function (_Component) {
  _inherits(InputDate, _Component);

  // 全局配置
  function InputDate(props, context) {
    _classCallCheck(this, InputDate);

    var _this = _possibleConstructorReturn(this, _Component.call(this, props, context));

    _this.correctDate = function (val) {
      var _this$context$locale = _this.context.locale,
          locale = _this$context$locale === undefined ? {} : _this$context$locale;
      var _this$props = _this.props,
          type = _this$props.type,
          min = _this$props.min,
          max = _this$props.max,
          fail = _this$props.fail,
          _this$props$pickerPro = _this$props.pickerProps,
          pickerProps = _this$props$pickerPro === undefined ? {} : _this$props$pickerPro;

      var split = pickerProps.split || '-';
      var timeSplit = pickerProps.timeSplit || ':';
      var text = val;
      var selectDate = text.toDate(split, timeSplit);
      var value = _this.$input.value;
      var e = _this.$picker && _this.$picker.instance ? _this.$picker.instance : {};
      e.target = _this.$input;
      if (min && (min.isDateTime(split, timeSplit) || min.isDate(split) || min.isMonth(split) || min.isTime(timeSplit))) {
        if (type === 'date' && selectDate.compareDate(min.toDate(split, timeSplit)) === -1) {
          if (fail) {
            fail({ errMsg: (locale['hint_cannot_be_less_than'] || '不能小于') + min, select: text, min: min, value: value, event: e });
            return false;
          }
          text = min;
        } else if (type === 'month' && selectDate.compareMonth(min.toDate(split, timeSplit)) === -1) {
          if (fail) {
            fail({ errMsg: (locale['hint_cannot_be_less_than'] || '不能小于') + min, select: text, min: min, value: value, event: e });
            return false;
          }
          text = min;
        } else if (type === 'time' && selectDate.compareTime(min.toDate(split, timeSplit)) === -1) {
          if (fail) {
            fail({ errMsg: (locale['hint_cannot_be_less_than'] || '不能小于') + min, select: text, min: min, value: value, event: e });
            return false;
          }
          text = min;
        } else if (type === 'datetime' && selectDate.compareDateTime(min.toDate(split, timeSplit)) === -1) {
          if (fail) {
            fail({ errMsg: (locale['hint_cannot_be_less_than'] || '不能小于') + min, select: text, min: min, value: value, event: e });
            return false;
          }
          text = min;
        }
      }
      if (max && (max.isDateTime(split, timeSplit) || max.isDate(split) || max.isMonth(split) || max.isTime(timeSplit))) {
        if (type === 'date' && selectDate.compareDate(max.toDate(split, timeSplit)) === 1) {
          if (fail) {
            fail({ errMsg: (locale['hint_cannot_be_greater_than'] || '不能大于') + max, select: text, max: max, value: value, event: e });
            return false;
          }
          text = max;
        } else if (type === 'month' && selectDate.compareMonth(max.toDate(split, timeSplit)) === 1) {
          if (fail) {
            fail({ errMsg: (locale['hint_cannot_be_greater_than'] || '不能大于') + max, select: text, max: max, value: value, event: e });
            return false;
          }
          text = max;
        } else if (type === 'time' && selectDate.compareTime(max.toDate(split, timeSplit)) === 1) {
          if (fail) {
            fail({ errMsg: (locale['hint_cannot_be_greater_than'] || '不能大于') + max, select: text, max: max, value: value, event: e });
            return false;
          }
          text = max;
        } else if (type === 'datetime' && selectDate.compareDateTime(max.toDate(split, timeSplit)) === 1) {
          if (fail) {
            fail({ errMsg: (locale['hint_cannot_be_greater_than'] || '不能大于') + max, select: text, max: max, value: value, event: e });
            return false;
          }
          text = max;
        }
      }
      return text;
    };

    _this.onClickInput = function () {
      var onClick = _this.props.onClick;

      if (onClick) onClick.apply(undefined, arguments);
      _this.setState(function (prevState) {
        return {
          show: !prevState.show
        };
      });
    };

    _this.onClickMask = function (e) {
      var _this$props$pickerPro2 = _this.props.pickerProps,
          pickerProps = _this$props$pickerPro2 === undefined ? {} : _this$props$pickerPro2;

      if (pickerProps && pickerProps.maskAttribute && pickerProps.maskAttribute.onClick) {
        e.target = _this.$input;
        pickerProps.maskAttribute.onClick(e);
        return;
      }
      _this.setState(function (prevState) {
        return {
          show: !prevState.show
        };
      });
    };

    _this.onClickSubmit = function (e) {
      var _this$props2 = _this.props,
          onChange = _this$props2.onChange,
          _this$props2$pickerPr = _this$props2.pickerProps,
          pickerProps = _this$props2$pickerPr === undefined ? {} : _this$props2$pickerPr;
      // 获取值

      if (!_this.$input) _this.$input = _this.refs.$ComponentInputText.$input;
      var value = _this.correctDate(e.activeText); // 如果发生错误的话, 将返回false
      if (value === false) return;
      var options = e.activeOptions;
      // 确定按钮回调
      if (pickerProps && pickerProps.submitAttribute && pickerProps.submitAttribute.onClick) {
        e.target = _this.$input;
        pickerProps.submitAttribute.onClick(e, value, options);
        return;
      }
      // 赋值
      if (onChange) {
        e.target = _this.$input;
        onChange(e, value, options);
      }
      // 隐藏框
      _this.setState({
        show: !_this.state.show
      });
    };

    _this.onClickCancel = function (e) {
      var _this$props$pickerPro3 = _this.props.pickerProps,
          pickerProps = _this$props$pickerPro3 === undefined ? {} : _this$props$pickerPro3;

      if (pickerProps && pickerProps.cancelAttribute && pickerProps.cancelAttribute.onClick) {
        e.target = _this.$input;
        pickerProps.cancelAttribute.onClick(e);
        return;
      }
      _this.setState(function (prevState) {
        return {
          show: !prevState.show
        };
      });
    };

    _this.state = {
      show: false
    };
    return _this;
  }

  InputDate.prototype.componentDidMount = function componentDidMount() {
    this.$el = null;
    this.$input = null;
    if (this.refs.$ComponentInputText && this.refs.$ComponentInputText.$el && this.refs.$ComponentInputText.$input) {
      this.$el = this.refs.$ComponentInputText.$el;
      this.$input = this.refs.$ComponentInputText.$input;
      this.$ComponentInputText = this.refs.$ComponentInputText;
    }
    this.$picker = this.refs.$ComponentPicker;
  };
  // 日期纠正

  // 点击文本框

  // 点击遮罩

  // 点击确定按钮

  // 点击取消按钮


  InputDate.prototype.render = function render() {
    var _props = this.props,
        min = _props.min,
        max = _props.max,
        onClick = _props.onClick,
        onChange = _props.onChange,
        fail = _props.fail,
        type = _props.type,
        valueForKey = _props.valueForKey,
        _props$pickerProps = _props.pickerProps,
        pickerProps = _props$pickerProps === undefined ? {} : _props$pickerProps,
        others = _objectWithoutProperties(_props, ['min', 'max', 'onClick', 'onChange', 'fail', 'type', 'valueForKey', 'pickerProps']);

    return React.createElement(
      Fragment,
      null,
      React.createElement(InputText, _extends({ ref: '$ComponentInputText' }, others, { type: 'text', readOnly: true, onClick: this.onClickInput })),
      React.createElement(PickerDate, _extends({
        ref: '$ComponentPicker'
      }, pickerProps, {
        maskAttribute: _extends({}, pickerProps.maskAttribute, {
          onClick: this.onClickMask
        }),
        submitAttribute: _extends({}, pickerProps.submitAttribute, {
          onClick: this.onClickSubmit
        }),
        cancelAttribute: _extends({}, pickerProps.cancelAttribute, {
          onClick: this.onClickCancel
        }),
        valueForKey: valueForKey,
        type: type,
        value: this.$input ? this.$input.value : this.props.value,
        show: pickerProps.show === undefined ? this.state.show : pickerProps.show
      }))
    );
  };

  return InputDate;
}(Component), _class.contextTypes = {
  locale: PropTypes.object,
  portal: PropTypes.object
}, _class.defaultProps = {
  type: 'date'
}, _temp);
export { InputDate as default };
InputDate.propTypes = process.env.NODE_ENV !== "production" ? {
  // Input
  min: PropTypes.string, // YYYY-MM-DD
  max: PropTypes.string, // YYYY-MM-DD

  onClick: PropTypes.func,
  onChange: PropTypes.func,
  fail: PropTypes.func,

  // Picker
  type: PropTypes.string, // 'date | month | time | datetime'
  valueForKey: PropTypes.string,
  pickerProps: PropTypes.object
} : {};