import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { useStore } from "@/stores/counter";
import { el } from 'element-plus/es/locales.mjs';
// import { log } from 'console';
// import { log } from 'console';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/login',
      name:'login',
      component: () => import('../views/LoginView.vue')
    },
    {
      path:'/home',
      name:'home',
      component:() => import('../views/HomeView.vue'),
      redirect:"/home/algorithem",
      children:[
        {
          path:"algorithem",
          name:"algorithem",
          meta:{
            isShow:true,
            title:"算法管理"
          },
          component:()=> import('../views/AlgorithemView.vue')
        },

        {
          path:"field",
          name:"field",
          meta:{
            isShow:true,
            title:"领域管理"
          },
          component:()=> import('../views/FieldView.vue')
        },
        {
          path:"use",
          name:"use",
          meta:{
            isShow:true,
            title:"场景管理"
          },
          component:()=> import('../views/UseView.vue')
        }
      ]
    },
    {
      path:"/",
      redirect:"/login"
    }
  


  ]
})
router.beforeEach((to,from,next)=>{
  const store = useStore();
  const storeuser = store.auth.user
  // console.log(store.auth.user);
  // const username :string | null = sessionStorage.getItem("username")
  if(!storeuser && to.path !== '/login'){
    console.log(33);
    console.log(store.auth.user);
    
    // console.log("username")
    // console.log(username);
    next('/login')
  }else if(storeuser != null && to.matched.length === 0) {
    next('/home')
  }else if(storeuser && to.path === '/login'){
    next('/home')
  }else{
    next()
  }
  
})
export default router
