"use strict";
const common_vendor = require("../../common/vendor.js");
require("../../common/libraries/request.js");
const common_store_useWeAppAuthStore = require("../../common/store/useWeAppAuthStore.js");
const common_store_useWorkStore = require("../../common/store/useWorkStore.js");
const ME_pre_stored_form_model = require("./model.js");
const gen_Apis = require("../../gen/Apis.js");
const common_libraries_apiLoading = require("../../common/libraries/apiLoading.js");
const common_libraries_pay = require("../../common/libraries/pay.js");
const common_libraries_naviHelper = require("../../common/libraries/naviHelper.js");
common_store_useWeAppAuthStore.useWeAppAuthStore();
common_store_useWorkStore.useWorkStore();
const getPayStatus = (timer) => {
  var _a, _b, _c;
  gen_Apis.Apis.HouseOrder.HousePrepayments.GetPayments({
    house_prepayment_payments_id: (_c = (_b = (_a = ME_pre_stored_form_model.useModel) == null ? void 0 : _a.housePrepaymentPayment) == null ? void 0 : _b.value) == null ? void 0 : _c.id
  }).then((res) => {
    var _a2;
    console.log("获取支付状态", res == null ? void 0 : res.data);
    ME_pre_stored_form_model.useModel.loadingShowNumber.value += 1;
    ME_pre_stored_form_model.useModel.orderShow.value = res == null ? void 0 : res.data;
    if (((_a2 = res == null ? void 0 : res.data) == null ? void 0 : _a2.payment_status) === "Paid") {
      clearInterval(timer);
      ME_pre_stored_form_model.useModel.formData.value.amount = void 0;
      ME_pre_stored_form_model.useModel.payDisabled.value = false;
      common_libraries_naviHelper.showToast("支付成功");
    }
  });
};
const loadShow = () => {
  var _a, _b;
  console.log("定时器", (_b = (_a = ME_pre_stored_form_model.useModel) == null ? void 0 : _a.loadingShowNumber) == null ? void 0 : _b.value);
  ME_pre_stored_form_model.useModel.timer.value = setInterval(() => {
    var _a2, _b2, _c, _d, _e;
    if (((_b2 = (_a2 = ME_pre_stored_form_model.useModel) == null ? void 0 : _a2.loadingShowNumber) == null ? void 0 : _b2.value) > 2 && ((_e = (_d = (_c = ME_pre_stored_form_model.useModel) == null ? void 0 : _c.orderShow) == null ? void 0 : _d.value) == null ? void 0 : _e.payment_status) === "Pending") {
      ME_pre_stored_form_model.useModel.payDisabled.value = false;
      common_vendor.index.hideLoading();
      clearInterval(ME_pre_stored_form_model.useModel.timer.value);
      return;
    }
    if (ME_pre_stored_form_model.useModel.loadingShowNumber.value <= 2) {
      getPayStatus(ME_pre_stored_form_model.useModel.timer.value);
    }
  }, 1e3);
};
const method = {
  loadShow,
  handlePay() {
    var _a, _b;
    ME_pre_stored_form_model.useModel.payDisabled.value = true;
    common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.HouseOrder.HousePrepayments.Payment, (_b = (_a = ME_pre_stored_form_model.useModel) == null ? void 0 : _a.formData) == null ? void 0 : _b.value).then((res) => {
      var _a2, _b2;
      ME_pre_stored_form_model.useModel.housePrepaymentPayment.value = (_a2 = res == null ? void 0 : res.data) == null ? void 0 : _a2.house_prepayment_payment;
      common_vendor.index.showLoading({
        title: "正在支付...",
        mask: true
      });
      common_libraries_pay.tongLianPay(
        (_b2 = res == null ? void 0 : res.data) == null ? void 0 : _b2.pay_info,
        () => {
          ME_pre_stored_form_model.useModel.loadingShowNumber.value = -1;
          console.log("支付成功");
        },
        () => {
          ME_pre_stored_form_model.useModel.payDisabled.value = false;
          common_vendor.index.hideLoading();
        }
      );
      console.log(res);
    }).catch(() => {
      ME_pre_stored_form_model.useModel.payDisabled.value = false;
    });
    console.log("e");
  }
};
exports.method = method;
