/**
 * @description 路由配置模块，定义应用所有页面的路由映射与导航守卫
 * @author D吕贺034244311
 * @date 20260620
 */
import { createRouter, createWebHashHistory } from 'vue-router'

// 主要框架
import Layout from '@/layout/index.vue'

// 登录框架
import LoginLayout from '@/views/login/components/LoginLayout.vue'

export const constantRoutes = [{
		path: '/redirect',
		component: Layout,
		hidden: true,
		children: [{
			path: '/redirect/:path(.*)',
			component: () => import('@/views/redirect/index.vue')
		}]
	},

	{
		path: '/login',
		component: LoginLayout,
		hidden: true,
		children: [{
				path: '/login',
				name: 'Login',
				component: () => import('@/views/login/index.vue'),
				hidden: true
			},
			{
				path: '/register',
				name: 'Register',
				component: () => import('@/views/login/register.vue'),
				hidden: true
			}
		]
	},

	{
		path: '/404',
		component: () => import('@/views/error-page/404.vue'),
		hidden: true
	},
	{
		path: '/401',
		component: () => import('@/views/error-page/401.vue'),
		hidden: true
	},
	{
		path: '/',
		component: Layout,
		redirect: '/dashboard',
		children: [{
			path: 'dashboard',
			component: () => import('@/views/dashboard/index.vue'),
			name: 'Dashboard',
			meta: {
				title: '控制台',
				icon: 'dashboard',
				affix: true
			}
		}, ]
	},

	{
		path: '/profile',
		component: Layout,
		redirect: '/profile/index',
		hidden: true,
		children: [{
			path: 'index',
			component: () => import('@/views/profile/index.vue'),
			name: 'Profile',
			meta: {
				title: '个人资料',
				icon: 'user',
				noCache: true
			}
		}]
	}
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [

	{
		path: '/sys',
		component: Layout,
		redirect: '/sys/config',
		name: 'Sys',
		meta: {
			title: '系统设置',
			icon: 'configure',
			roles: ['root']
		},
		children: [{
				path: 'config',
				component: () => import('@/views/sys/config/index.vue'),
				name: 'SysConfig',
				meta: {
					title: '系统配置',
					icon: 'theme'
				}
			},
			{
				path: 'dict',
				component: () => import('@/views/sys/dict/index.vue'),
				name: 'SysDict',
				meta: {
					title: '码值管理',
					icon: 'theme'
				}
			},
			{
				path: 'role',
				component: () => import('@/views/sys/role/index.vue'),
				name: 'SysRole',
				meta: {
					title: '角色管理',
					icon: 'role'
				}
			},

			{
				path: 'user',
				component: () => import('@/views/sys/user/index.vue'),
				name: 'SysUser',
				meta: {
					title: '用户管理',
					icon: 'admin'
				}
			},

      {
        path: 'bookgl',
        component: () => import('@/views/sys/bookgl/index.vue'),
        name: 'SysBookgl',
        meta: {
          title: '书籍信息管理',
          icon: 'admin'
        }
      },
      {
        path: 'jieshu',
        component: () => import('@/views/sys/jieshu/index.vue'),
        name: 'SysJieshu',
        meta: {
          title: '图书搜索',
          icon: 'admin'
        }
      },

      {
        path: 'huanshu',
        component: () => import('@/views/sys/huanshu/index.vue'),
        name: 'Syshuanshu',
        meta: {
          title: '我的借还',
          icon: 'admin'
        }
      },
      {
        path: 'jiehuanjilu',
        component: () => import('@/views/sys/jiehuanjilu/index.vue'),
        name: 'SysJiehuanjilu',
        meta: {
          title: '借还记录',
          icon: 'admin'
        }
      },{
        path: 'gggl',
        component: () => import('@/views/sys/gggl/index.vue'),
        name: 'SysGggl',
        meta: {
          title: '公告管理',
          icon: 'admin'
        }
      },
      {
        path: 'lygl',
        component: () => import('@/views/sys/liuyangl/index.vue'),
        name: 'SysLygl',
        meta: {
          title: '留言管理',
          icon: 'admin'
        }
      },

		]
	},
  {
    path: '/sys',
    component: Layout,
    redirect: '/sys/config',
    name: 'student',
    meta: {
      title: '用户系统',
      icon: 'configure',
      roles: ['student']
    },
    children: [
      {
        path: 'jieshu',
        component: () => import('@/views/sys/jieshu/index.vue'),
        name: 'SysJieshu',
        meta: {
          title: '图书搜索',
          icon: 'admin'
        }
      },

      {
        path: 'huanshu',
        component: () => import('@/views/sys/huanshu/index.vue'),
        name: 'Syshuanshu',
        meta: {
          title: '我的借还',
          icon: 'admin'
        }
      },
      {
        path: 'jiehuanjilu',
        component: () => import('@/views/sys/jiehuanjilu/index.vue'),
        name: 'SysJiehuanjilu',
        meta: {
          title: '借还记录',
          icon: 'admin'
        }
      }
      ,
      {
        path: 'liuyan',
        component: () => import('@/views/sys/stuliuyan/index.vue'),
        name: 'SysStuliuyan',
        meta: {
          title: '留言板块',
          icon: 'admin'
        }
      }
      ,
      {
        path: 'gonggao',
        component: () => import('@/views/sys/stugggl/index.vue'),
        name: 'SysStugggl',
        meta: {
          title: '公告板块',
          icon: 'admin'
        }
      }

    ]
  },

	// 404 page must be placed at the end !!!
	{
		path: '/:pathMatch(.*)*',
		redirect: '/dashboard',
		hidden: true
	}
]

const router = createRouter({
	history: createWebHashHistory(),
	scrollBehavior: () => ({ top: 0 }),
	routes: constantRoutes
})

export function resetRouter() {
	const newRouter = createRouter({
		history: createWebHashHistory(),
		scrollBehavior: () => ({ top: 0 }),
		routes: constantRoutes
	})
	// 注意：Vue Router 4 没有 matcher，通过移除所有路由再重新添加来重置
	const currentRoutes = router.getRoutes()
	currentRoutes.forEach(route => {
		if (route.name && !constantRoutes.find(cr => cr.name === route.name)) {
			router.removeRoute(route.name)
		}
	})
}

export default router
