<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 RwaView from '@/views/RwaView.vue'
import { useAuthStore } from '@/stores/auth'
import { useSystemAnnouncementStore } from '@/stores/systemAnnouncement'
import { useRwaModalStore } from '@/stores/rwaModal'
import { registerSessionExpiredHandler } from '@/utils/request'

const authStore = useAuthStore()
const { isLoggedIn } = storeToRefs(authStore)
const sysAnnStore = useSystemAnnouncementStore()
const rwaModalStore = useRwaModalStore()
const { showModal: showRwaModal } = storeToRefs(rwaModalStore)

// 当后端返回 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 />
  <Teleport to="body">
    <Transition name="modal-fade">
      <div v-if="showRwaModal" class="rwa-modal-overlay" @click.self="rwaModalStore.close()">
        <div class="rwa-modal-content">
          <button class="rwa-modal-close" @click="rwaModalStore.close()">
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <line x1="18" y1="6" x2="6" y2="18"/>
              <line x1="6" y1="6" x2="18" y2="18"/>
            </svg>
          </button>
          <RwaView />
        </div>
      </div>
    </Transition>
  </Teleport>
</template>

<style>
.modal-fade-enter-active,
.modal-fade-leave-active {
  transition: opacity 0.3s ease;
}

.modal-fade-enter-from,
.modal-fade-leave-to {
  opacity: 0;
}

.rwa-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}

.rwa-modal-content {
  position: relative;
  background: #1a1a1a;
  border-radius: 16px;
  width: 60%;
  min-height: 50vh;
  max-height: 80vh;
  overflow-y: auto;
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
}

.rwa-modal-close {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 36px;
  height: 36px;
  background: rgba(255, 255, 255, 0.08);
  border: none;
  border-radius: 50%;
  color: rgba(255, 255, 255, 0.7);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  z-index: 10;
}

.rwa-modal-close:hover {
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  transform: rotate(90deg);
}

.rwa-modal-content :deep(.rwa-view) {
  min-height: auto;
  padding: 48px 24px;
}

.rwa-modal-content :deep(.rwa-content) {
  padding: 0;
}

.rwa-modal-content :deep(.rwa-title) {
  font-size: 28px;
  margin-bottom: 16px;
}

.rwa-modal-content :deep(.rwa-desc) {
  font-size: 15px;
  line-height: 1.6;
}
</style>
