<template></template>

<script setup>
import { useHead } from "#app"; //匯入Head
import { ref } from "vue";
import { onMounted } from "vue"; //開機自啟
import { useRouter } from "vue-router"; //匯入路徑
const { $api_host } = useNuxtApp(); //匯入API host
const router = useRouter(); // 匯入

definePageMeta({
  layout: false, // 禁用 layout
});

useHead({
  title: "Lamiter",
  meta: [
    { charset: "utf-8" },
    { "http-equiv": "X-UA-Compatible", content: "IE=edge" },
    {
      name: "viewport",
      content: "width=device-width, initial-scale=1, shrink-to-fit=no",
    },
    { name: "description", content: "" },
    { name: "author", content: "" },
  ],
  link: [
    {
      rel: "stylesheet",
      href: "https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i",
    }, // 確保 `styles.css` 位於 public/css 資料夾中
    { rel: "stylesheet", href: "/css/sb-admin-2.min.css" },
    { rel: "stylesheet", href: "/vendor/fontawesome-free/css/all.min.css" },
  ],
  script: [
    //{ src: 'https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js', crossorigin: 'anonymous' },
    { src: "/vendor/jquery/jquery.min.js" },
    { src: "/vendor/bootstrap/js/bootstrap.bundle.min.js" },
    { src: "/vendor/jquery-easing/jquery.easing.min.js" },
    { src: "/js/sb-admin-2.min.js" },
  ],
});

console.log($api_host);
var token; // 加上分號

//檢查token
function token_check() {
  // 检查本地存储中是否存在JWT令牌
  token = localStorage.getItem("token_TCM");
  console.log(token);
  // 確認有無token
  if (token != null) {
    $.ajax({
      type: "GET",
      url: $api_host + "/Users/token_check_user",
      headers: {
        Authorization: token,
      },
      success: function (response) {
        // 若層級為10 為BOSS
        if(response.level==10){
          router.push("/Lamiter_pages/");
        }
        // 若層級為9 為管理階層上層
        if(response.level==9){
          router.push("/Manage_pages/");
        }
      },
      error: function (xhr) {
        console.log("false");
      },
    });
  } else {
    console.log("pass");
    router.push("/Home_pages/");
  }
}

//開機自啟
onMounted(() => {
  token_check();
});
</script>
