<script setup>
import { ref, onMounted } from 'vue';
import serverConfig from "./serverConfig"
// import AppHeader from './components/AppHeader.vue';
// import language from './assets/language/index.js'
const userInfo = useUserInfoStore()
const layout = ref("default")
onMounted(() => {
  setTimeout(() => {
    loginData()
  }, 10);
  window.onresize = () => {
    let w = window.innerWidth
    console.log(window.innerWidth)
    if (w <= 690) {
      layout.value = "mobile"
    } else {
      layout.value = "default"
    }
  }
})
console.log(process)
const loginData = async () => {
  let LoginState = await useFetch(serverConfig.host + '/api/state', {
    method: 'GET',
    credentials: 'include',
    mode: 'cors',
    // cache: false
  }).then(r => {
    return r
  })
  console.log(LoginState)
  if (LoginState.data.value == "Logged") {
    Error.value = false
    await useFetch(serverConfig.host + '/api/profile', {
      method: 'GET',
      credentials: 'include',
      mode: 'cors',
      // cache: false
    }).then(r => {
      if (r.data.value && r.data.value.username) {
        let data = r.data.value
        let obj = {
          username: data.username,
          alias: data.alias,
          userid: data.userid,
          isLoading: true,
          googleId: data.gender,
          login: true,
          gender: data.gender,
          defaultSchool: data.defaultSchool,
          defaultAddress: data.defaultAddress,
          apiKey: data.apiKey
        }
        userInfo.value = obj
        try {
          window.parent.postMessage({ id: 'loginVerify', status: 'logged', data }, "*");
        } catch (error) {
          console.log(error);
        }
      }
    })
  }
}

</script>
<template>
  <div>
    <NuxtLayout :name="layout">
    </NuxtLayout>
  </div>
</template>
