function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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 { locales } from '@pisell/utils';
import { Decimal } from 'decimal.js';
import { Button, InputNumber } from 'antd';
import React, { useMemo, useEffect, useState } from 'react';
import usePrefix from "../../../../hooks/usePrefix";
import { ModeEnum } from "../../const";
import { useContextData, useDispatch, updateState } from "../../store";
import { limitDecimals } from "../../helper";
import payLog from "../../utils/payLog";
import "./index.less";

/**
 * @title: 结果标记页面
 * @Author: hongbing.wang
 * @Date: 2024-08-20 11:46
 */
export default (function (_ref) {
  var onChange = _ref.onChange,
    _ref$className = _ref.className,
    className = _ref$className === void 0 ? '' : _ref$className,
    formatAmount = _ref.formatAmount;
  var dispatch = useDispatch();
  var _useContextData = useContextData(),
    symbol = _useContextData.symbol,
    manual = _useContextData.manual,
    order_id = _useContextData.order_id,
    mode = _useContextData.mode,
    device = _useContextData.device,
    number = _useContextData.number,
    pay = _useContextData.pay,
    amount = _useContextData.amount;
  useEffect(function () {
    payLog.log('手动标记页展示');
  }, []);
  var start = useMemo(function () {
    if (typeof (manual === null || manual === void 0 ? void 0 : manual.money) === 'number' || typeof (manual === null || manual === void 0 ? void 0 : manual.money) === 'string') {
      return Number(manual === null || manual === void 0 ? void 0 : manual.money);
    }
    if (typeof (pay === null || pay === void 0 ? void 0 : pay.total) === 'number' || typeof (pay === null || pay === void 0 ? void 0 : pay.total) === 'string') {
      return Number(pay === null || pay === void 0 ? void 0 : pay.total);
    }
    return amount;
  }, [manual === null || manual === void 0 ? void 0 : manual.money, pay === null || pay === void 0 ? void 0 : pay.total, amount]);
  var _useState = useState(start),
    _useState2 = _slicedToArray(_useState, 2),
    money = _useState2[0],
    setMoney = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    disabled = _useState4[0],
    setDisabled = _useState4[1];
  var prefix = usePrefix();
  var _useState5 = useState(manual === null || manual === void 0 ? void 0 : manual.status),
    _useState6 = _slicedToArray(_useState5, 2),
    isUpdateAmount = _useState6[0],
    updateAmountStatus = _useState6[1];

  /**
   * @title: 标记成功
   * @Author: hongbing.wang
   * @Date: 2024-08-20 11:48
   */
  var onSuccess = function onSuccess() {
    if (disabled) {
      payLog.log('手动标记：点击成功被拦截');
      return;
    }
    // if (mode === ModeEnum.Pay) {
    //   setDisabled(true);
    //   const obj = pay || {};
    //   onChange('success', {
    //     ...obj,
    //     number,
    //     device,
    //     order_id,
    //   } as { [key: string]: unknown });
    //   return;
    // }
    if (mode === ModeEnum.Refund) {
      payLog.log('手动标记(退款)：点击标记为成功');
      setDisabled(true);
      onChange('success', {
        amount: amount,
        total: amount,
        number: number,
        uniquePaymentNumber: number,
        device: device,
        order_id: order_id
      });
      return;
    }
    payLog.log('手动标记：点击标记为成功');
    updateAmountStatus(true);
    dispatch(updateState({
      manual: _objectSpread(_objectSpread({}, manual), {}, {
        status: true
      })
    }));
  };

  /**
   * @title: 标记失败
   * @Author: hongbing.wang
   * @Date: 2024-08-20 11:49
   */
  var onFail = function onFail() {
    if (disabled) {
      return;
    }
    payLog.log('手动标记：点击标记为失败');
    setDisabled(true);
    onChange('fail', {
      number: number,
      uniquePaymentNumber: number,
      device: device,
      order_id: order_id
    });
  };
  var onSubmit = function onSubmit() {
    if (disabled) {
      return;
    }
    payLog.log('手动标记-确认金额：点击确认', {
      money: money
    });
    setDisabled(true);
    if (mode === ModeEnum.Refund || mode === ModeEnum.Query) {
      return;
    }
    var prevMoneyDeci = new Decimal((pay === null || pay === void 0 ? void 0 : pay.total) || 0);
    var moneyDeci = new Decimal(Number(money || 0));

    // 手续费-固定金额
    var surMoneyDeci = new Decimal(Number((pay === null || pay === void 0 ? void 0 : pay.surMoney) || 0));

    // 手续费-比例金额
    var surchargeDeci = new Decimal(Number(pay === null || pay === void 0 ? void 0 : pay.surcharge));
    var diff = moneyDeci.sub(prevMoneyDeci);
    var flag = diff.comparedTo(0);
    if (flag < 0) {
      setDisabled(false);
      return;
    }
    if (flag > 0) {
      surMoneyDeci = surMoneyDeci.add(diff);
      surchargeDeci = surchargeDeci.add(diff);
    }
    var obj = pay || {};
    onChange('success', _objectSpread(_objectSpread({}, obj), {}, {
      surMoney: surMoneyDeci.toFixed(2, Decimal.ROUND_HALF_UP),
      surcharge: surchargeDeci.toFixed(2, Decimal.ROUND_HALF_UP),
      total: moneyDeci.toFixed(2, Decimal.ROUND_HALF_UP),
      totalUnit: formatAmount(moneyDeci.toFixed(2, Decimal.ROUND_HALF_UP), 2, symbol),
      number: number,
      uniquePaymentNumber: number,
      device: device,
      order_id: order_id
    }));
    return;
  };
  var onCancel = function onCancel() {
    payLog.log('手动标记-确认金额：点击取消');
    updateAmountStatus(false);
    setMoney(pay === null || pay === void 0 ? void 0 : pay.total);
    dispatch(updateState({
      manual: _objectSpread(_objectSpread({}, manual), {}, {
        status: true,
        money: pay === null || pay === void 0 ? void 0 : pay.total
      })
    }));
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-manual ".concat(className)
  }, isUpdateAmount ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-manual-edit-title"
  }, locales.getText('page.eftpos-pay.text.manual.title')), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-manual-edit-body"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-manual-edit-text"
  }, locales.getText('page.eftpos-pay.text.manual.subtitle')(formatAmount(pay === null || pay === void 0 ? void 0 : pay.total, 2, symbol)), ' ', /*#__PURE__*/React.createElement("br", null), locales.getText('page.eftpos-pay.text.manual.confirm'), /*#__PURE__*/React.createElement("br", null), locales.getText('page.eftpos-pay.text.manual.diff')), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-manual-edit-act"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-manual-edit-text"
  }, locales.getText('page.eftpos-pay.text.manual.tip')), /*#__PURE__*/React.createElement(InputNumber, {
    type: "number",
    addonBefore: symbol || '$',
    min: pay === null || pay === void 0 ? void 0 : pay.total,
    precision: 2,
    style: {
      width: '100%'
    },
    formatter: limitDecimals,
    parser: limitDecimals,
    defaultValue: money,
    onChange: function onChange(event) {
      if (event !== null && event !== undefined) {
        setMoney(event || 0);
        dispatch(updateState({
          manual: _objectSpread(_objectSpread({}, manual), {}, {
            money: event || 0
          })
        }));
      } else {
        setMoney(0);
        dispatch(updateState({
          manual: _objectSpread(_objectSpread({}, manual), {}, {
            money: 0
          })
        }));
      }
    }
  }))), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-step-item-btns"
  }, /*#__PURE__*/React.createElement(Button, {
    className: "pisell-lowcode__eft-pay-manual-btn",
    block: true,
    onClick: onCancel
  }, locales.getText('page.eftpos-pay.text.cancel')), /*#__PURE__*/React.createElement(Button, {
    className: "pisell-lowcode__eft-pay-manual-btn",
    block: true,
    type: "primary",
    onClick: onSubmit
  }, locales.getText('page.eftpos-pay.text.manual.btn.confirm')(formatAmount(money, 2, symbol))))) : /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-manual-title"
  }, manual === null || manual === void 0 ? void 0 : manual.title), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-manual-txt"
  }, manual === null || manual === void 0 ? void 0 : manual.text), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-manual-card-box"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-manual-card",
    onClick: onSuccess
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-manual-img",
    style: {
      backgroundImage: "url(".concat(prefix, "/pay_success.png)")
    }
  }), /*#__PURE__*/React.createElement(Button, {
    className: "pisell-lowcode__eft-pay-manual-btn",
    block: true
  }, locales.getText('page.eftpos-pay.text.successful')(mode))), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-manual-card",
    onClick: onFail
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-manual-img",
    style: {
      backgroundImage: "url(".concat(prefix, "/pay_error.png)")
    }
  }), /*#__PURE__*/React.createElement(Button, {
    className: "pisell-lowcode__eft-pay-manual-btn",
    block: true
  }, locales.getText('page.eftpos-pay.text.failed')(mode))))));
});