<script setup lang="ts">
import { watch } from 'vue'
import { storeToRefs } from 'pinia'
import TheHeader from '@/components/layout/TheHeader.vue'
import TheFooter from '@/components/layout/TheFooter.vue'
import AppToast from '@/components/common/AppToast.vue'
import SystemAnnouncementModal from '@/components/common/SystemAnnouncementModal.vue'
import { useAuthStore } from '@/stores/auth'
import { useSystemAnnouncementStore } from '@/stores/systemAnnouncement'
import { registerSessionExpiredHandler } from '@/utils/request'

const authStore = useAuthStore()
const { isLoggedIn } = storeToRefs(authStore)
const sysAnnStore = useSystemAnnouncementStore()

// 当后端返回 4000（token 过期）或 HTTP 401 时，清除本地登录态
registerSessionExpiredHandler(() => authStore.clearLocalSession())

watch(isLoggedIn, (val) => {
  if (val) {
    sysAnnStore.check()
    authStore.fetchMyInfo().catch(() => {
      // 资料拉取失败不阻断公告；顶栏仍可用邮箱首字母占位
    })
  } else {
    sysAnnStore.reset()
  }
}, { immediate: true })
</script>

<template>
  <TheHeader />
  <RouterView />
  <TheFooter />
  <AppToast />
  <SystemAnnouncementModal />
</template>
