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/pay/toB/components/Content/index.tsx
var Content_exports = {};
__export(Content_exports, {
  default: () => Content_default
});
module.exports = __toCommonJS(Content_exports);
var import_react = __toESM(require("react"));
var import_materials = require("@pisell/materials");
var import_antd = require("antd");
var import_ahooks = require("ahooks");
var import_hooks = require("../../hooks");
var import_store = require("../../store");
var import_types = require("../../types");
var import_CardGroup = __toESM(require("../CardGroup"));
var import_ConfirmedPayment = __toESM(require("../ConfirmedPayment"));
var import_Header = __toESM(require("./Header"));
var import_Footer = __toESM(require("./Footer"));
var import_Cache = __toESM(require("../Cache"));
var import_utils = require("./utils");
var import_index = require("./index.less");
var ToBPayContent = (0, import_react.forwardRef)((props, ref) => {
  var _a, _b, _c, _d;
  const network = (0, import_ahooks.useNetwork)();
  const engineUtils = (0, import_hooks.useEngineUtils)();
  const symbol = (0, import_hooks.useSymbol)();
  const osPayment = (0, import_store.useOSPayment)();
  const { osPaymentEffectOn, osPaymentEffectOff } = (0, import_store.useOSPaymentEffect)();
  const { order, getPayMethodList, payMethodGroupList } = (0, import_store.useToBPay)();
  const [open, setOpen] = (0, import_react.useState)(false);
  const [modalStatus, setModalStatus] = (0, import_react.useState)({
    open: false,
    paymethod: null
  });
  const [submitLoading, setSubmitLoading] = (0, import_react.useState)(false);
  const eftposOpenRef = (0, import_react.useRef)(false);
  (0, import_react.useEffect)(() => {
    return () => {
      osPaymentEffectOff();
    };
  }, []);
  (0, import_react.useEffect)(() => {
    var _a2;
    console.log("tob_pay_useEffect_network", network);
    if (eftposOpenRef.current) {
      (_a2 = engineUtils == null ? void 0 : engineUtils.action) == null ? void 0 : _a2.call(engineUtils, {
        type: "pisell1.handleEftposPayNetWork",
        data: {
          network: network.online
        },
        callback() {
        }
      });
    }
  }, [network.online]);
  const openModal = (orderInfo) => {
    console.log("tob_pay_open", orderInfo);
    setOpen(true);
    osPaymentEffectOn();
    osPayment == null ? void 0 : osPayment.pushOrderAsync({ order_info: orderInfo || {} });
    getPayMethodList();
  };
  const onCloseModal = () => {
    var _a2, _b2;
    if (submitLoading) {
      return;
    }
    closeModalAction();
    (_a2 = osPayment == null ? void 0 : osPayment.submitPayAsync) == null ? void 0 : _a2.call(osPayment, order == null ? void 0 : order.uuid);
    (_b2 = props.callback) == null ? void 0 : _b2.call(props, { status: "cancel" });
  };
  const onSubmit = async () => {
    var _a2, _b2, _c2;
    if (submitLoading) {
      return;
    }
    setSubmitLoading(true);
    try {
      await ((_a2 = osPayment == null ? void 0 : osPayment.submitPayAsync) == null ? void 0 : _a2.call(osPayment, order == null ? void 0 : order.uuid));
      console.log("tob_pay_onSubmit_res");
      (_b2 = props.callback) == null ? void 0 : _b2.call(props, { status: "success" });
      closeModalAction();
    } catch (error) {
      console.log("tob_pay_onSubmit_error", error);
      (_c2 = props.callback) == null ? void 0 : _c2.call(props, { status: "error" });
    } finally {
      setSubmitLoading(false);
    }
  };
  const closeModalAction = () => {
    setOpen(false);
    osPaymentEffectOff();
  };
  (0, import_react.useImperativeHandle)(ref, () => ({
    open: openModal
  }));
  const onChangePayMethod = (item, type) => {
    console.log("onChange", item, type);
    if (item.code === import_types.PayMethodCode.CASHMANUAL) {
      setModalStatus({ open: true, paymethod: item });
    } else if ((0, import_utils.isEftposPay)(item.code)) {
      eftposOpenRef.current = true;
      (0, import_utils.handleEftposPay)(
        engineUtils == null ? void 0 : engineUtils.action,
        {
          order,
          payMethod: item,
          symbol,
          network,
          type
        },
        (res) => {
          eftposOpenRef.current = false;
          console.log("tob_pay_onChangePayMethod_eftpos_callback", res);
          osPayment == null ? void 0 : osPayment.pushPaymentAsync(order == null ? void 0 : order.uuid, (res == null ? void 0 : res.result) || {});
        }
      );
    }
  };
  const onModalCancel = () => {
    setModalStatus({ open: false, paymethod: null });
  };
  const onModalConfirm = (amount) => {
    console.log("onConfirmPay", amount);
    const paymethod = modalStatus.paymethod || {};
    osPayment == null ? void 0 : osPayment.pushPaymentAsync(order == null ? void 0 : order.uuid, {
      amount,
      // 当前支付方式付出去多少钱
      code: paymethod.code,
      // 支付类型code
      id: paymethod.id,
      // 支付类型id
      name: paymethod.name,
      // 支付类型name
      type: paymethod.type,
      // 支付类型type
      voucher_id: ""
    });
    setModalStatus({ open: false, paymethod: null });
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_materials.PisellModal,
    {
      open,
      width: "65vw",
      maskClosable: false,
      bodyStyle: { padding: 0 },
      header: true,
      title: /* @__PURE__ */ import_react.default.createElement(import_Header.default, { amount: (order == null ? void 0 : order.expect_amount) || "0.00" }),
      footer: /* @__PURE__ */ import_react.default.createElement(
        import_Footer.default,
        {
          onCancel: onCloseModal,
          onSubmit,
          submitDisabled: ((_a = order == null ? void 0 : order.payment) == null ? void 0 : _a.length) ? false : true,
          loading: submitLoading
        }
      ),
      closable: true,
      onCancel: onCloseModal
    },
    /* @__PURE__ */ import_react.default.createElement(import_antd.Spin, { spinning: submitLoading }, /* @__PURE__ */ import_react.default.createElement("div", { className: "tob-pay-container" }, ((_b = order == null ? void 0 : order.payment) == null ? void 0 : _b.length) ? /* @__PURE__ */ import_react.default.createElement(import_ConfirmedPayment.default, { data: order == null ? void 0 : order.payment }) : null, payMethodGroupList.map((item) => /* @__PURE__ */ import_react.default.createElement(
      import_CardGroup.default,
      {
        key: item.title,
        data: item,
        onChange: onChangePayMethod
      }
    )), modalStatus.open && ((_c = modalStatus.paymethod) == null ? void 0 : _c.code) === import_types.PayMethodCode.CASHMANUAL ? /* @__PURE__ */ import_react.default.createElement(
      import_Cache.default,
      {
        title: ((_d = modalStatus.paymethod) == null ? void 0 : _d.name) || "",
        uuid: (order == null ? void 0 : order.uuid) || "",
        expect_amount: (order == null ? void 0 : order.expect_amount) || "0.00",
        open: modalStatus.open,
        onCancel: onModalCancel,
        onOk: onModalConfirm
      }
    ) : null))
  );
});
var Content_default = ToBPayContent;
