<template>
  <div class="container">
    <NuxtLayout>
        <NuxtPage/>
    </NuxtLayout>
  </div>
</template>


<script setup>
import { ID_INJECTION_KEY } from 'element-plus'
import { useFiltersStore } from "@/store/filter.js";

const filtersStore = useFiltersStore();

useHead({
  link: [
    { rel: 'icon', type: 'image/x-icon', href: '/favicon.png' },
 ],
 script: [
      {
        src: 'https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit',
        type:'text/javascript',
        async: true,
        body: true,
      },
    ]
})


const setHead = () => {
  get('v3/system/front/setting/getSettings?names=gz_code').then(res=>{
    if(!res.data[0] || res.data[0] == ''){
      return
    }
    useHead({
        script: [
              {
                src: "https://www.googletagmanager.com/gtm.js?id="+ res.data[0],
                type:'text/javascript',
                async: true,
              },
            ]
        })
  })
}
setHead()

provide(ID_INJECTION_KEY, {
  prefix: 100,
  current: 0,
})

const googleTranslateInit = () => {
  let checkIfGoogleLoaded = setInterval(() => {
    if (window.google && window.google.translate?.TranslateElement != null) {
        filtersStore.setGoogleTanslateStatus('1')
        clearInterval(checkIfGoogleLoaded);
      googleTranslateElement('google_translate_element');
    }
    }, 1000);
}

const googleTranslateElement = (id) => {
  new window.google.translate.TranslateElement({pageLanguage: 'en',layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, id);
}

onMounted(() => {
  nextTick(()=>{
    googleTranslateInit()
  })

  window.addEventListener('storage', function (e) {
    if (e.key === 'isLoggedIn') {
      location.reload();  // 其他页面刷新
    }
  });
})


</script>
