<template>
  <el-config-provider :locale="zhCn">
    <app-page v-if="isReady" />
    <router-view v-else />
  </el-config-provider>
</template>

<!--<script lang="ts">
export default {
  name: 'App',
}
</script>-->

<script lang="ts" setup>
import { ref, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessageBox } from 'element-plus'
import { logout } from '@/business/common'
import { useLoginStore } from '@/stores'
import zhCn from 'element-plus/lib/locale/lang/zh-cn'
import eventBus from '@/services/bus'

const { getToken } = useLoginStore()
const route = useRoute()
const router = useRouter()
const isReady = ref(false)

// 接收用户登出通知
eventBus.$on('LogoutNotify', (msg) => {
  logout(() => {
    if (msg) {
      ElMessageBox.alert(msg as string)
    }
    router.replace({ name: 'login' })
  })
})

watch(() => route.name, (routeName) => {
  const token = getToken()
  if (routeName !== 'boot' && token) {
    isReady.value = true
  } else {
    isReady.value = false
  }
})
</script>