function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useState, useEffect } from 'react';
import { locales } from '@pisell/utils';
import { InputNumber } from 'antd';
import { PisellModal } from '@pisell/materials';
import { limitDecimals } from "../../store/utils";
import { useOSPayment } from "../../store";
import { useSymbol } from "../../hooks";
import "./index.less";
var ToBPayCache = function ToBPayCache(props) {
  var open = props.open,
    title = props.title,
    uuid = props.uuid,
    _props$expect_amount = props.expect_amount,
    expect_amount = _props$expect_amount === void 0 ? '' : _props$expect_amount,
    onCancel = props.onCancel,
    _onOk = props.onOk;
  var osPayment = useOSPayment();
  var symbol = useSymbol();
  var _useState = useState(expect_amount),
    _useState2 = _slicedToArray(_useState, 2),
    inputAmount = _useState2[0],
    setInputAmount = _useState2[1];
  var _useState3 = useState(''),
    _useState4 = _slicedToArray(_useState3, 2),
    payDesc = _useState4[0],
    setPayDesc = _useState4[1];
  useEffect(function () {
    setInputAmount(expect_amount);
  }, [expect_amount]);
  useEffect(function () {
    osPayment.getRemainingOrderAmountWithInputAsync(inputAmount, uuid).then(function (res) {
      // 全额支付
      if (Number(res) === 0) {
        setPayDesc(locales.getText('tobPay.text.cash.modal.no.change.given'));
      }
      // 找零
      if (Number(res) < 0) {
        setPayDesc(locales.getText('tobPay.text.cash.modal.amount.pay.change')("".concat(symbol).concat(Math.abs(Number(res)))));
      }
      // 还需支付
      if (Number(res) > 0) {
        setPayDesc(locales.getText('tobPay.text.cash.modal.amount.left.pay.paid')("".concat(symbol).concat(res)));
      }
    });
  }, [inputAmount]);
  return /*#__PURE__*/React.createElement(PisellModal, {
    title: title,
    open: open,
    onCancel: onCancel,
    okButtonProps: {
      disabled: Number(inputAmount) <= 0
    },
    okText: locales.getText('tobPay.text.cash.modal.ok'),
    onOk: function onOk() {
      _onOk(inputAmount);
    },
    destroyOnClose: true
  }, /*#__PURE__*/React.createElement("div", {
    className: "tob-pay-cache"
  }, /*#__PURE__*/React.createElement("div", {
    className: "tob-pay-cache-money"
  }, /*#__PURE__*/React.createElement("div", {
    className: "tob-pay-cache-text"
  }, locales.getText('tobPay.text.cash.modal.amount.toPay')), /*#__PURE__*/React.createElement("div", {
    className: "tob-pay-cache-num"
  }, symbol, Number(expect_amount).toFixed(2))), /*#__PURE__*/React.createElement("div", {
    className: "tob-pay-cache-title"
  }, locales.getText('tobPay.text.cash.modal.input.title')), /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(InputNumber, {
    precision: 2,
    value: inputAmount,
    min: 0,
    formatter: limitDecimals,
    parser: limitDecimals,
    onChange: function onChange(event) {
      if (event !== null && event !== undefined) {
        setInputAmount(event);
      } else {
        setInputAmount('0');
      }
    },
    style: {
      width: '100%'
    }
  })), /*#__PURE__*/React.createElement("div", {
    className: "tob-pay-cache-desc"
  }, payDesc)));
};
export default ToBPayCache;