import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
// import { usePermissStore } from '../store/permiss';
import Home from '../views/home.vue';
import NProgress from 'nprogress';
import i18n from '@/utils/i18n'
import 'nprogress/nprogress.css';

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/user',
    },
    {
        path: '/',
        name: 'Home',
        component: Home,
        children: [
            {
                path: '/user',
                name: 'user',
                meta: {
                    title: 'userManagement'
                },
                component: () => import('@/views/pages/user.vue'),
            },
            {
                path: '/dashboard',
                name: 'dashboard',
                meta: {
                    title: 'dataAnalysis'
                },
                component: () => import('@/views/pages/dashboard.vue'),
            },
            {
                path: '/rule',
                name: 'rule',
                meta: {
                    title: 'ruleSetting'
                },
                component: () => import('@/views/pages/rule.vue'),
            },
            {
              path: '/theme',
              name: 'theme',
              meta: {
                  title: 'themeSetting'
              },
              component: () => import(/* webpackChunkName: "theme" */ '../views/pages/theme.vue'),
          }
        ],
    },
    {
        path: '/login',
        meta: {
            title: 'login',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "login" */ '../views/pages/login.vue'),
    },
    {
        path: '/403',
        meta: {
            title: 'noAuthority',
        },
        component: () => import(/* webpackChunkName: "403" */ '../views/pages/403.vue'),
    },
    {
        path: '/404',
        meta: {
            title: 'notFound',
        },
        component: () => import(/* webpackChunkName: "404" */ '../views/pages/404.vue'),
    },
    { path: '/:path(.*)', redirect: '/404' },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    NProgress.start();
    if (typeof document !== 'undefined') {
      document.title = to.meta.title?  i18n.global.t(to.meta.title as string) : i18n.global.t('management') 
    }
    const role = localStorage.getItem('token');
    // const permiss = usePermissStore();
    if (!role && to.meta.noAuth !== true) {

        next('/login');
        /* else if (typeof to.meta.permiss == 'string' && permiss.userAuth.includes(to.meta.permiss)) {
        // 如果没有权限，则进入403
        next('/403');
    }*/
    } else {
        next();
    }
});

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

export default router;
