import { createRouter, createWebHashHistory } from "vue-router";
import Cookies from "js-cookie";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

let history = createWebHashHistory("/admin");

let routes = [
  {
    path: "/",
    name: "Common",
    component: () => import("../layouts/AdminLayout.vue"),
    redirect: "/home",
    children: [
      {
        path: "/home",
        name: "Home",
        meta: {
          title: "首頁輪播",
        },
        component: () => import("../pages/Index/banner/Index.vue"),
      },
      {
        path: "/index/banner",
        name: "IndexBanner",
        meta: {
          title: "首頁輪播",
        },
        component: () => import("../pages/Index/banner/Index.vue"),
      },
      {
        path: "/index/supplier",
        name: "IndexSupplier",
        meta: {
          title: "供應商",
        },
        component: () => import("../pages/Index/supplier/Index.vue"),
      },     
      {
        path: "/priv/admin",
        name: "PrivAdmin",
        meta: {
          title: "管理員列表",
        },
        component: () => import("../pages/Priv/admin/index.vue"),
      },
      {
        path: "/priv/adminlog",
        name: "PrivAdminlog",
        meta: {
          title: "操作紀錄",
        },
        component: () => import("../pages/Priv/adminLog/index.vue"),
      },
      {
        path: "/about/company",
        name: "AboutCompany",
        meta: {
          title: "公司簡介",
        },
        component: () => import("../pages/About/company/Index.vue"),
      },      
      {
        path: "/about/concept",
        name: "AboutConcept",
        meta: {
          title: "經營理念",
        },
        component: () => import("../pages/About/concept/Index.vue"),
      },
      {
        path: "/news/list",
        name: "News",
        meta: {
          title: "產業動態",
        },
        component: () => import("../pages/News/list/index.vue"),
      },  
      {
        path: "/news/list/form",
        name: "NewsForm",
        meta: {
          title: "新增產業動態",
        },
        component: () => import("../pages/News/list/form.vue"),
      },  
      {
        path: "/news/esg",
        name: "ESG",
        meta: {
          title: "ESG",
        },
        component: () => import("../pages/News/esg/index.vue"),
      },  
      {
        path: "/news/esg/form",
        name: "EsgForm",
        meta: {
          title: "ESG表單",
        },
        component: () => import("../pages/News/esg/form.vue"),
      },  
      {
        path: "/news/activity",
        name: "newsActivity",
        meta: {
          title: "公司活動",
        },
        component: () => import("../pages/News/activity/Index.vue"),
      },
      {
        path: "/product/list",
        name: "ProductList",
        meta: {
          title: "產品列表",
        },
        component: () => import("../pages/Product/list/index.vue"),
      },      
      {
        path: "/product/list/form",
        name: "ProductForm",
        meta: {
          title: "產品表單",
        },
        component: () => import("../pages/Product/list/form.vue"),
      },      
      {
        path: "/product/group",
        name: "ProductGroup",
        meta: {
          title: "產品群組",
        },
        component: () => import("../pages/Product/group/Index.vue"),
      },  
      {
        path: "/contact/email",
        name: "ContactEmail",
        meta: {
          title: "表單信件",
        },
        component: () => import("../pages/Contact/email/Index.vue"),
      },  
      {
        path: "/Contact/map",
        name: "ContactMap",
        meta: {
          title: "公司據點",
        },
        component: () => import("../pages/Contact/map/Index.vue"),
      },  
      {
        path: "/site/config",
        name: "SiteConfig",
        meta: {
          title: "站台設定",
        },
        component: () => import("../pages/Site/config.vue"),
      },    
    ],
  },
  {
    path: "/login",
    name: "Login",
    meta: {
      title: "登入",
    },
    component: () => import("../pages/Login/index.vue"),
  },
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    meta: {
      title: "404",
    },
    component: () => import("../pages/NotFound/index.vue"),
  },
];

const router = createRouter({
  history,
  routes,
  scrollBehavior(to, from, savedPosition) {
    return { top: 0 };
  },
});

const whiteList = ["/login","/error","/404"];

router.beforeEach((to, from, next) => {
  NProgress.start();
  const path = to.path;
  //判斷是否在whiteList中
  if (!whiteList.includes(path)) {
    if (!sessionStorage.getItem("token")) {
      return next("/login");
    }

    //檢查是否有語系,沒有則設定為繁體中文
    if (!Cookies.get("lang")) {
      Cookies.set("lang", "tw");
    }

    // }else{
      // // 檢查account store是否有資料
      // const accountStore = useAccountStore();
      // if (!accountStore.user) {
      //   accountStore.getAgentUserInfo();
      // }
    // }
  }
  
  return next();
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
