<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, ElNotification } from 'element-plus'
import { useLogin } from '@/business/login'
import { i18n, useSettingStore } from "@/stores"
import eventBus from '@/services/bus'
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'

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

// 国际化语言
const locale = computed(() => {
  switch (i18n.global.locale) {
    case 'zh-CN':
      return zhCn
    case 'zh-TW':
      return zhTW
    case 'th':
      return th
    case 'vi':
      return vi
    default:
      return en
  }
})

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

// 接收风控通知
eventBus.$on('RiskToWebNtf', (msg, type) => {
  const res = msg as { title: string, content: string }
  if (type === 1) {
    ElNotification({
      title: res.title,
      message: res.content,
      type: 'warning'
    })
  } else {
    ElMessageBox.alert(res.content, res.title)
  }
})

// 接收委托单成交通知
eventBus.$on('OrderDealedNtf', (res) => {
  if (settingStore.getSettingValue('showOrderSuccessNotify')) {
    const data = res as Proto.OrderDealedNtf
    ElNotification({
      title: '订单成交通知',
      message: `订单 ${data.OrderID} 已成交，价格 ${data.TradePrice}`
    })
  }
})
</script>