<script setup lang="ts">
import * as echarts from 'echarts'
import { provide } from 'vue'
import { fetchGetUserInfo } from './api/user'
import { getToken } from './utils/user'
import { EnumStorageKey } from './enum'
import { getLocal } from './utils/storage'
import { fetchGetAgreementList, fetchGetAppConfig } from './api/config'

provide('echarts', echarts)

const userInfo = ref<any>({})
const agreementList = ref([])
const isShow = ref(false)
const token = getToken()

function getUserInfo() {
  fetchGetUserInfo().then((res) => {
    if (res.data) {
      userInfo.value = res.data
      isShow.value = true
    }
  })
  fetchGetAgreementList().then((res) => {
    if (res.data)
      agreementList.value = res.data
  })
  fetchGetAppConfig().then((res) => {
    if (res.data)
      window.localStorage.setItem('__APP_CONFIG__', JSON.stringify(res.data))
  })
}

const route = useRoute()

onMounted(() => {
  if (!token) {
    userInfo.value = getLocal(EnumStorageKey['user-info'])
    return
  }
  getUserInfo()
})
</script>

<template>
  <div v-if="isShow" id="app_inner">
    <router-view />
    <!-- <Watermark :user-name="userInfo.userName" /> -->
    <GrantPopup v-if="route.path !== '/agreement'" :grant-flag="userInfo.grantFlag" :agreement-list="agreementList" />
  </div>
</template>

<style scoped></style>
