import {createRouter, createWebHistory} from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import Layout from '@/layout/index.vue'
import {getToken} from '../utils/auth'
import {usePermisstionStroe} from '@/stores/permisstion.js'
import {useAuthStore} from '@/stores/userstore.js'

NProgress.configure({showSpinner: false})

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/login',
            name: 'login',
            component: () => import('@/views/login/index.vue'),
            meta: {
                hidden: true,
                title: '登录'
            }
        },
        {
            path: '/cas/login',
            name: 'casLogin',
            component: () => import('@/views/cas-login/index.vue'),
        },
        {
            path: '/projectdetail/mobile',
            name: 'projectDetailMobile',
            component: () => import('@/views/project-management/mobledetail/index.vue')
        },
        {
            path: '/fund/mobile',
            name: 'specialFundDetailMobile',
            component: () => import('@/views/project-management/mobledetail/SpecialFundDetailMobile.vue')
        },
        {
            path: '/projectDemand/requirement/moblie',
            name: 'projectDemandRequirementMoblie',
            component: () => import('@/views/project-demand/requirement/moblieDetail/index.vue')
        },
        {
            path: '/expenseManagement/share/moblie',
            name: 'expenseManagementMoblie',
            component: () => import('@/views/expense-management/share/moblieDetail/index.vue')
        },
        {
            path: '/phase/detail/moblie',
            name: 'phaseDetailMoblie',
            component: () => import('@/views/project-management/phaseDetailMoblie/index.vue')
        },
        {
            path: '/',
            name: 'layout',
            component: Layout,
            redirect: '/home',
            meta: {
                hidden: false
            },
            children: [
                {
                    path: '/home',
                    name: 'home',
                    component: () => import('@/views/home/index.vue'),
                    meta: {
                        title: '首页',
                        breadcrumb: true
                    }
                },
                {
                    path: '/auth',
                    name: 'auth',
                    component: () => import('@/views/auth/index.vue'),
                    meta: {
                        title: '个人中心',
                        breadcrumb: true
                    }
                },
                {
                    path: '/workflow/process/edit/:deploymentId',
                    name: 'processEdit',
                    component: () => import('@/views/workflow/process/ProcessEdit.vue'),
                    meta: {
                        title: '编辑流程',
                        breadcrumb: true
                    }
                },
                {
                    path: '/workflow/process',
                    name: 'process',
                    component: () => import('@/views/workflow/process/index.vue'),
                    meta: {
                        title: '流程管理',
                        breadcrumb: true
                    }
                },
                {
                    path: '/workflow/process/add',
                    name: 'processAdd',
                    component: () => import('@/views/workflow/process/ProcessEdit.vue'),
                    meta: {
                        title: '新增流程',
                        breadcrumb: true
                    }
                },
                {
                    path: '/role-auth/user/:roleId(\\d+)/:roleName',
                    name: 'distribute',
                    component: () => import('@/views/system/role/DistributeUser.vue'),
                    meta: {
                        title: '角色分配用户',
                        breadcrumb: true
                    }
                },
                {
                    path: '/post-auth/user/:postId(\\d+)/:postName',
                    name: 'assignUser',
                    component: () => import('@/views/system/post/DistributeUser.vue'),
                    meta: {
                        title: '岗位分配用户',
                        breadcrumb: true
                    }
                },
                {
                    path: '/menu-auth/role/:menuId(\\d+)/:menuName',
                    name: 'assignRole',
                    component: () => import('@/views/system/menu/DistributeRole.vue'),
                    meta: {
                        title: '菜单分配角色',
                        breadcrumb: true
                    }
                },
                {
                    path: '/system/notice/inform/index/:queryId',
                    name: 'notify',
                    component: () => import('@/views/system/notice/inform/index.vue'),
                    meta: {
                        title: '通知公告',
                        breadcrumb: false
                    }
                },
                // 项目详情
                {
                    path: '/project/management/implementation/implementation/detail',
                    name: 'Implementation/detail',
                    component: () => import('@/views/project-management/implementation/detail.vue'),
                    meta: {
                        title: '项目详情',
                        breadcrumb: false
                    }
                },
                // 需求征集详情
                {
                    path: '/project/demand/requirement/requirement/detail',
                    name: 'Requirement/detail',
                    component: () => import('@/views/project-demand/requirement/detail.vue'),
                    meta: {
                        title: '需求征集-详情',
                        breadcrumb: false
                    }
                },
                // 需求汇总详情
                {
                    path: '/project/demand/summary/summary/detail',
                    name: 'Summary/detail',
                    component: () => import('@/views/project-demand/summary/detail.vue'),
                    meta: {
                        title: '需求汇总-详情',
                        breadcrumb: false
                    }
                },
                // 专项资金详情
                {
                    path: '/special/fund/fund/detail',
                    name: 'Fund/detail',
                    component: () => import('@/views/special-fund/detail.vue'),
                    meta: {
                        title: '专项资金-详情',
                        breadcrumb: false
                    }
                },
                // 费用分摊详情
                {
                    path: '/expense/management/expense/share/share/detail',
                    name: 'Share/detail',
                    component: () => import('@/views/expense-management/share/detail.vue'),
                    meta: {
                        title: '费用分摊-详情',
                        breadcrumb: false
                    }
                },
                {
                    path: '/todolist',
                    name: 'Todolist',
                    component: () => import('@/views/todoList/index.vue'),
                    meta: {
                        title: '待办',
                        breadcrumb: false
                    }
                },
                {
                    path: '/donelist',
                    name: 'Donelist',
                    component: () => import('@/views/doneList/index.vue'),
                    meta: {
                        title: '已办',
                        breadcrumb: false
                    }
                },
            ]
        },
        {
            path: '/forbidden',
            name: 'forbidden',
            component: () => import('@/views/forbidden/index.vue'),
        }
    ]
})

router.beforeEach(async (to, form, next) => {
    const permisstionStore = usePermisstionStroe()
    const authStore = useAuthStore()
    NProgress.start()
    if (!getToken()) {
        if (to.path === '/login' || to.path === '/cas/login' || to.path === '/forbidden') {
            next()
            NProgress.done()
        } else {
            let path = window.location.pathname;
            let query = window.location.search
            sessionStorage.setItem('toView', JSON.stringify({
                path: path,
                query: query
            }))
            window.location.href = `${window.location.origin}/api/auth/cas/login`
            // next({path: '/api/auth/cas/login'})
        }
    } else {
        if (to.path === '/login' || to.path === '/cas/login') {
            next('/')
            NProgress.done()
        } else {
            permisstionStore.setIsLoadRoutes(true)
            if (permisstionStore.isLoadRoutes && !permisstionStore.isSussessReq) {
                await permisstionStore.setAsyncRouters()
                await authStore.setUserInfo()
                next({...to, replace: true})
            } else {
                next()
            }
        }

    }


})

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

export default router
