"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 _sfc_main = {
  data() {
    return {
      activeTab: "all",
      loading: false,
      notificationList: [],
      unreadCount: { all: 0, system: 0, activity: 0, service: 0 },
      pagination: { page: 1, limit: 10, total: 0 },
      hasMore: true
    };
  },
  onLoad() {
    this.fetchUnreadCount();
    this.fetchNotifications();
  },
  methods: {
    back() {
      common_vendor.index.navigateBack({ delta: 1 });
    },
    switchTab(tab) {
      if (this.activeTab === tab)
        return;
      this.activeTab = tab;
      this.pagination.page = 1;
      this.notificationList = [];
      this.hasMore = true;
      this.fetchNotifications();
    },
    async fetchUnreadCount() {
      try {
        const appserver = new modules_api_AppServer.AppServer();
        const res = await appserver.GetNotificationUnreadCount();
        if (res.code === 0 && res.data) {
          this.unreadCount = res.data;
        }
      } catch (e) {
        common_vendor.index.__f__("error", "at pages/me/notification-page.vue:103", "获取未读数量失败:", e);
      }
    },
    async fetchNotifications() {
      var _a;
      if (this.loading)
        return;
      this.loading = true;
      try {
        const appserver = new modules_api_AppServer.AppServer();
        const params = {
          page: this.pagination.page,
          limit: this.pagination.limit
        };
        if (this.activeTab !== "all") {
          params.type = this.activeTab;
        }
        const res = await appserver.GetNotifications(params);
        if (res.code === 0) {
          const newList = res.data || [];
          if (this.pagination.page === 1) {
            this.notificationList = newList;
          } else {
            this.notificationList = [...this.notificationList, ...newList];
          }
          this.pagination.total = ((_a = res.pagination) == null ? void 0 : _a.total) || 0;
          this.hasMore = newList.length >= this.pagination.limit;
        }
      } catch (e) {
        common_vendor.index.__f__("error", "at pages/me/notification-page.vue:130", "获取通知列表失败:", e);
      } finally {
        this.loading = false;
      }
    },
    loadMore() {
      if (!this.hasMore || this.loading)
        return;
      this.pagination.page++;
      this.fetchNotifications();
    },
    async readNotification(item) {
      if (item.isRead)
        return;
      try {
        const appserver = new modules_api_AppServer.AppServer();
        await appserver.MarkNotificationAsRead(item.id);
        item.isRead = true;
        this.unreadCount.all = Math.max(0, this.unreadCount.all - 1);
        this.unreadCount[item.type] = Math.max(0, this.unreadCount[item.type] - 1);
      } catch (e) {
        common_vendor.index.__f__("error", "at pages/me/notification-page.vue:150", "标记已读失败:", e);
      }
    },
    async markAllRead() {
      if (this.unreadCount.all === 0) {
        common_vendor.index.showToast({ title: this.$t("notification.allRead") || "已全部已读", icon: "none" });
        return;
      }
      try {
        const appserver = new modules_api_AppServer.AppServer();
        await appserver.MarkAllNotificationsAsRead();
        this.notificationList.forEach((item) => {
          item.isRead = true;
        });
        this.unreadCount = { all: 0, system: 0, activity: 0, service: 0 };
        common_vendor.index.showToast({ title: this.$t("common.success"), icon: "success" });
      } catch (e) {
        common_vendor.index.__f__("error", "at pages/me/notification-page.vue:166", "标记全部已读失败:", e);
        common_vendor.index.showToast({ title: this.$t("common.failed") || "操作失败", icon: "none" });
      }
    },
    formatTime(timestamp) {
      if (!timestamp)
        return "";
      const now = Date.now();
      const time = new Date(timestamp).getTime();
      const diff = now - time;
      const minutes = Math.floor(diff / (1e3 * 60));
      const hours = Math.floor(diff / (1e3 * 60 * 60));
      const days = Math.floor(diff / (1e3 * 60 * 60 * 24));
      if (minutes < 1)
        return this.$t("notification.justNow") || "刚刚";
      if (minutes < 60)
        return (this.$t("notification.minutesAgo") || "{n}分钟前").replace("{n}", minutes);
      if (hours < 24)
        return (this.$t("notification.hoursAgo") || "{n}小时前").replace("{n}", hours);
      return (this.$t("notification.daysAgo") || "{n}天前").replace("{n}", days);
    }
  }
};
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return common_vendor.e({
    a: common_assets._imports_0,
    b: common_vendor.o((...args) => $options.back && $options.back(...args)),
    c: common_vendor.t(_ctx.$t("me.notification")),
    d: common_vendor.t(_ctx.$t("notification.markAllRead")),
    e: common_vendor.o((...args) => $options.markAllRead && $options.markAllRead(...args)),
    f: common_vendor.t(_ctx.$t("notification.all")),
    g: $data.activeTab === "all" ? 1 : "",
    h: $data.unreadCount.all > 0
  }, $data.unreadCount.all > 0 ? {
    i: common_vendor.t($data.unreadCount.all)
  } : {}, {
    j: $data.activeTab === "all" ? 1 : "",
    k: common_vendor.o(($event) => $options.switchTab("all")),
    l: common_vendor.t(_ctx.$t("notification.system")),
    m: $data.activeTab === "system" ? 1 : "",
    n: $data.unreadCount.system > 0
  }, $data.unreadCount.system > 0 ? {
    o: common_vendor.t($data.unreadCount.system)
  } : {}, {
    p: $data.activeTab === "system" ? 1 : "",
    q: common_vendor.o(($event) => $options.switchTab("system")),
    r: common_vendor.t(_ctx.$t("notification.activity")),
    s: $data.activeTab === "activity" ? 1 : "",
    t: $data.unreadCount.activity > 0
  }, $data.unreadCount.activity > 0 ? {
    v: common_vendor.t($data.unreadCount.activity)
  } : {}, {
    w: $data.activeTab === "activity" ? 1 : "",
    x: common_vendor.o(($event) => $options.switchTab("activity")),
    y: common_vendor.t(_ctx.$t("notification.service")),
    z: $data.activeTab === "service" ? 1 : "",
    A: $data.unreadCount.service > 0
  }, $data.unreadCount.service > 0 ? {
    B: common_vendor.t($data.unreadCount.service)
  } : {}, {
    C: $data.activeTab === "service" ? 1 : "",
    D: common_vendor.o(($event) => $options.switchTab("service")),
    E: common_vendor.f($data.notificationList, (item, k0, i0) => {
      return common_vendor.e({
        a: common_vendor.t(item.title),
        b: !item.isRead
      }, !item.isRead ? {} : {}, {
        c: common_vendor.t(item.content),
        d: common_vendor.t($options.formatTime(item.createdAt)),
        e: !item.isRead ? 1 : "",
        f: item.id,
        g: common_vendor.o(($event) => $options.readNotification(item), item.id)
      });
    }),
    F: $data.loading
  }, $data.loading ? {
    G: common_vendor.t(_ctx.$t("common.loading"))
  } : {}, {
    H: !$data.loading && !$data.hasMore && $data.notificationList.length > 0
  }, !$data.loading && !$data.hasMore && $data.notificationList.length > 0 ? {
    I: common_vendor.t(_ctx.$t("common.noMore") || "没有更多了")
  } : {}, {
    J: $data.notificationList.length === 0 && !$data.loading
  }, $data.notificationList.length === 0 && !$data.loading ? {
    K: common_assets._imports_1$1,
    L: common_vendor.t(_ctx.$t("notification.noNotification"))
  } : {}, {
    M: common_vendor.o((...args) => $options.loadMore && $options.loadMore(...args))
  });
}
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
wx.createPage(MiniProgramPage);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/me/notification-page.js.map
