import { createRouter, createWebHashHistory } from 'vue-router';
import { useStore } from 'vuex';
import Home from '../views/home/home.vue';
import CarControl from '../views/CarControl.vue'; // 直接导入组件

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/loggin/loggin.vue')
  },
  {
    path: '/reset',
    name: 'reset',
    component: () => import('../views/loggin/reset.vue'),
    meta: { requiresAuth: false } // 不需要认证

  },
  {
    path: '/',
    name: 'Home',
    component: Home,
  },

  {
    path: '/recognition',
    name: 'recognition',
    component: () => import('../views/recognize/License-plate-recognition.vue')
  },
  {
    path: '/info',
    name: 'info',
    component: () => import('../views/system-info/info.vue')
  },
  {
    path: '/warning',
    name: 'warning',
    component: () => import('../views/forewarning/forewarning.vue')
  },
  {
    path: '/Footer',
    name: 'Footer',
    component: () => import('../components/Footer.vue')
  },
  {
    path: '/voiceset',
    name: 'voiceset',
    component: () => import('../views/voice/voiceset.vue')
  },
  {
    path: '/car_control',
    name: 'CarControl',
    component: CarControl, // 直接使用导入的组件，而不是使用动态导入
  },
  {
    path: '/audio-play',
    name: 'AudioPlay',
    component: () => import('../views/AudioPlay.vue'),
  },
  {
    path: '/TEST',
    name: 'TEST',
    component: () => import('../views/TEST.vue'),
  },
  {
    path: '/gateway-setting',
    name: 'GatewaySetting',
    component: () => import('../views/gateway/GatewaySetting.vue')
  },
  {
    path: '/cone-control',
    name: 'ConeControl',
    component: () => import('../views/ConeControl.vue')
  },
  {
    path: '/flipScreen',
    name: 'flipScreen',
    component: () => import('../views/flipScreen/flipScreen.vue')
  },
];

const router = createRouter({
  history: createWebHashHistory(), // 使用 hash 模式
  routes
});
router.beforeEach((to, from, next) => {
  const store = useStore(); // 获取 store 实例
  if (to.meta.requiresAuth === false) {
    next(); // 允许访问所有不需要认证的页面
  } else if (to.path !== '/login' && !store.state.isAuthenticated) {
    next('/login'); // 未认证，重定向到登录页面
  } else {
    next(); // 已认证，继续导航
  }
});

export default router;
