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 React, { useState } from 'react';
import classnames from 'classnames';
import { formatAmount } from '@pisell/utils';
import { Iconfont } from '@pisell/materials';
import { Button } from 'antd';
import { PaymentKeyboard } from "../../../../pro/priceKeyboard";
import "./index.less";

// 扩展支付方法接口，添加手续费相关属性

var PaymentMethodItem = function PaymentMethodItem(_ref) {
  var method = _ref.method,
    amountSymbol = _ref.amountSymbol,
    customAmount = _ref.customAmount,
    _ref$disabled = _ref.disabled,
    disabled = _ref$disabled === void 0 ? false : _ref$disabled,
    onClick = _ref.onClick,
    getText = _ref.getText;
  // 控制键盘弹窗显示状态
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isKeyboardVisible = _useState2[0],
    setIsKeyboardVisible = _useState2[1];
  // 计算Surcharge金额
  var calculateSurcharge = function calculateSurcharge(method) {
    if (!(method !== null && method !== void 0 && method.is_surcharge)) return 0;
    var baseAmount = Number(customAmount) || 0;
    if (Number(method.percentage)) {
      return baseAmount * Number(method.percentage) + Number(method.fixed || 0);
    }
    return Number(method.fixed || 0);
  };

  // 计算总金额（包含Surcharge）
  var calculateTotalAmount = function calculateTotalAmount(method) {
    var baseAmount = Number(customAmount) || 0;
    var surcharge = calculateSurcharge(method);
    return baseAmount + surcharge;
  };

  // 处理右侧按钮点击事件
  var handleRightButtonClick = function handleRightButtonClick(e) {
    if (!isEftpos) return;
    e.preventDefault();
    // e.stopPropagation(); // 防止事件冒泡到外层按钮
    if (disabled || !onClick) {
      return;
    }
    onClick(_objectSpread(_objectSpread({}, method), {}, {
      surcharge: String(formatAmount(calculateSurcharge(method), 2, amountSymbol))
    }));
  };

  // 渲染支付图标
  var renderPaymentIcon = function renderPaymentIcon(method) {
    if (method.code === 'EFTPOS') {
      return /*#__PURE__*/React.createElement("div", {
        className: classnames('payment-options-module-icon-container'),
        style: {
          background: '#FE8279'
        }
      }, /*#__PURE__*/React.createElement(Iconfont, {
        type: "pisell2-credit",
        style: {
          fontSize: '26px',
          color: '#ffffff'
        }
      }));
    }
    switch (String(method.type)) {
      case 'credit-card':
      case 'eftpos':
        return /*#__PURE__*/React.createElement("div", {
          className: classnames('payment-options-module-icon-container'),
          style: {
            background: '#FE8279'
          }
        }, /*#__PURE__*/React.createElement(Iconfont, {
          type: "pisell2-credit",
          style: {
            fontSize: '26px',
            color: '#ffffff'
          }
        }));
      default:
        return /*#__PURE__*/React.createElement("div", {
          className: classnames('payment-options-module-icon-container'),
          style: {
            background: '#D277E4'
          }
        }, /*#__PURE__*/React.createElement(Iconfont, {
          type: "pisell2-custom",
          style: {
            fontSize: '26px',
            color: '#ffffff'
          }
        }));
    }
  };

  // 判断是否为EFTPOS支付方式
  var isEftpos = method.code === 'EFTPOS' || method.type === 'eftpos';
  var showKeyboard = function showKeyboard() {
    if (isEftpos) return;
    setIsKeyboardVisible(true);
  };

  // 渲染左侧内容
  var renderLeftContent = function renderLeftContent() {
    return /*#__PURE__*/React.createElement("div", {
      className: classnames('payment-options-module-payment-method-left')
    }, /*#__PURE__*/React.createElement("div", {
      className: classnames('payment-options-module-icon-container')
    }, renderPaymentIcon(method)), /*#__PURE__*/React.createElement("div", {
      className: classnames('payment-options-module-payment-info')
    }, /*#__PURE__*/React.createElement("div", {
      className: classnames('payment-options-module-payment-info-inner')
    }, /*#__PURE__*/React.createElement("div", {
      className: classnames('payment-options-module-payment-name')
    }, method.name), /*#__PURE__*/React.createElement("div", {
      className: classnames('payment-options-module-payment-info-inner-surcharge')
    }, !!(method !== null && method !== void 0 && method.is_surcharge) && calculateSurcharge(method) !== 0 && /*#__PURE__*/React.createElement("div", {
      className: classnames('payment-options-module-surcharge-text')
    }, (getText === null || getText === void 0 ? void 0 : getText('pisell2.payment-options.surcharge-label', 'Surcharge')) || 'Surcharge', ":", formatAmount(calculateSurcharge(method), 2, amountSymbol)), false && /*#__PURE__*/React.createElement("div", {
      className: classnames('payment-options-module-surcharge-text'),
      style: {
        cursor: 'pointer'
      }
    }, /*#__PURE__*/React.createElement(Iconfont, {
      type: "pisell2-edit-05",
      style: {
        fontSize: '24px',
        color: disabled ? '#D0D5DD' : '#7F56DA'
      }
    }))))));
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("button", {
    className: classnames('payment-options-module-payment-method-item', {
      'payment-options-module-pointer': !disabled,
      'payment-options-module-disabled': disabled
    }),
    onClick: showKeyboard,
    type: "button",
    disabled: disabled
  }, renderLeftContent(), /*#__PURE__*/React.createElement("div", {
    className: classnames('payment-options-module-payment-method-right'),
    onClick: handleRightButtonClick
  },
  // 右侧按钮，如果是eftpos，则显示Pay按钮
  isEftpos ? /*#__PURE__*/React.createElement(Button, {
    type: "primary",
    className: classnames('payment-options-module-pay-button'),
    disabled: disabled
  }, getText === null || getText === void 0 ? void 0 : getText('pay', 'Pay')) : /*#__PURE__*/React.createElement("div", {
    className: classnames('payment-options-module-selection-button')
  }, /*#__PURE__*/React.createElement(Iconfont, {
    type: "pisell2-a-arrow-circle-right2",
    style: {
      fontSize: '30px',
      color: disabled ? '#D0D5DD' : '#7F56DA'
    }
  })))), /*#__PURE__*/React.createElement(PaymentKeyboard, {
    containerProps: {
      renderType: 'modal',
      open: isKeyboardVisible,
      onCancel: function onCancel() {
        return setIsKeyboardVisible(false);
      }
    },
    paymentOriginalValue: String(customAmount || 0),
    paymentDefaultValue: String(customAmount || 0),
    enableExceedMaxAmount: !isEftpos,
    payment: {
      enable: true
    },
    surcharge: {
      enable: !!(method !== null && method !== void 0 && method.is_surcharge) && calculateSurcharge(method) !== 0
    },
    surchargeDefaultValue: {
      percent: method.percentage ? String(Number(method.percentage) * 100) : '',
      fixed: method.fixed ? String(method.fixed) : ''
    },
    selectType: "dark",
    onChange: function onChange(value) {
      console.log(value);
    },
    onPay: function onPay(args) {
      console.log(args);
      // 处理支付逻辑
      setIsKeyboardVisible(false);
      if (onClick) {
        var _args$surcharge, _args$surcharge2;
        onClick(_objectSpread(_objectSpread({}, method), {}, {
          fixed: Number(((_args$surcharge = args.surcharge) === null || _args$surcharge === void 0 ? void 0 : _args$surcharge.fixed) || 0),
          percentage: (_args$surcharge2 = args.surcharge) !== null && _args$surcharge2 !== void 0 && _args$surcharge2.percent ? Number(args.surcharge.percent) / 100 : 0,
          totalAmount: String(formatAmount(args.amount, 2))
        }));
      }
    }
  }));
};
export default PaymentMethodItem;