"use strict";
const common_vendor = require("../../common/vendor.js");
const util_index = require("../../util/index.js");
const config_user = require("../../config/user.js");
const _sfc_main = {
  data() {
    return {
      depositAmount: "0.00",
      orderNo: "",
      records: [],
      orderId: ""
    };
  },
  onLoad() {
  },
  onShow() {
    this.loadUserInfo();
  },
  methods: {
    async loadUserInfo() {
      try {
        const res = await util_index.getUserInfo();
        common_vendor.index.__f__("log", "at pages/deposit/index.vue:64", "loadUserInfo", res);
        if (res.code === 200) {
          this.depositAmount = res.data.balanceAmount || "0.00";
          this.orderNo = res.data.latestOrderNo || "";
          this.orderId = res.data.latestOrderId || "";
          if (parseFloat(this.depositAmount) > 0 && this.orderNo) {
            this.records = [
              {
                type: "支付",
                time: this.formatDate(/* @__PURE__ */ new Date()),
                amount: this.depositAmount
              }
            ];
          } else {
            this.records = [];
          }
        }
      } catch (error) {
        common_vendor.index.__f__("error", "at pages/deposit/index.vue:84", "获取用户信息失败：", error);
        common_vendor.index.showToast({
          title: "获取用户信息失败",
          icon: "none"
        });
      }
    },
    async handleWithdraw() {
      if (parseFloat(this.depositAmount) <= 0) {
        common_vendor.index.showToast({
          title: "无可提现余额",
          icon: "none"
        });
        return;
      }
      if (this.orderId.length != 0 || this.orderNo.length != 0) {
        const res = await config_user.queryById(Number(this.orderId));
        common_vendor.index.__f__("log", "at pages/deposit/index.vue:101", res);
      }
      common_vendor.index.showModal({
        title: "确认提现",
        content: "押金将原路退回，预计0-7个工作日到账",
        success: async (res) => {
          if (res.confirm) {
            common_vendor.index.showLoading({
              title: "提现中..."
            });
            try {
              common_vendor.index.__f__("log", "at pages/deposit/index.vue:122", "发起提现请求，订单号：", this.orderNo);
              const result = await config_user.withdrawDeposit(this.orderNo);
              common_vendor.index.__f__("log", "at pages/deposit/index.vue:124", "提现响应：", result);
              if (result.code === 200) {
                common_vendor.index.hideLoading();
                common_vendor.index.showToast({
                  title: "提现申请已提交",
                  icon: "success"
                });
                this.depositAmount = "0.00";
                this.records.push({
                  type: "退还",
                  time: this.formatDate(/* @__PURE__ */ new Date()),
                  amount: this.depositAmount
                });
                setTimeout(() => {
                  this.loadUserInfo();
                }, 1500);
              } else {
                throw new Error(result.msg || "提现失败");
              }
            } catch (error) {
              common_vendor.index.__f__("error", "at pages/deposit/index.vue:149", "提现失败：", error);
              common_vendor.index.hideLoading();
              let errorMessage = "提现失败，请稍后再试";
              if (error.message) {
                if (error.message.includes("尚未归还")) {
                  errorMessage = "当前订单尚未归还，请归还后再提现";
                } else if (error.message.includes("已退还")) {
                  errorMessage = "押金已退还，无需重复提现";
                } else if (error.message.includes("处理中")) {
                  errorMessage = "押金退还处理中，请耐心等待";
                } else if (error.message.includes("余额为0")) {
                  errorMessage = "账户余额为0，无法提现";
                } else {
                  errorMessage = error.message;
                }
              }
              common_vendor.index.showModal({
                title: "提现失败",
                content: errorMessage,
                showCancel: false
              });
            }
          }
        }
      });
    },
    formatDate(date) {
      const year = date.getFullYear();
      const month = (date.getMonth() + 1).toString().padStart(2, "0");
      const day = date.getDate().toString().padStart(2, "0");
      const hours = date.getHours().toString().padStart(2, "0");
      const minutes = date.getMinutes().toString().padStart(2, "0");
      return `${year}-${month}-${day} ${hours}:${minutes}`;
    }
  }
};
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return common_vendor.e({
    a: common_vendor.t($data.depositAmount),
    b: common_vendor.o((...args) => $options.handleWithdraw && $options.handleWithdraw(...args)),
    c: $data.depositAmount <= 0,
    d: $data.records.length > 0
  }, $data.records.length > 0 ? {
    e: common_vendor.f($data.records, (item, index, i0) => {
      return {
        a: common_vendor.t(item.type),
        b: common_vendor.t(item.time),
        c: common_vendor.t(item.type === "退还" ? "+" : "-"),
        d: common_vendor.t(item.amount),
        e: common_vendor.n(item.type === "退还" ? "refund" : ""),
        f: index
      };
    })
  } : {});
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-c7792057"]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/deposit/index.js.map
