<script setup>
import { RouterView } from 'vue-router'
import { ref, onMounted, onUnmounted, nextTick } from 'vue'
import Confirm from '@/plugins/confirm'
import axios from 'axios'

const openCheckVersion = true // 开启检测版本
const timer = ref(null)
const setIntervalTime = 300000 // 300000 五分钟
onMounted(() => {
  nextTick(() => {
    console.log('baseurl:', import.meta.env?.VITE_BASE_URL, 'version:', import.meta.env?.VITE_VERSION, process?.env?.NODE_ENV)
    //
    const process_ENV = process?.env?.NODE_ENV || ''
    if ((process_ENV === 'production' || process_ENV === 'development') && openCheckVersion) {
      if (timer.value) clearInterval(timer.value)
      timer.value = setInterval(() => { checkVersion() }, setIntervalTime)
    }
  })
})
onUnmounted(() => {
  if (timer.value) clearInterval(timer.value)
})

// 检查版本号
const checkVersion = () => {
  const baseUrl = import.meta.env.VITE_BASE_URL || ''
  const version = import.meta.env?.VITE_VERSION || ''
  const tenantId = import.meta.env?.VITE_TENANTCODE || ''
  // 
  if (!baseUrl || !version || !tenantId) return
  axios.get(`${baseUrl}/app-api/menduner/system/get/version`, {
    headers: { ['tenant-id']: tenantId }
  }).then((res) => {
    if (res?.data?.data !== version) {
      const option = {
        autoCloseTime: setIntervalTime > 15000 ? setIntervalTime - 10000 : setIntervalTime >= 5000 ? setIntervalTime - 1000 : setIntervalTime
      }
      Confirm('系统提示', '发现新版本，是否立即刷新页面', option).then(() => {
        window.location.reload()
      })
    }
  }).catch(err => {
    console.log('checkVersion-err', err)
  })
}
</script>

<template>
  <v-app>
    <RouterView />
  </v-app>
</template>

<style scoped>
</style>
