<template>
  <el-config-provider :locale="zhCn">
    <app-page v-if="hasLogin" />
    <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 { useLogin } from '@/business/login'
import { loginStore } from '@/stores'
import zhCn from 'element-plus/lib/locale/lang/zh-cn'
import eventBus from '@/services/bus'

const { token } = loginStore.$mapGetters()
const { userLogout } = useLogin()
const route = useRoute()
const router = useRouter()
const hasLogin = ref(false)

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

watch(() => route.name, (routeName) => {
  if (routeName === 'boot' || routeName === 'login' || !token.value) {
    hasLogin.value = false
  } else {
    hasLogin.value = true
  }
})
</script>