"use strict";
const common_vendor = require("../../common/vendor.js");
const config_user = require("../../config/user.js");
const _sfc_main = {
  data() {
    return {
      orderId: "",
      orderInfo: {},
      isLoading: true,
      deviceMessage: "正在准备您的设备，请稍候...",
      hasTriggeredDevice: false
    };
  },
  onLoad(options) {
    if (options && options.orderId) {
      this.orderId = options.orderId;
      this.loadOrderInfo();
      common_vendor.index.$once("orderSuccess:" + this.orderId, () => {
        common_vendor.index.__f__("log", "at pages/order/success.vue:67", "已经触发过弹出逻辑，不再重复触发");
        this.hasTriggeredDevice = true;
      });
    } else {
      common_vendor.index.showToast({
        title: "订单信息不存在",
        icon: "none"
      });
      setTimeout(() => {
        this.goToHome();
      }, 1500);
    }
  },
  methods: {
    async loadOrderInfo() {
      try {
        common_vendor.index.showLoading({
          title: "加载中"
        });
        const res = await config_user.queryById(this.orderId);
        if (res.code === 200 && res.data) {
          const orderData = res.data;
          this.orderInfo = {
            orderNo: orderData.orderNo || orderData.orderId,
            deviceNo: orderData.deviceNo,
            amount: orderData.payAmount || orderData.amount,
            payTime: orderData.payTime || this.formatTime(/* @__PURE__ */ new Date())
          };
          if (orderData.orderStatus === "IN_USED") {
            this.deviceMessage = "设备已弹出，请取走您的风扇";
            this.isLoading = false;
            if (!this.hasTriggeredDevice) {
              common_vendor.index.$emit("orderSuccess:" + this.orderId);
              this.hasTriggeredDevice = true;
            }
          } else {
            this.triggerDeviceEject();
          }
        } else {
          throw new Error("获取订单信息失败");
        }
        common_vendor.index.hideLoading();
      } catch (error) {
        common_vendor.index.hideLoading();
        common_vendor.index.showToast({
          title: error.message || "获取订单信息失败",
          icon: "none"
        });
      }
    },
    // 触发弹出风扇
    async triggerDeviceEject() {
      if (this.hasTriggeredDevice) {
        common_vendor.index.__f__("log", "at pages/order/success.vue:129", "已经触发过弹出风扇，不重复触发");
        return;
      }
      this.hasTriggeredDevice = true;
      common_vendor.index.$emit("orderSuccess:" + this.orderId);
      this.isLoading = true;
      this.deviceMessage = "正在准备您的设备，请稍候...";
      try {
        common_vendor.index.__f__("log", "at pages/order/success.vue:139", `准备触发弹出风扇，orderId: ${this.orderId}`);
        const result = await config_user.confirmPaymentAndRent(this.orderId);
        common_vendor.index.__f__("log", "at pages/order/success.vue:143", "确认支付并弹出风扇结果:", JSON.stringify(result));
        if (result && result.code === 200) {
          this.deviceMessage = "设备已弹出，请取走您的风扇";
          common_vendor.index.showToast({
            title: "风扇已弹出",
            icon: "success"
          });
        } else {
          throw new Error(result && result.msg || "弹出风扇失败");
        }
      } catch (error) {
        common_vendor.index.__f__("error", "at pages/order/success.vue:155", "弹出风扇错误:", error);
        this.deviceMessage = "弹出设备失败，请联系客服";
        common_vendor.index.showToast({
          title: error.message || "弹出风扇失败，请联系客服",
          icon: "none"
        });
      } finally {
        this.isLoading = false;
      }
    },
    formatTime(date) {
      const year = date.getFullYear();
      const month = (date.getMonth() + 1).toString().padStart(2, "0");
      const day = date.getDate().toString().padStart(2, "0");
      const hour = date.getHours().toString().padStart(2, "0");
      const minute = date.getMinutes().toString().padStart(2, "0");
      const second = date.getSeconds().toString().padStart(2, "0");
      return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    },
    goToHome() {
      common_vendor.index.switchTab({
        url: "/pages/index/index"
      });
    },
    goToOrderList() {
      common_vendor.index.redirectTo({
        url: "/pages/order/index"
      });
    }
  }
};
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return common_vendor.e({
    a: common_vendor.t($data.orderInfo.orderNo || "-"),
    b: common_vendor.t($data.orderInfo.deviceNo || "-"),
    c: common_vendor.t($data.orderInfo.amount || "0.00"),
    d: common_vendor.t($data.orderInfo.payTime || "-"),
    e: common_vendor.t($data.deviceMessage),
    f: $data.isLoading
  }, $data.isLoading ? {} : {}, {
    g: common_vendor.o((...args) => $options.goToHome && $options.goToHome(...args)),
    h: common_vendor.o((...args) => $options.goToOrderList && $options.goToOrderList(...args))
  });
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-2795c576"]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/order/success.js.map
