import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import LayoutIndex from '@/components/layout/index.vue';

export const constantRoutes: Array<RouteRecordRaw> = [
    {
        path: '',
        redirect: '/login',
    },
    {
        path: '/login',
        name: 'Login',
        meta: {
            title: '登录',
            keepAlive: true,
            requireAuth: false,
        },
        component: () => import('~/views/login.vue'),
    },
    {
        path: '/entry',
        component: LayoutIndex,
        meta: {
            title: '入境检疫',
            keepAlive: true,
            requireAuth: true,
        },
        children: [
            {
                path: '/entry/details',
                name: 'details',
                meta: {
                    title: '详情',
                    keepAlive: true,
                    requireAuth: false,
                },
                component: () => import('~/views/entry/details.vue'),
            },
        ],
    },
    // 系统管理路由
    {
        path: '/system',
        component: LayoutIndex,
        meta: {
            title: '系统管理',
            keepAlive: true,
            requireAuth: true,
        },
        children: [
            {
                path: '/system/post',
                name: 'Post',
                meta: {
                    title: '岗位管理',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/system/post.vue'),
            },
            {
                path: '/system/staff',
                name: 'Staff',
                meta: {
                    title: '员工管理',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/system/staff.vue'),
            },
            {
                path: '/system/tenant',
                name: 'Tenant',
                meta: {
                    title: '租户管理',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/system/tenant.vue'),
            },
        ],
    },
    // 权限管理路由
    {
        path: '/permission',
        component: LayoutIndex,
        meta: {
            title: '权限管理',
            keepAlive: true,
            requireAuth: true,
        },
        children: [
            {
                path: '/permission/menu',
                name: 'MenuPermission',
                meta: {
                    title: '菜单权限管理',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/permission/menuPermission.vue'),
            },
            {
                path: '/permission/operation',
                name: 'OperationPermission',
                meta: {
                    title: '操作权限管理',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/permission/operationPermission.vue'),
            },
        ],
    },
    // 智能硬件路由
    {
        path: '/hardware',
        component: LayoutIndex,
        meta: {
            title: '智能硬件',
            keepAlive: true,
            requireAuth: true,
        },
        children: [
            {
                path: '/hardware/collar',
                name: 'Collar',
                meta: {
                    title: '智能项圈',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/hardware/collar.vue'),
            },
            {
                path: '/hardware/eartag',
                name: 'Eartag',
                meta: {
                    title: '智能耳标',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/hardware/eartag.vue'),
            },
            {
                path: '/hardware/host',
                name: 'Host',
                meta: {
                    title: '智能主机',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/hardware/host.vue'),
            },
        ],
    },
    // 用户管理路由
    {
        path: '/userManage',
        component: LayoutIndex,
        meta: {
            title: '用户管理',
            keepAlive: true,
            requireAuth: true,
        },
        children: [
            {
                path: '/userManage/user',
                name: 'UserManage',
                meta: {
                    title: '用户管理',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/userManage/user.vue'),
            },
            {
                path: '/userManage/driver',
                name: 'DriverManage',
                meta: {
                    title: '司机管理',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/userManage/driver.vue'),
            },
            {
                path: '/userManage/vehicle',
                name: 'VehicleManage',
                meta: {
                    title: '车辆管理',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/userManage/vehicle.vue'),
            },
        ],
    },
    // 早期预警路由
    {
        path: '/earlywarning',
        component: LayoutIndex,
        meta: {
            title: '早期预警',
            keepAlive: true,
            requireAuth: true,
        },
        children: [
            {
                path: '/earlywarning/earlywarninglist',
                name: 'EarlyWarningList',
                meta: {
                    title: '早期预警列表',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/earlywarning/list.vue'),
            },
        ],
    },
    // 运送清单路由
    {
        path: '/shipping',
        component: LayoutIndex,
        meta: {
            title: '运送清单',
            keepAlive: true,
            requireAuth: true,
        },
        children: [
            {
                path: '/shipping/loadingOrder',
                name: 'LoadingOrder',
                meta: {
                    title: '装车订单',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/shipping/loadingOrder.vue'),
            },
            

            {
                path: '/shipping/shippinglist',
                name: 'ShippingList',
                meta: {
                    title: '运送清单',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/entry/attestation.vue'),
            },
        ],
        
    },
];

export const dynamicRoutes = [
    {
        path: '/menu',
        name: 'Menu',
        meta: {
            title: '首页',
            keepAlive: true,
            requireAuth: false,
        },
        component: () => import('~/views/system/menu.vue'),
    },
];

const router = createRouter({
    history: createWebHistory(),
    routes: constantRoutes,
    scrollBehavior(to, from, savedPosition) {
        // 如果有保存的位置（浏览器前进/后退），使用保存的位置
        if (savedPosition) {
            return savedPosition;
        }
        // 否则滚动到顶部
        return { top: 0 };
    },
});

export default router;