<template>
  <div class="overflow-x-hidden">
      <!-- 手机版 -->
        <MHeader/>
          <slot />
        <MFooter/>
  </div>
</template>

<script setup lang="ts">
import { useMenu, useSubMenu, useWebsite} from "~/composables/configState";
import MHeader from "~/components/MHeader.vue";
import MFooter from "~/components/MFooter.vue";
import {useI18n} from "#imports";
import {getSiteInfo} from "~/api/layout";
import {getTenantIdByDomain} from "~/api/cms/cmsDomain";

// 加载状态
const website = useWebsite()
const menu = useMenu()
const subMenu = useSubMenu()
const i18n = useI18n()


// 挂载钩子
onMounted(() => {
  //必须在onMounted的时候才能用local和window
  // getLoacl();
  window.onbeforeunload = () => {
    //离开页面时保存数据，由于可能突发情况，所以重要数据请手动调用setLocal函数
    // setLocal(); //如果需要调试本地存储数据，记得把这个注释一下
  };
});


// 加载数据
const reload = async () => {
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading'
  })

  // TODO 1 查询当前域名是否合法
  const domain = window.location.hostname;

  if (process.env.NODE_ENV === 'production') {
    getTenantIdByDomain({
      domain
    }).then(data => {
      if(data?.count && data.count > 0){
        console.log('域名已授权 => ',data.list[0].domain)
        localStorage.setItem('TenantId',`${data?.list[0].tenantId}`)
      }
    })
  }

  // TODO 2 读取服务器缓存数据
  await getSiteInfo({
    lang: i18n.locale.value
  }).then(data => {
    website.value = data
    localStorage.setItem('SiteName',`${data.websiteName}`);
    if (data.topNavs) {
      menu.value = data.topNavs;
    }
    if (data.bottomNavs) {
      subMenu.value = data.bottomNavs;
    }
    if(data.config){
      localStorage.setItem('Domain',data.config.Domain);
      localStorage.setItem('SysDomain',data.config.SysDomain);
    }
    // 站点状态
    if(data.running != 1){
      showError({
        statusCode: data.running,
        message: data.statusText
      })
    }
  })
  loading.close();
}

reload()
</script>

