function _typeof(obj) { "@babel/helpers - typeof"; 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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

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 _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 _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

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

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

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

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 from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { Decimal } from 'decimal.js';
import { InputNumber, Icon, Button } from 'mkui-fd';
import { CurrencyInput } from 'mkui-ext';
import CurrencyDisabledView from './CurrencyDisabledView';

var MultipleCurrencyAdjustAmount = /*#__PURE__*/function (_React$Component) {
  _inherits(MultipleCurrencyAdjustAmount, _React$Component);

  var _super = _createSuper(MultipleCurrencyAdjustAmount);

  /** *******************static************************** */

  /** *******************life cycle************************** */
  function MultipleCurrencyAdjustAmount(props) {
    var _this;

    _classCallCheck(this, MultipleCurrencyAdjustAmount);

    _this = _super.call(this, props);

    _defineProperty(_assertThisInitialized(_this), "mathSignChangeHandle", function () {
      var mathSign = _this.state.mathSign;
      var _this$data = _this.data,
          amount = _this$data.amount,
          adjustAmount = _this$data.adjustAmount;
      var precision = _this.props.precision;
      var number = 0;

      if (mathSign === 'balance') {
        number = parseFloat(new Decimal(amount).add(new Decimal(adjustAmount))).toFixed(precision);

        _this.setState({
          mathSign: 'plus'
        });
      } else {
        number = parseFloat(new Decimal(amount).minus(new Decimal(adjustAmount))).toFixed(precision);

        _this.setState({
          mathSign: 'balance'
        });
      }

      _this._setAmount(adjustAmount, number);
    });

    _defineProperty(_assertThisInitialized(_this), "adjustAmountChangeHandle", function (value) {
      if (isNaN(value)) return false;

      var number = _this._countAmount(value, 'adjust');

      _this._setAmount(value, number);
    });

    _defineProperty(_assertThisInitialized(_this), "adjustedAmountChangeHandle", function (value) {
      if (isNaN(value)) return false;

      var number = _this._countAmount(value, 'adjusted');

      _this._setAmount(number, value);
    });

    _defineProperty(_assertThisInitialized(_this), "translateCurrency", function (value) {
      var _this$props = _this.props,
          baseCcyCode = _this$props.baseCcyCode,
          orgCcyCode = _this$props.orgCcyCode,
          baseAmount = _this$props.baseAmount,
          orgAmount = _this$props.orgAmount;
      var _this$data2 = _this.data,
          orgAdjustAmount = _this$data2.orgAdjustAmount,
          orgAdjustedAmount = _this$data2.orgAdjustedAmount,
          baseAdjustAmount = _this$data2.baseAdjustAmount,
          baseAdjustedAmount = _this$data2.baseAdjustedAmount,
          isBaseCcy = _this$data2.isBaseCcy;

      if (!isBaseCcy) {
        _this.setState({
          currencyText: '切换成消费金额',
          adjustAmount: baseAdjustAmount,
          adjustedAmount: baseAdjustedAmount
        });

        _this.data.ccyCode = baseCcyCode;
        _this.data.amount = baseAmount;
      } else {
        _this.setState({
          currencyText: '切换成收款金额',
          adjustAmount: orgAdjustAmount
        });

        _this.data.ccyCode = orgCcyCode;
        _this.data.amount = orgAmount;
      }

      _this.data.isBaseCcy = !_this.data.isBaseCcy;
    });

    _this.state = _objectSpread(_objectSpread({}, _this.props), {}, {
      mathSign: 'balance',
      amount: '',
      ccyCode: '￥'
    });

    _this._initState();

    return _this;
  }
  /** *******************handle************************** */


  _createClass(MultipleCurrencyAdjustAmount, [{
    key: "_initState",
    value:
    /** *******************inner method************************** */
    function _initState() {
      var _this$props$value = this.props.value,
          orgAdjustedAmount = _this$props$value.orgAdjustedAmount,
          baseAdjustedAmount = _this$props$value.baseAdjustedAmount;
      var _this$props2 = this.props,
          orgCcyCode = _this$props2.orgCcyCode,
          orgAmount = _this$props2.orgAmount,
          exchangeRate = _this$props2.exchangeRate,
          precision = _this$props2.precision;
      this.data = {};
      var adjustAmount, mathSign, baseAdjustAmount;

      if (orgAdjustedAmount) {
        adjustAmount = parseFloat(new Decimal(orgAmount).minus(new Decimal(orgAdjustedAmount))).toFixed(precision);
        baseAdjustAmount = parseFloat(new Decimal(adjustAmount).mul(new Decimal(exchangeRate))).toFixed(precision);

        if (orgAmount >= orgAdjustedAmount) {
          mathSign = 'balance';
        } else {
          mathSign = 'plus';
        }
      }

      this.data = {
        isBaseCcy: false,
        ccyCode: orgCcyCode,
        amount: orgAmount,
        orgAdjustAmount: adjustAmount,
        orgAdjustedAmount: orgAdjustedAmount,
        baseAdjustAmount: baseAdjustAmount,
        baseAdjustedAmount: baseAdjustedAmount,
        adjustAmount: adjustAmount
      };
      this.state = {
        mathSign: mathSign,
        currencyText: '切换成收款金额'
      };
    }
  }, {
    key: "_countAmount",
    value: function _countAmount(value, type) {
      var mathSign = this.state.mathSign;
      var amount = this.data.amount;
      var precision = this.props.precision;
      var number = 0;

      if (type === 'adjust') {
        if (mathSign === 'balance') {
          number = parseFloat(new Decimal(amount).minus(new Decimal(value))).toFixed(precision);
        } else {
          number = parseFloat(new Decimal(amount).add(new Decimal(value))).toFixed(precision);
        }
      } else {
        if (mathSign === 'plus') {
          number = parseFloat(new Decimal(value).minus(new Decimal(amount))).toFixed(precision);
        } else {
          number = parseFloat(new Decimal(amount).minus(new Decimal(value))).toFixed(precision);
        }
      }

      return number;
    }
  }, {
    key: "_setAmount",
    value: function _setAmount(val1, val2) {
      var _this$data3 = this.data,
          isBaseCcy = _this$data3.isBaseCcy,
          ccyCode = _this$data3.ccyCode;
      var _this$props3 = this.props,
          exchangeRate = _this$props3.exchangeRate,
          precision = _this$props3.precision;
      var adjustAmount, adjustedAmount, baseAdjustAmount, baseAdjustedAmount, amount;

      if (!isBaseCcy) {
        amount = this.props.orgAmount;
        adjustAmount = val1;
        adjustedAmount = val2;
        baseAdjustAmount = parseFloat(new Decimal(val1).mul(new Decimal(exchangeRate))).toFixed(precision);
        baseAdjustedAmount = parseFloat(new Decimal(val2).mul(new Decimal(exchangeRate))).toFixed(precision);
      } else {
        amount = this.props.baseAmount;
        adjustAmount = parseFloat(new Decimal(val1).div(new Decimal(exchangeRate))).toFixed(precision);
        adjustedAmount = parseFloat(new Decimal(val2).div(new Decimal(exchangeRate))).toFixed(precision);
        baseAdjustAmount = val1;
        baseAdjustedAmount = val2;
      }

      this.data = {
        adjustAmount: val1,
        ccyCode: ccyCode,
        amount: amount,
        orgAdjustAmount: adjustAmount,
        orgAdjustedAmount: adjustedAmount,
        baseAdjustAmount: baseAdjustAmount,
        baseAdjustedAmount: baseAdjustedAmount,
        isBaseCcy: isBaseCcy
      };
      this.props.onChange({
        orgAdjustedAmount: adjustedAmount,
        baseAdjustedAmount: baseAdjustedAmount
      });
    }
  }, {
    key: "formatCcyCode",
    value: function formatCcyCode(ccyCode) {
      return ccyCode === 'CNY' ? '￥' : ccyCode;
    }
    /** *******************render************************** */

  }, {
    key: "render",
    value: function render() {
      var _this$props4 = this.props,
          disabled = _this$props4.disabled,
          precision = _this$props4.precision,
          min = _this$props4.min,
          max = _this$props4.max;
      var _this$state = this.state,
          mathSign = _this$state.mathSign,
          currencyText = _this$state.currencyText;
      var _this$data4 = this.data,
          amount = _this$data4.amount,
          ccyCode = _this$data4.ccyCode;
      var adjustedAmount;

      if (this.data.isBaseCcy) {
        adjustedAmount = this.props.value.baseAdjustedAmount;
      } else {
        adjustedAmount = this.props.value.orgAdjustedAmount;
      }

      if (mathSign === 'balance') {
        this.data.adjustAmount = parseFloat(new Decimal(amount).minus(new Decimal(adjustedAmount))).toFixed(precision);
      } else {
        this.data.adjustAmount = parseFloat(new Decimal(adjustedAmount).minus(new Decimal(amount))).toFixed(precision);
      }

      var adjustAmount = this.data.adjustAmount;
      return /*#__PURE__*/React.createElement("div", {
        className: "mkui-adjust-amount"
      }, !disabled && /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
        className: "adjust-item"
      }, /*#__PURE__*/React.createElement("div", {
        className: "item-control-warpper"
      }, /*#__PURE__*/React.createElement(CurrencyInput, {
        precision: precision,
        value: amount,
        disabled: true,
        size: "large",
        symbol: this.formatCcyCode(ccyCode)
      }))), /*#__PURE__*/React.createElement("div", {
        className: "math-sign"
      }, /*#__PURE__*/React.createElement(Button, {
        onClick: this.mathSignChangeHandle
      }, /*#__PURE__*/React.createElement(Icon, {
        type: mathSign,
        theme: "outlined"
      }))), /*#__PURE__*/React.createElement("div", {
        className: "adjust-item"
      }, /*#__PURE__*/React.createElement("div", {
        className: "item-control-warpper"
      }, /*#__PURE__*/React.createElement(InputNumber, {
        value: adjustAmount,
        onChange: this.adjustAmountChangeHandle,
        theme: "surround",
        precision: precision,
        placeholder: "\u8BF7\u8F93\u5165\u91D1\u989D",
        min: min,
        max: max
      }))), /*#__PURE__*/React.createElement("div", {
        className: "math-sign"
      }, /*#__PURE__*/React.createElement(Icon, {
        type: "right",
        theme: "outlined"
      })), /*#__PURE__*/React.createElement("div", {
        className: "adjust-item"
      }, /*#__PURE__*/React.createElement("div", {
        className: "item-control-warpper"
      }, /*#__PURE__*/React.createElement(InputNumber, {
        value: adjustedAmount,
        onChange: this.adjustedAmountChangeHandle,
        theme: "surround",
        precision: precision,
        placeholder: "\u8BF7\u8F93\u5165\u91D1\u989D",
        min: min,
        max: max
      }))), /*#__PURE__*/React.createElement("div", {
        className: "math-sign",
        onClick: this.translateCurrency
      }, /*#__PURE__*/React.createElement(Icon, {
        className: "sign",
        type: "transfer",
        theme: "outlined"
      }), /*#__PURE__*/React.createElement("span", {
        className: "sign-explain"
      }, currencyText))), disabled && /*#__PURE__*/React.createElement(CurrencyDisabledView, {
        data: this.props
      }));
    }
  }]);

  return MultipleCurrencyAdjustAmount;
}(React.Component);

_defineProperty(MultipleCurrencyAdjustAmount, "propTypes", {
  prefixCls: PropTypes.string,
  disabled: PropTypes.bool,
  value: PropTypes.object,
  orgAmount: PropTypes.number,
  orgCcyCode: PropTypes.string,
  baseAmount: PropTypes.number,
  baseCcyCode: PropTypes.string,
  exchangeRate: PropTypes.number,
  // 汇率
  onChange: PropTypes.func
});

_defineProperty(MultipleCurrencyAdjustAmount, "defaultProps", {
  prefixCls: 'mkui-adjust-amount',
  value: {},
  orgAmount: 0,
  orgCcyCode: '',
  baseAmount: 0,
  baseCcyCode: '',
  exchangeRate: 0,
  // 汇率
  precision: 2,
  max: Infinity,
  min: -Infinity,
  disabled: false
});

export default MultipleCurrencyAdjustAmount;
//# sourceMappingURL=MultipleCurrencyAdjustAmount.js.map
