import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { usePermissStore } from '../store/permiss';
import Home from '../views/home.vue';
import settings from '../utils/settings'

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/dashboard',
    },
    {
        path: '/',
        name: 'Home',
        component: Home,
        children: [
            {
                path: '/dashboard',
                name: 'dashboard',
                meta: {
                    title: '系统首页',
                    permiss: 'dashboard',
                },
                component: () => import('../views/dashboard.vue'),
            },


            {
                path: '/access-log',
                name: 'access-log',
                meta: {
                    title: '人员进出记录',
                    permiss: 'access-log',
                },
                component: () => import('../views/access-log.vue'),
            },
            {
                path: '/access-gate-setting',
                name: 'access-gate-setting',
                meta: {
                    title: '大门管理',
                    permiss: 'access-gate-setting',
                },
                component: () => import('../views/access-gate-setting.vue'),
            },


            {
                path: '/report-log',
                name: 'report-log',
                meta: {
                    title: '体温上报记录',
                    permiss: 'report-log',
                },
                component: () => import('../views/report-log.vue'),
            },


            {
                path: '/report-setting',
                name: 'report-setting',
                meta: {
                    title: '体温上报记录',
                    permiss: 'report-setting',
                },
                component: () => import('../views/report-log.vue'),
            },


            {
                path: '/shop-cate-setting',
                name: 'shop-cate-setting',
                meta: {
                    title: '分类管理',
                    permiss: 'shop-good-setting',
                },
                component: () => import('../views/shop-cate-setting.vue'),
            },
            {
                path: '/shop-good-setting',
                name: 'shop-good-setting',
                meta: {
                    title: '商品管理',
                    permiss: 'shop-good-setting',
                },
                component: () => import('../views/shop-good-setting.vue'),
            },
            {
                path: '/shop-order-setting',
                name: 'shop-order-setting',
                meta: {
                    title: '订单管理',
                    permiss: 'shop-order-setting',
                },
                component: () => import('../views/shop-order-setting.vue'),
            },


            {
                path: '/privilege-user-setting',
                name: 'privilege-user-setting',
                meta: {
                    title: '用户管理',
                    permiss: 'privilege-user-setting',
                },
                component: () => import('../views/privilege-user-setting.vue'),
            },
            {
                path: '/user',
                name: 'user',
                meta: {
                    title: '个人中心',
                },
                component: () => import('../views/user.vue'),
            },
        ],
    },
    {
        path: '/login',
        name: 'Login',
        meta: {
            title: '登录',
        },
        component: () => import('../views/login.vue'),
    },
    {
        path: '/403',
        name: '403',
        meta: {
            title: '没有权限',
        },
        component: () => import('../views/error-page/403.vue'),
    },
];

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

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | ${settings.siteTitle}`;
    const username = localStorage.getItem('ms_username');
    const roleId = localStorage.getItem('ms_role_id');
    const permiss = usePermissStore();
    const currentUserPermiss = permiss[roleId as string];
    // console.log("currentUserPermiss", currentUserPermiss)
    if (!username && to.path !== '/login') {
        next({
            path: '/login',
            query: {
                redirectTo: router.currentRoute.value.path // window.location.href
            },
        });
        return
    } else if (
        to.meta.permiss &&
        !(currentUserPermiss && currentUserPermiss.includes(to.meta.permiss as string))
    ) {
        // 如果没有权限，则进入403
        next('/403');
        return
    } else {
        next();
    }
});

export default router;
