"use strict";
const common_vendor = require("../../common/vendor.js");
const utils_tabbarI18n = require("../../utils/tabbar-i18n.js");
const utils_auth = require("../../utils/auth.js");
const modules_api_AppServer = require("../../modules/api/AppServer.js");
const common_assets = require("../../common/assets.js");
const _sfc_main = {
  data() {
    return {
      showLanguagePicker: false,
      languages: [
        { code: "zh", name: "中文" },
        { code: "en", name: "English" },
        { code: "pt", name: "Português" }
      ],
      user: null,
      isLogin: false,
      appointmentStats: {
        pending: 0,
        inProgress: 0,
        completed: 0
      },
      unreadCount: 0
    };
  },
  computed: {
    currentLanguage() {
      return this.$i18n.locale;
    }
  },
  onLoad() {
    this.checkLoginStatus();
  },
  onShow() {
    utils_tabbarI18n.updateTabBarI18n(this);
    this.checkLoginStatus();
    if (this.isLogin) {
      this.refreshUserInfo();
      this.fetchAppointmentStats();
      this.fetchUnreadCount();
    }
  },
  methods: {
    /**
     * 检查登录状态
     */
    checkLoginStatus() {
      this.isLogin = utils_auth.isLoggedIn();
      if (this.isLogin) {
        this.user = utils_auth.getCurrentUser();
      }
    },
    /**
     * 从服务器刷新用户信息
     */
    async refreshUserInfo() {
      try {
        const appserver = new modules_api_AppServer.AppServer();
        const response = await appserver.GetUserProfile();
        if (response.code === 0 && response.data) {
          this.user = response.data;
          utils_auth.saveUserInfo(response.data);
        }
      } catch (error) {
        common_vendor.index.__f__("error", "at pages/me/me-page.vue:303", "刷新用户信息失败:", error);
      }
    },
    /**
     * 获取预约统计数据
     */
    async fetchAppointmentStats() {
      try {
        const appserver = new modules_api_AppServer.AppServer();
        const response = await appserver.GetAppointmentStats();
        if (response.code === 0 && response.data) {
          this.appointmentStats = {
            pending: response.data.pending || 0,
            inProgress: response.data.inProgress || 0,
            completed: response.data.completed || 0
          };
        }
      } catch (error) {
        common_vendor.index.__f__("error", "at pages/me/me-page.vue:323", "获取预约统计失败:", error);
      }
    },
    /**
     * 获取未读通知数量
     */
    async fetchUnreadCount() {
      try {
        const appserver = new modules_api_AppServer.AppServer();
        const response = await appserver.GetNotificationUnreadCount();
        if (response.code === 0 && response.data) {
          this.unreadCount = response.data.all || 0;
          this.updateTabBarBadge();
        }
      } catch (error) {
        common_vendor.index.__f__("error", "at pages/me/me-page.vue:341", "获取未读通知数量失败:", error);
      }
    },
    /**
     * 更新TabBar红点
     */
    updateTabBarBadge() {
      if (this.unreadCount > 0) {
        common_vendor.index.setTabBarBadge({
          index: 2,
          text: this.unreadCount > 99 ? "99+" : String(this.unreadCount)
        });
      } else {
        common_vendor.index.removeTabBarBadge({
          index: 2
        });
      }
    },
    getCurrentLanguageName() {
      const current = this.languages.find((lang) => lang.code === this.currentLanguage);
      return current ? current.name : "中文";
    },
    changeLanguage(langCode) {
      if (langCode === this.$i18n.locale) {
        this.showLanguagePicker = false;
        return;
      }
      this.$i18n.locale = langCode;
      common_vendor.index.setStorageSync("language", langCode);
      this.showLanguagePicker = false;
      common_vendor.index.showToast({
        title: this.$t("common.success"),
        icon: "success",
        duration: 1e3
      });
      setTimeout(() => {
        common_vendor.index.reLaunch({
          url: "/pages/index/index"
        });
      }, 1e3);
    },
    /**
     * 跳转到个人资料编辑页面（需要登录）
     */
    goToProfileEdit() {
      if (!utils_auth.requireAuth()) {
        return;
      }
      common_vendor.index.navigateTo({
        url: "/pages/me/profile-edit-page"
      });
    },
    /**
     * 跳转到通知页面（需要登录）
     */
    goToNotification() {
      if (!utils_auth.requireAuth()) {
        return;
      }
      common_vendor.index.navigateTo({
        url: "/pages/me/notification-page"
      });
    },
    /**
     * 跳转到我的预约页面（需要登录）
     */
    goToMyAppointments(tab = "all") {
      if (!utils_auth.requireAuth()) {
        return;
      }
      common_vendor.index.navigateTo({
        url: "/pages/me/my-appointments-page?tab=" + tab
      });
    },
    /**
     * 跳转到联系我们（不需要登录）
     */
    goToContactUs() {
      common_vendor.index.navigateTo({
        url: "/pages/me/contact-us-page"
      });
    },
    /**
     * 跳转到邀请奖励页面（需要登录）
     */
    goToInviteReward() {
      if (!utils_auth.requireAuth()) {
        return;
      }
      common_vendor.index.navigateTo({
        url: "/pages/me/invite-reward-page"
      });
    },
    /**
     * 跳转到关于我们页面（不需要登录）
     */
    goToAboutUs() {
      common_vendor.index.navigateTo({
        url: "/pages/me/about-us-page"
      });
    },
    /**
     * 跳转到用户协议页面（不需要登录）
     */
    goToUserAgreement() {
      common_vendor.index.navigateTo({
        url: "/pages/me/user-agreement-page"
      });
    },
    /**
     * 跳转到隐私政策页面（不需要登录）
     */
    goToPrivacyPolicy() {
      common_vendor.index.navigateTo({
        url: "/pages/me/privacy-policy-page"
      });
    },
    /**
     * 退出登录
     */
    handleLogout() {
      if (!this.isLogin) {
        common_vendor.index.navigateTo({
          url: "/pages/login/login-page"
        });
        return;
      }
      common_vendor.index.showModal({
        title: this.$t("common.confirm"),
        content: "确定要退出登录吗？",
        success: async (res) => {
          if (res.confirm) {
            await utils_auth.logout();
          }
        }
      });
    }
  }
};
if (!Array) {
  const _easycom_up_popup2 = common_vendor.resolveComponent("up-popup");
  _easycom_up_popup2();
}
const _easycom_up_popup = () => "../../node-modules/uview-plus/components/u-popup/u-popup.js";
if (!Math) {
  _easycom_up_popup();
}
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return common_vendor.e({
    a: $data.isLogin && $data.user
  }, $data.isLogin && $data.user ? {
    b: $data.user.avatar || "/static/default_avatar.png",
    c: common_vendor.t($data.user.nickname || "用户"),
    d: common_vendor.t($data.user.uid || ""),
    e: common_assets._imports_0$1,
    f: common_vendor.o((...args) => $options.goToProfileEdit && $options.goToProfileEdit(...args))
  } : {
    g: common_assets._imports_1$2,
    h: common_assets._imports_0$1,
    i: common_vendor.o((...args) => $options.handleLogout && $options.handleLogout(...args))
  }, {
    j: common_vendor.t($data.isLogin ? $data.appointmentStats.pending : "-"),
    k: common_vendor.t(_ctx.$t("me.appointment")),
    l: common_vendor.o(($event) => $options.goToMyAppointments("all")),
    m: common_vendor.t($data.isLogin ? $data.appointmentStats.inProgress : "-"),
    n: common_vendor.t(_ctx.$t("me.inProgress")),
    o: common_vendor.o(($event) => $options.goToMyAppointments("inProgress")),
    p: common_vendor.t($data.isLogin ? $data.appointmentStats.completed : "-"),
    q: common_vendor.t(_ctx.$t("me.completed")),
    r: common_vendor.o(($event) => $options.goToMyAppointments("completed")),
    s: common_assets._imports_2,
    t: common_assets._imports_1$1,
    v: common_vendor.t(_ctx.$t("me.notification")),
    w: $data.unreadCount > 0
  }, $data.unreadCount > 0 ? {
    x: common_vendor.t($data.unreadCount > 99 ? "99+" : $data.unreadCount)
  } : {}, {
    y: common_assets._imports_1,
    z: common_vendor.o((...args) => $options.goToNotification && $options.goToNotification(...args)),
    A: common_assets._imports_5,
    B: common_vendor.t(_ctx.$t("me.customerService")),
    C: common_assets._imports_1,
    D: common_assets._imports_6,
    E: common_vendor.t(_ctx.$t("me.contactUs")),
    F: common_assets._imports_1,
    G: common_vendor.o((...args) => $options.goToContactUs && $options.goToContactUs(...args)),
    H: common_assets._imports_7,
    I: common_vendor.t(_ctx.$t("me.inviteReward")),
    J: common_assets._imports_1,
    K: common_vendor.o((...args) => $options.goToInviteReward && $options.goToInviteReward(...args)),
    L: common_assets._imports_8,
    M: common_assets._imports_9,
    N: common_vendor.t(_ctx.$t("me.language")),
    O: common_vendor.t($options.getCurrentLanguageName()),
    P: common_assets._imports_1,
    Q: common_vendor.o(($event) => $data.showLanguagePicker = true),
    R: common_assets._imports_10,
    S: common_vendor.t(_ctx.$t("me.about")),
    T: common_assets._imports_1,
    U: common_vendor.o((...args) => $options.goToAboutUs && $options.goToAboutUs(...args)),
    V: common_assets._imports_11,
    W: common_vendor.t(_ctx.$t("me.userAgreement")),
    X: common_assets._imports_1,
    Y: common_vendor.o((...args) => $options.goToUserAgreement && $options.goToUserAgreement(...args)),
    Z: common_assets._imports_12,
    aa: common_vendor.t(_ctx.$t("me.privacyPolicy")),
    ab: common_assets._imports_1,
    ac: common_vendor.o((...args) => $options.goToPrivacyPolicy && $options.goToPrivacyPolicy(...args)),
    ad: common_assets._imports_13,
    ae: common_vendor.t($data.isLogin ? _ctx.$t("me.logout") : "登录"),
    af: common_assets._imports_1,
    ag: common_vendor.o((...args) => $options.handleLogout && $options.handleLogout(...args)),
    ah: common_vendor.t(_ctx.$t("me.language")),
    ai: common_vendor.o(($event) => $data.showLanguagePicker = false),
    aj: common_vendor.f($data.languages, (lang, k0, i0) => {
      return common_vendor.e({
        a: common_vendor.t(lang.name),
        b: $options.currentLanguage === lang.code
      }, $options.currentLanguage === lang.code ? {} : {}, {
        c: lang.code,
        d: common_vendor.o(($event) => $options.changeLanguage(lang.code), lang.code),
        e: $options.currentLanguage === lang.code ? 1 : ""
      });
    }),
    ak: common_vendor.o(($event) => $data.showLanguagePicker = false),
    al: common_vendor.p({
      show: $data.showLanguagePicker,
      mode: "bottom",
      ["border-radius"]: "20"
    })
  });
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/me/me-page.js.map
