'use strict';

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

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 _desc, _value, _class, _VIEW$EDIT; /**
                                        * Created by baoyinghai on 11/17/16.
                                        */


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _coreDecorators = require('core-decorators');

var _constant = require('../../constant');

var _validateTypes = require('../../formValidator/validateTypes');

var _validateTypes2 = _interopRequireDefault(_validateTypes);

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

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

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

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

function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
  var desc = {};
  Object['ke' + 'ys'](descriptor).forEach(function (key) {
    desc[key] = descriptor[key];
  });
  desc.enumerable = !!desc.enumerable;
  desc.configurable = !!desc.configurable;

  if ('value' in desc || desc.initializer) {
    desc.writable = true;
  }

  desc = decorators.slice().reverse().reduce(function (desc, decorator) {
    return decorator(target, property, desc) || desc;
  }, desc);

  if (context && desc.initializer !== void 0) {
    desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
    desc.initializer = undefined;
  }

  if (desc.initializer === void 0) {
    Object['define' + 'Property'](target, property, desc);
    desc = null;
  }

  return desc;
}

var MAX = '99999999999'; // 一千亿
var numeral = require('numeral');

var MyInputNumber = (_class = function (_React$Component) {
  _inherits(MyInputNumber, _React$Component);

  function MyInputNumber() {
    _classCallCheck(this, MyInputNumber);

    return _possibleConstructorReturn(this, (MyInputNumber.__proto__ || Object.getPrototypeOf(MyInputNumber)).apply(this, arguments));
  }

  _createClass(MyInputNumber, [{
    key: '_onChange',
    value: function _onChange(e) {
      /* eslint-disable */
      var _props = this.props,
          onChange = _props.onChange,
          max = _props.max,
          format = _props.format,
          magnitude = _props.magnitude,
          min = _props.min;

      var pointNum = this.getPointNum(e.target.value);
      var a = parseInt('1' + '0'.repeat(pointNum));
      if (e && e.target && (isNaN && !isNaN(e.target.value) || e.target.value === '-') && parseFloat(e.target.value && e.target.value !== '-' ? e.target.value : 0) < parseFloat(max) && parseFloat(e.target.value && e.target.value !== '-' ? e.target.value : 0) > parseFloat(min) && this.getPointNum(format) >= pointNum) {
        this.inputValue = e.target.value + '';
        var res = e.target.value === '' ? undefined : numeral(numeral(e.target.value).format(format)).value() * a / (magnitude * a);
        onChange && onChange(res);
      }
    }
  }, {
    key: 'shouldComponentUpdate',
    value: function shouldComponentUpdate(nextProps, nextState) {
      var _props2 = this.props,
          addonBefore = _props2.addonBefore,
          magnitude = _props2.magnitude,
          value = _props2.value,
          format = _props2.format;

      if (nextProps.addonBefore !== addonBefore) {
        // nextProps.value = parseFloat(nextProps.value, 10) ? numeral(magnitude * nextProps.value).format(format) : nextProps.value;
        return true;
      }

      if (nextProps.value === (this.inputValue ? this.inputValue : value)) {
        return false;
      }
      return true;
    }
  }, {
    key: '_onFocus',
    value: function _onFocus(e) {
      if (e.target.value && e.target.value.length > 0) {
        e.target.value = numeral(e.target.value).value();
      }
    }
  }, {
    key: '_onBlur',
    value: function _onBlur(e) {
      if (parseFloat(e.target.value)) {
        e.target.value = numeral(e.target.value).format(this.props.format);
      }
    }
  }, {
    key: 'getPointNum',
    value: function getPointNum(value) {
      return value && value.indexOf('.') !== -1 ? value.substring(value.indexOf('.'), value.length).length : 1;
    }
  }, {
    key: 'render',
    value: function render() {
      var _props3 = this.props,
          value = _props3.value,
          magnitude = _props3.magnitude,
          format = _props3.format,
          addonAfter = _props3.addonAfter,
          addonBefore = _props3.addonBefore,
          disabled = _props3.disabled;

      var renderValue = this.inputValue ? this.inputValue : parseFloat(value, 10) ? numeral(magnitude * value).format(format) : value;
      this.inputValue = null;
      return _react2.default.createElement(_mxa.Input, { value: renderValue, addonBefore: addonBefore || '', addonAfter: addonAfter || '', onFocus: this._onFocus, onBlur: this._onBlur, onChange: this._onChange, disabled: disabled });
    }
  }]);

  return MyInputNumber;
}(_react2.default.Component), (_applyDecoratedDescriptor(_class.prototype, '_onChange', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, '_onChange'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, '_onFocus', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, '_onFocus'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, '_onBlur', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, '_onBlur'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'getPointNum', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, 'getPointNum'), _class.prototype)), _class);


var compRender = function compRender(config) {
  var record = config.record,
      props = config.props;


  var formValidate = record.formValidates && record.formValidates.filter(function (item) {
    return item.validateType === _validateTypes2.default.ISAMOUNT;
  })[0];

  var format = formValidate && formValidate.format;
  var max = formValidate && formValidate.max;
  var min = formValidate && formValidate.min;
  // TODO: min
  if (min || min === 0) {
    var magnitude = record.displayComponent.magnitude;
    if (magnitude) {
      min = magnitude * min;
    }
  } else {
    min = -100000000000;
  }

  if (max || max === 0) {
    var _magnitude = record.displayComponent.magnitude;
    if (_magnitude) {
      max = _magnitude * max;
    }
  } else {
    max = 100000000000;
  }
  return _react2.default.createElement(MyInputNumber, {
    disabled: record.isReadonly || props.isModalInputMappingItem && props.isLeadInto && props.modalInputIsReadonly,
    format: format || '0,0.0000',
    addonAfter: record.displayComponent.addOnAfter,
    addonBefore: record.displayComponent.addOnBefore,
    magnitude: record.displayComponent.magnitude || 1,
    max: max,
    min: min
  });
};

var StaticDisplay = function (_React$Component2) {
  _inherits(StaticDisplay, _React$Component2);

  function StaticDisplay() {
    _classCallCheck(this, StaticDisplay);

    return _possibleConstructorReturn(this, (StaticDisplay.__proto__ || Object.getPrototypeOf(StaticDisplay)).apply(this, arguments));
  }

  _createClass(StaticDisplay, [{
    key: 'render',
    value: function render() {
      var value = this.props.value;
      var format = this.props.format ? this.props.format : '0,0.0000';
      return _react2.default.createElement(
        'span',
        null,
        value && numeral(value).format(format)
      );
    }
  }]);

  return StaticDisplay;
}(_react2.default.Component);

exports.default = (_VIEW$EDIT = {}, _defineProperty(_VIEW$EDIT, _constant.VIEW, function (config) {
  var record = config.record;


  var formValidate = record.formValidates && record.formValidates.filter(function (item) {
    return item.validateType === _validateTypes2.default.ISAMOUNT;
  })[0];

  var format = formValidate && formValidate.format;

  return _react2.default.createElement(StaticDisplay, { format: format });
}), _defineProperty(_VIEW$EDIT, _constant.EDIT, compRender), _VIEW$EDIT);