<template>
  <el-config-provider :locale="locale">
    <!-- 一级路由 -->
    <router-view />
  </el-config-provider>
</template>

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

<script lang="ts" setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessageBox,dayjs } from 'element-plus'
import { Language } from '@/constants/language'
import { useLogin } from '@/business/login'
import { i18n } from '@/stores'
import eventBus from '@/services/bus'
import historyRouter from './router/historyRouter'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import en from 'element-plus/dist/locale/en.mjs'
import th from 'element-plus/dist/locale/th.mjs'
import zhTW from 'element-plus/dist/locale/zh-tw.mjs'
import vi from 'element-plus/dist/locale/vi.mjs'
import updateLocale from 'dayjs/plugin/updateLocale'

// 扩展插件
dayjs.extend(updateLocale)
// 全局设置
dayjs.locale('en')
dayjs.updateLocale('en', { weekStart: 1 })

const { userLogout } = useLogin()
const router = useRouter()

// 国际化语言
const locale = computed(() => {
  switch (i18n.global.locale) {
    case Language.Simplified:
      return zhCn
    case Language.Thai:
      return th
    case Language.Traditional:
      return zhTW
    case Language.Vietnamese:
      return vi
    default:
      return en
  }
})

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