var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/eftposPay/component/manual/index.tsx
var manual_exports = {};
__export(manual_exports, {
  default: () => manual_default
});
module.exports = __toCommonJS(manual_exports);
var import_utils = require("@pisell/utils");
var import_decimal = require("decimal.js");
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_usePrefix = __toESM(require("../../../../hooks/usePrefix"));
var import_const = require("../../const");
var import_store = require("../../store");
var import_helper = require("../../helper");
var import_payLog = __toESM(require("../../utils/payLog"));
var import_index = require("./index.less");
var manual_default = ({ onChange, className = "", formatAmount }) => {
  const dispatch = (0, import_store.useDispatch)();
  const { symbol, manual, order_id, mode, device, number, pay, amount } = (0, import_store.useContextData)();
  (0, import_react.useEffect)(() => {
    import_payLog.default.log("手动标记页展示");
  }, []);
  const start = (0, import_react.useMemo)(() => {
    if (typeof (manual == null ? void 0 : manual.money) === "number" || typeof (manual == null ? void 0 : manual.money) === "string") {
      return Number(manual == null ? void 0 : manual.money);
    }
    if (typeof (pay == null ? void 0 : pay.total) === "number" || typeof (pay == null ? void 0 : pay.total) === "string") {
      return Number(pay == null ? void 0 : pay.total);
    }
    return amount;
  }, [manual == null ? void 0 : manual.money, pay == null ? void 0 : pay.total, amount]);
  const [money, setMoney] = (0, import_react.useState)(start);
  const [disabled, setDisabled] = (0, import_react.useState)(false);
  const prefix = (0, import_usePrefix.default)();
  const [isUpdateAmount, updateAmountStatus] = (0, import_react.useState)(manual == null ? void 0 : manual.status);
  const onSuccess = () => {
    if (disabled) {
      import_payLog.default.log("手动标记：点击成功被拦截");
      return;
    }
    if (mode === import_const.ModeEnum.Refund) {
      import_payLog.default.log("手动标记(退款)：点击标记为成功");
      setDisabled(true);
      onChange("success", {
        amount,
        total: amount,
        number,
        uniquePaymentNumber: number,
        device,
        order_id,
        manual_marking_flag: 1
        // 手动标记标志
      });
      return;
    }
    import_payLog.default.log("手动标记：点击标记为成功");
    updateAmountStatus(true);
    dispatch((0, import_store.updateState)({ manual: { ...manual, status: true } }));
  };
  const onFail = () => {
    if (disabled) {
      return;
    }
    import_payLog.default.log("手动标记：点击标记为失败");
    setDisabled(true);
    onChange("fail", { number, uniquePaymentNumber: number, device, order_id });
  };
  const onSubmit = () => {
    if (disabled) {
      return;
    }
    import_payLog.default.log("手动标记-确认金额：点击确认", { money });
    setDisabled(true);
    if (mode === import_const.ModeEnum.Refund || mode === import_const.ModeEnum.Query) {
      return;
    }
    const prevMoneyDeci = new import_decimal.Decimal((pay == null ? void 0 : pay.total) || 0);
    const moneyDeci = new import_decimal.Decimal(Number(money || 0));
    let surMoneyDeci = new import_decimal.Decimal(Number((pay == null ? void 0 : pay.surMoney) || 0));
    let surchargeDeci = new import_decimal.Decimal(Number(pay == null ? void 0 : pay.surcharge));
    const diff = moneyDeci.sub(prevMoneyDeci);
    const flag = diff.comparedTo(0);
    if (flag < 0) {
      setDisabled(false);
      return;
    }
    if (flag > 0) {
      surMoneyDeci = surMoneyDeci.add(diff);
      surchargeDeci = surchargeDeci.add(diff);
    }
    const obj = pay || {};
    onChange("success", {
      ...obj,
      surMoney: surMoneyDeci.toFixed(2, import_decimal.Decimal.ROUND_HALF_UP),
      surcharge: surchargeDeci.toFixed(2, import_decimal.Decimal.ROUND_HALF_UP),
      total: moneyDeci.toFixed(2, import_decimal.Decimal.ROUND_HALF_UP),
      totalUnit: formatAmount(
        moneyDeci.toFixed(2, import_decimal.Decimal.ROUND_HALF_UP),
        2,
        symbol
      ),
      number,
      uniquePaymentNumber: number,
      device,
      order_id,
      manual_marking_flag: 1
      // 手动标记标志
    });
    return;
  };
  const onCancel = () => {
    import_payLog.default.log("手动标记-确认金额：点击取消");
    updateAmountStatus(false);
    setMoney(pay == null ? void 0 : pay.total);
    dispatch(
      (0, import_store.updateState)({
        manual: { ...manual, status: true, money: pay == null ? void 0 : pay.total }
      })
    );
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { className: `pisell-lowcode__eft-pay-manual ${className}` }, isUpdateAmount ? /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-manual-edit-title" }, import_utils.locales.getText("page.eftpos-pay.text.manual.title")), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-manual-edit-body" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-manual-edit-text" }, import_utils.locales.getText("page.eftpos-pay.text.manual.subtitle")(
    formatAmount(pay == null ? void 0 : pay.total, 2, symbol)
  ), " ", /* @__PURE__ */ import_react.default.createElement("br", null), import_utils.locales.getText("page.eftpos-pay.text.manual.confirm"), /* @__PURE__ */ import_react.default.createElement("br", null), import_utils.locales.getText("page.eftpos-pay.text.manual.diff")), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-manual-edit-act" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-manual-edit-text" }, import_utils.locales.getText("page.eftpos-pay.text.manual.tip")), /* @__PURE__ */ import_react.default.createElement(
    import_antd.InputNumber,
    {
      type: "number",
      addonBefore: symbol || "$",
      min: pay == null ? void 0 : pay.total,
      precision: 2,
      style: { width: "100%" },
      formatter: import_helper.limitDecimals,
      parser: import_helper.limitDecimals,
      defaultValue: money,
      onChange: (event) => {
        if (event !== null && event !== void 0) {
          setMoney(event || 0);
          dispatch(
            (0, import_store.updateState)({ manual: { ...manual, money: event || 0 } })
          );
        } else {
          setMoney(0);
          dispatch((0, import_store.updateState)({ manual: { ...manual, money: 0 } }));
        }
      }
    }
  ))), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-step-item-btns" }, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Button,
    {
      className: "pisell-lowcode__eft-pay-manual-btn",
      block: true,
      onClick: onCancel
    },
    import_utils.locales.getText("page.eftpos-pay.text.cancel")
  ), /* @__PURE__ */ import_react.default.createElement(
    import_antd.Button,
    {
      className: "pisell-lowcode__eft-pay-manual-btn",
      block: true,
      type: "primary",
      onClick: onSubmit
    },
    import_utils.locales.getText("page.eftpos-pay.text.manual.btn.confirm")(
      formatAmount(money, 2, symbol)
    )
  ))) : /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-manual-title" }, manual == null ? void 0 : manual.title), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-manual-txt" }, manual == null ? void 0 : manual.text), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode__eft-pay-manual-card-box" }, /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: "pisell-lowcode__eft-pay-manual-card",
      onClick: onSuccess
    },
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: "pisell-lowcode__eft-pay-manual-img",
        style: { backgroundImage: `url(${prefix}/pay_success.png)` }
      }
    ),
    /* @__PURE__ */ import_react.default.createElement(import_antd.Button, { className: "pisell-lowcode__eft-pay-manual-btn", block: true }, import_utils.locales.getText("page.eftpos-pay.text.successful")(mode))
  ), /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: "pisell-lowcode__eft-pay-manual-card",
      onClick: onFail
    },
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: "pisell-lowcode__eft-pay-manual-img",
        style: { backgroundImage: `url(${prefix}/pay_error.png)` }
      }
    ),
    /* @__PURE__ */ import_react.default.createElement(import_antd.Button, { className: "pisell-lowcode__eft-pay-manual-btn", block: true }, import_utils.locales.getText("page.eftpos-pay.text.failed")(mode))
  ))));
};
