"use strict";
const common_vendor = require("../../common/vendor.js");
const modules_api_AppServer = require("../../modules/api/AppServer.js");
const common_assets = require("../../common/assets.js");
const appServer = new modules_api_AppServer.AppServer();
const _sfc_main = {
  data() {
    return {
      navbarBgColor: "transparent",
      scrollTop: 0,
      showWithdrawModal: false,
      showApplyModal: false,
      showQRCodeModal: false,
      showQRCodeContent: false,
      // 控制二维码内容显示
      qrcodeDataUrl: "",
      qrcodeValue: "",
      // 二维码内容
      appLogo: "",
      // 应用logo，用于分享图片
      applyStep: 1,
      withdrawAmount: "",
      paymentMethod: "wechat",
      qrcodeImage: "",
      bankCardNumber: "",
      cardholderName: "",
      bankName: "",
      swiftCode: "",
      withdrawRecords: [],
      withdrawDetailList: [],
      inviteRecords: [],
      // 佣金统计
      commissionStats: {
        totalInvites: 0,
        paidInvites: 0,
        totalCommission: "0.00",
        availableBalance: "0.00",
        invitationCode: ""
      },
      loading: false,
      inviteRules: ""
      // 邀请规则说明（从后台配置获取）
    };
  },
  computed: {
    // 弹窗显示时禁止页面滚动
    pageStyle() {
      if (this.showQRCodeModal || this.showWithdrawModal || this.showApplyModal) {
        return "overflow: hidden;";
      }
      return "";
    }
  },
  onLoad() {
    this.loadData();
  },
  // 微信小程序分享配置
  onShareAppMessage() {
    const inviteCode = this.commissionStats.invitationCode || "";
    let shareImage = "/static/new_bg1.png";
    if (this.appLogo && this.appLogo.startsWith("http")) {
      shareImage = this.appLogo;
    }
    common_vendor.index.__f__("log", "at pages/me/invite-reward-page.vue:340", "分享图片路径:", shareImage, "appLogo:", this.appLogo);
    return {
      title: "邀请你加入，一起赚佣金！",
      path: `/pages/index/index?inviteCode=${inviteCode}`,
      imageUrl: shareImage
    };
  },
  // 分享到朋友圈（微信小程序）
  onShareTimeline() {
    const inviteCode = this.commissionStats.invitationCode || "";
    let shareImage = "/static/new_bg1.png";
    if (this.appLogo && this.appLogo.startsWith("http")) {
      shareImage = this.appLogo;
    }
    return {
      title: "邀请你加入，一起赚佣金！",
      query: `inviteCode=${inviteCode}`,
      imageUrl: shareImage
    };
  },
  methods: {
    // 加载数据
    async loadData() {
      this.loading = true;
      try {
        await Promise.all([
          this.loadCommissionStats(),
          this.loadCommissionRecords(),
          this.loadWithdrawRecords(),
          this.loadAppConfig()
        ]);
      } catch (error) {
        common_vendor.index.__f__("error", "at pages/me/invite-reward-page.vue:372", "加载数据失败:", error);
      } finally {
        this.loading = false;
      }
    },
    // 加载应用配置（从globalData获取logo和邀请规则）
    loadAppConfig() {
      try {
        const app = getApp();
        if (app && app.globalData && app.globalData.config) {
          const config = app.globalData.config;
          if (config.app_logo) {
            this.appLogo = app.getConfigImageUrl("app_logo");
            common_vendor.index.__f__("log", "at pages/me/invite-reward-page.vue:386", "从globalData获取到应用logo:", this.appLogo);
          }
          if (config.invite_rules) {
            this.inviteRules = config.invite_rules;
          }
        }
      } catch (error) {
        common_vendor.index.__f__("error", "at pages/me/invite-reward-page.vue:394", "获取应用配置失败:", error);
      }
    },
    // 加载佣金统计
    async loadCommissionStats() {
      try {
        const res = await appServer.GetCommissionStats();
        if (res && res.code === 0 && res.data) {
          this.commissionStats = res.data;
          this.updateWithdrawDisplay();
          const inviteCode = res.data.invitationCode || "";
          this.qrcodeValue = `https://your-domain.com/invite?code=${inviteCode}`;
        }
      } catch (error) {
        common_vendor.index.__f__("error", "at pages/me/invite-reward-page.vue:412", "获取佣金统计失败:", error);
      }
    },
    // 加载佣金记录
    async loadCommissionRecords() {
      try {
        const res = await appServer.GetCommissions({
          page: 1,
          limit: 50
        });
        if (res && res.code === 0 && res.data && res.data.records) {
          this.inviteRecords = res.data.records.map((item) => {
            var _a, _b;
            return {
              username: ((_a = item.invitee) == null ? void 0 : _a.nickname) || "-",
              uid: ((_b = item.invitee) == null ? void 0 : _b.uid) || "-",
              time: this.formatDate(item.createdAt),
              paid: true,
              amount: parseFloat(item.commissionAmount)
            };
          });
        }
      } catch (error) {
        common_vendor.index.__f__("error", "at pages/me/invite-reward-page.vue:434", "获取佣金记录失败:", error);
      }
    },
    // 加载提现记录
    async loadWithdrawRecords() {
      try {
        const res = await appServer.GetWithdrawals({
          page: 1,
          limit: 10
        });
        if (res && res.code === 0 && res.data && res.data.records) {
          this.withdrawDetailList = res.data.records.map((item) => ({
            time: this.formatDate(item.createdAt),
            amount: item.amount,
            status: this.getWithdrawStatusText(item.status)
          }));
        }
      } catch (error) {
        common_vendor.index.__f__("error", "at pages/me/invite-reward-page.vue:454", "获取提现记录失败:", error);
      }
    },
    // 更新提现显示
    updateWithdrawDisplay() {
      this.withdrawRecords = [{
        amount: this.commissionStats.availableBalance || "0",
        status: this.$t("invite.statusWaiting")
      }];
    },
    // 格式化日期
    formatDate(dateStr) {
      if (!dateStr)
        return "-";
      const date = new Date(dateStr);
      return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
    },
    // 获取提现状态文本
    getWithdrawStatusText(status) {
      const statusMap = {
        "waiting": this.$t("invite.statusWaiting"),
        "processing": this.$t("invite.statusProcessing"),
        "completed": this.$t("invite.statusCompleted"),
        "rejected": this.$t("invite.statusRejected") || "已拒绝"
      };
      return statusMap[status] || status;
    },
    goBack() {
      common_vendor.index.navigateBack();
    },
    showDetail() {
      common_vendor.index.showModal({
        title: "规则说明",
        content: this.inviteRules || this.$t("invite.ruleContent"),
        showCancel: false
      });
    },
    generateQRCode() {
      const inviteCode = this.commissionStats.invitationCode || "";
      this.qrcodeValue = `https://your-domain.com/invite?code=${inviteCode}`;
      this.showQRCodeContent = false;
      this.showQRCodeModal = true;
    },
    onQRCodePopupOpen() {
      setTimeout(() => {
        this.showQRCodeContent = true;
      }, 100);
    },
    closeQRCodeModal() {
      this.showQRCodeContent = false;
      this.showQRCodeModal = false;
      this.qrcodeDataUrl = "";
    },
    shareQRCodeToFriend() {
      common_vendor.index.showToast({
        title: this.$t("invite.shareSuccess") || "请使用右上角分享",
        icon: "none"
      });
    },
    saveQRCodeImage() {
      if (this.$refs.uQrcode) {
        this.$refs.uQrcode.toTempFilePath({
          success: (res) => {
            common_vendor.index.saveImageToPhotosAlbum({
              filePath: res.tempFilePath,
              success: () => {
                common_vendor.index.showToast({
                  title: this.$t("invite.saveSuccess") || "保存成功",
                  icon: "success"
                });
              },
              fail: (err) => {
                common_vendor.index.__f__("error", "at pages/me/invite-reward-page.vue:540", "保存图片失败:", err);
                if (err.errMsg && err.errMsg.includes("auth deny")) {
                  common_vendor.index.showModal({
                    title: "提示",
                    content: "需要您授权保存图片到相册",
                    success: (modalRes) => {
                      if (modalRes.confirm) {
                        common_vendor.index.openSetting();
                      }
                    }
                  });
                } else {
                  common_vendor.index.showToast({
                    title: "保存失败",
                    icon: "none"
                  });
                }
              }
            });
          },
          fail: (err) => {
            common_vendor.index.__f__("error", "at pages/me/invite-reward-page.vue:561", "获取二维码图片失败:", err);
            common_vendor.index.showToast({
              title: "获取二维码失败",
              icon: "none"
            });
          }
        });
      } else {
        common_vendor.index.showToast({
          title: "二维码生成中，请稍候",
          icon: "none"
        });
      }
    },
    shareToFriend() {
      common_vendor.index.share({
        provider: "weixin",
        scene: "WXSceneSession",
        type: 0,
        title: this.$t("invite.shareTitle"),
        success: () => {
          common_vendor.index.showToast({
            title: this.$t("invite.shareSuccess"),
            icon: "success"
          });
        }
      });
    },
    goToWithdrawDetail() {
      this.showWithdrawModal = true;
    },
    closeWithdrawModal() {
      this.showWithdrawModal = false;
    },
    applyWithdraw() {
      this.applyStep = 1;
      this.withdrawAmount = "";
      this.paymentMethod = "wechat";
      this.qrcodeImage = "";
      this.bankCardNumber = "";
      this.cardholderName = "";
      this.bankName = "";
      this.swiftCode = "";
      this.showApplyModal = true;
    },
    closeApplyModal() {
      this.showApplyModal = false;
    },
    nextStep() {
      const amount = parseFloat(this.withdrawAmount);
      if (!this.withdrawAmount || isNaN(amount)) {
        common_vendor.index.showToast({
          title: this.$t("invite.enterAmountError"),
          icon: "none"
        });
        return;
      }
      if (amount < 1) {
        common_vendor.index.showToast({
          title: this.$t("invite.amountTooLow") || "低于1元，无法申请提现",
          icon: "none"
        });
        return;
      }
      const availableBalance = parseFloat(this.commissionStats.availableBalance || "0");
      if (amount > availableBalance) {
        common_vendor.index.showToast({
          title: this.$t("invite.amountExceedsBalance") || "超出可提现金额",
          icon: "none"
        });
        return;
      }
      const formattedAmount = Math.floor(amount * 100) / 100;
      this.withdrawAmount = formattedAmount.toString();
      this.applyStep = 2;
    },
    selectPaymentMethod(method) {
      this.paymentMethod = method;
    },
    uploadQRCode() {
      common_vendor.index.chooseImage({
        count: 1,
        success: (res) => {
          this.qrcodeImage = res.tempFilePaths[0];
        }
      });
    },
    async submitWithdraw() {
      var _a;
      if (this.paymentMethod !== "bank" && !this.qrcodeImage) {
        common_vendor.index.showToast({
          title: this.$t("invite.uploadQRCodeError"),
          icon: "none"
        });
        return;
      }
      if (this.paymentMethod === "bank") {
        if (!this.bankCardNumber || !this.cardholderName || !this.bankName) {
          common_vendor.index.showToast({
            title: this.$t("invite.bankInfoError"),
            icon: "none"
          });
          return;
        }
      }
      let paymentDetails = {};
      if (this.paymentMethod === "bank") {
        paymentDetails = {
          bankCardNumber: this.bankCardNumber,
          cardholderName: this.cardholderName,
          bankName: this.bankName,
          swiftCode: this.swiftCode || void 0
        };
      } else {
        try {
          const uploadRes = await appServer.UploadImage(this.qrcodeImage);
          if (uploadRes && uploadRes.code === 0 && uploadRes.data) {
            paymentDetails = {
              qrcodeUrl: uploadRes.data.url
            };
          } else {
            common_vendor.index.showToast({
              title: "上传收款码失败",
              icon: "none"
            });
            return;
          }
        } catch (error) {
          common_vendor.index.__f__("error", "at pages/me/invite-reward-page.vue:707", "上传收款码失败:", error);
          common_vendor.index.showToast({
            title: "上传收款码失败",
            icon: "none"
          });
          return;
        }
      }
      try {
        const res = await appServer.CreateWithdrawal({
          amount: parseFloat(this.withdrawAmount),
          paymentMethod: this.paymentMethod,
          paymentDetails
        });
        if (res && res.code === 0) {
          common_vendor.index.showToast({
            title: this.$t("invite.applySuccess"),
            icon: "success"
          });
          this.closeApplyModal();
          this.loadData();
        } else {
          common_vendor.index.showToast({
            title: ((_a = res.error) == null ? void 0 : _a.message) || "提现申请失败",
            icon: "none"
          });
        }
      } catch (error) {
        common_vendor.index.__f__("error", "at pages/me/invite-reward-page.vue:740", "提现申请失败:", error);
        common_vendor.index.showToast({
          title: "提现申请失败",
          icon: "none"
        });
      }
    },
    handleScroll(e) {
    }
  },
  onPageScroll(e) {
    const scrollTop = e.scrollTop;
    if (scrollTop > 100) {
      this.navbarBgColor = "linear-gradient(135deg, #FF3B4E 0%, #FF6B7A 100%)";
    } else {
      const opacity = scrollTop / 100;
      this.navbarBgColor = `rgba(255, 59, 78, ${opacity})`;
    }
  }
};
if (!Array) {
  const _easycom_up_popup2 = common_vendor.resolveComponent("up-popup");
  const _easycom_u_qrcode2 = common_vendor.resolveComponent("u-qrcode");
  (_easycom_up_popup2 + _easycom_u_qrcode2)();
}
const _easycom_up_popup = () => "../../node-modules/uview-plus/components/u-popup/u-popup.js";
const _easycom_u_qrcode = () => "../../node-modules/uview-plus/components/u-qrcode/u-qrcode.js";
if (!Math) {
  (_easycom_up_popup + _easycom_u_qrcode)();
}
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return common_vendor.e({
    a: $options.pageStyle,
    b: common_assets._imports_0,
    c: common_vendor.o((...args) => $options.goBack && $options.goBack(...args)),
    d: common_vendor.t(_ctx.$t("invite.title")),
    e: $data.navbarBgColor,
    f: common_assets._imports_1$3,
    g: common_vendor.t(_ctx.$t("invite.rewardTitle")),
    h: common_vendor.t(_ctx.$t("invite.rewardDesc")),
    i: common_vendor.t(_ctx.$t("invite.stepsTitle")),
    j: common_assets._imports_2$2,
    k: common_vendor.t(_ctx.$t("invite.step1")),
    l: common_assets._imports_3,
    m: common_vendor.t(_ctx.$t("invite.step2")),
    n: common_assets._imports_4$1,
    o: common_vendor.t(_ctx.$t("invite.step3")),
    p: common_vendor.t(_ctx.$t("invite.viewDetail")),
    q: common_vendor.o((...args) => $options.showDetail && $options.showDetail(...args)),
    r: common_vendor.t(_ctx.$t("invite.generateQRCode")),
    s: common_vendor.o((...args) => $options.generateQRCode && $options.generateQRCode(...args)),
    t: common_vendor.t(_ctx.$t("invite.shareToFriend")),
    v: common_vendor.t(_ctx.$t("invite.withdrawRecord")),
    w: common_vendor.t(_ctx.$t("invite.withdrawPeriod")),
    x: common_assets._imports_5$1,
    y: common_vendor.o((...args) => $options.goToWithdrawDetail && $options.goToWithdrawDetail(...args)),
    z: common_assets._imports_6$1,
    A: common_vendor.f($data.withdrawRecords, (item, index, i0) => {
      return {
        a: common_vendor.t(item.amount),
        b: common_vendor.t(item.status),
        c: index
      };
    }),
    B: common_vendor.t(_ctx.$t("common.currency")),
    C: common_vendor.t(_ctx.$t("invite.applyWithdraw")),
    D: common_vendor.o((...args) => $options.applyWithdraw && $options.applyWithdraw(...args)),
    E: common_vendor.t(_ctx.$t("invite.inviteRecord")),
    F: common_assets._imports_7$1,
    G: common_vendor.t(_ctx.$t("invite.username")),
    H: common_vendor.t(_ctx.$t("invite.uid")),
    I: common_vendor.t(_ctx.$t("invite.inviteTime")),
    J: common_vendor.t(_ctx.$t("invite.paid")),
    K: common_vendor.f($data.inviteRecords, (item, index, i0) => {
      return common_vendor.e({
        a: common_vendor.t(item.username),
        b: common_vendor.t(item.uid),
        c: common_vendor.t(item.time),
        d: item.paid && item.amount
      }, item.paid && item.amount ? {
        e: common_vendor.t(item.amount)
      } : {}, {
        f: index
      });
    }),
    L: $data.inviteRecords.length === 0
  }, $data.inviteRecords.length === 0 ? {
    M: common_vendor.t(_ctx.$t("common.noData"))
  } : {}, {
    N: common_vendor.t(_ctx.$t("invite.withdrawDetail")),
    O: common_assets._imports_4,
    P: common_vendor.o((...args) => $options.closeWithdrawModal && $options.closeWithdrawModal(...args)),
    Q: common_vendor.t(_ctx.$t("invite.time")),
    R: common_vendor.t(_ctx.$t("invite.amount")),
    S: common_vendor.t(_ctx.$t("invite.status")),
    T: common_vendor.f($data.withdrawDetailList, (item, index, i0) => {
      return {
        a: common_vendor.t(item.time),
        b: common_vendor.t(item.amount),
        c: common_vendor.t(item.status),
        d: index
      };
    }),
    U: common_vendor.o($options.closeWithdrawModal),
    V: common_vendor.p({
      show: $data.showWithdrawModal,
      mode: "center",
      round: 20,
      overlay: true,
      closeOnClickOverlay: true,
      bgColor: "#ffffff"
    }),
    W: common_vendor.t(_ctx.$t("invite.qrcodeTitle") || "邀请好友，赢现金"),
    X: $data.showQRCodeContent
  }, $data.showQRCodeContent ? {
    Y: common_vendor.sr("uQrcode", "54a6e18d-2,54a6e18d-1"),
    Z: common_vendor.p({
      val: $data.qrcodeValue,
      size: 200,
      unit: "px",
      ["load-make"]: true,
      ["using-components"]: true
    })
  } : {}, {
    aa: common_vendor.t(_ctx.$t("invite.shareToFriend") || "分享给好友"),
    ab: common_vendor.t(_ctx.$t("invite.saveImage") || "保存图片"),
    ac: common_vendor.o((...args) => $options.saveQRCodeImage && $options.saveQRCodeImage(...args)),
    ad: common_vendor.o($options.closeQRCodeModal),
    ae: common_vendor.o($options.onQRCodePopupOpen),
    af: common_vendor.p({
      show: $data.showQRCodeModal,
      mode: "center",
      round: 20,
      overlay: true,
      closeOnClickOverlay: true,
      bgColor: "#ffffff"
    }),
    ag: $data.showApplyModal
  }, $data.showApplyModal ? common_vendor.e({
    ah: common_vendor.t(_ctx.$t("invite.withdrawApplication")),
    ai: common_vendor.t($data.applyStep),
    aj: $data.applyStep === 1
  }, $data.applyStep === 1 ? {
    ak: common_assets._imports_6$1,
    al: common_vendor.t(_ctx.$t("invite.enterAmount")),
    am: _ctx.$t("invite.enterPlaceholder"),
    an: $data.withdrawAmount,
    ao: common_vendor.o(($event) => $data.withdrawAmount = $event.detail.value),
    ap: common_vendor.t(_ctx.$t("common.currency")),
    aq: common_vendor.t(_ctx.$t("invite.amountHint")),
    ar: common_vendor.t(_ctx.$t("invite.nextStep")),
    as: common_vendor.o((...args) => $options.nextStep && $options.nextStep(...args))
  } : {}, {
    at: $data.applyStep === 2
  }, $data.applyStep === 2 ? common_vendor.e({
    av: common_assets._imports_6$1,
    aw: common_vendor.t(_ctx.$t("invite.selectPaymentMethod")),
    ax: common_vendor.t(_ctx.$t("invite.wechat")),
    ay: $data.paymentMethod === "wechat" ? 1 : "",
    az: common_vendor.o(($event) => $options.selectPaymentMethod("wechat")),
    aA: common_vendor.t(_ctx.$t("invite.alipay")),
    aB: $data.paymentMethod === "alipay" ? 1 : "",
    aC: common_vendor.o(($event) => $options.selectPaymentMethod("alipay")),
    aD: common_vendor.t(_ctx.$t("invite.bankCard")),
    aE: $data.paymentMethod === "bank" ? 1 : "",
    aF: common_vendor.o(($event) => $options.selectPaymentMethod("bank")),
    aG: $data.paymentMethod !== "bank"
  }, $data.paymentMethod !== "bank" ? common_vendor.e({
    aH: common_vendor.t(_ctx.$t("invite.uploadQRCode")),
    aI: $data.qrcodeImage
  }, $data.qrcodeImage ? {
    aJ: $data.qrcodeImage
  } : {}, {
    aK: common_vendor.o((...args) => $options.uploadQRCode && $options.uploadQRCode(...args))
  }) : {}, {
    aL: $data.paymentMethod === "bank"
  }, $data.paymentMethod === "bank" ? {
    aM: common_vendor.t(_ctx.$t("invite.enterBankInfo")),
    aN: common_vendor.t(_ctx.$t("invite.bankCardNumber")),
    aO: _ctx.$t("invite.enterBankCardNumber"),
    aP: $data.bankCardNumber,
    aQ: common_vendor.o(($event) => $data.bankCardNumber = $event.detail.value),
    aR: common_vendor.t(_ctx.$t("invite.cardholderName")),
    aS: _ctx.$t("invite.enterCardholderName"),
    aT: $data.cardholderName,
    aU: common_vendor.o(($event) => $data.cardholderName = $event.detail.value),
    aV: common_vendor.t(_ctx.$t("invite.bankName")),
    aW: _ctx.$t("invite.enterBankName"),
    aX: $data.bankName,
    aY: common_vendor.o(($event) => $data.bankName = $event.detail.value),
    aZ: common_vendor.t(_ctx.$t("invite.swiftCode")),
    ba: common_vendor.t(_ctx.$t("invite.optional")),
    bb: _ctx.$t("invite.enterSwiftCode"),
    bc: $data.swiftCode,
    bd: common_vendor.o(($event) => $data.swiftCode = $event.detail.value)
  } : {}, {
    be: common_vendor.t(_ctx.$t("invite.applyWithdraw")),
    bf: common_vendor.o((...args) => $options.submitWithdraw && $options.submitWithdraw(...args))
  }) : {}, {
    bg: common_vendor.o(() => {
    }),
    bh: common_vendor.o((...args) => $options.closeApplyModal && $options.closeApplyModal(...args))
  }) : {});
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-54a6e18d"]]);
_sfc_main.__runtimeHooks = 7;
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/me/invite-reward-page.js.map
