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: 'details',  // ✅ 修复：使用相对路径
                name: 'details',
                meta: {
                    title: '详情',
                    keepAlive: true,
                    requireAuth: false,
                },
                component: () => import('~/views/entry/details.vue'),
            },
            {
                path: 'devices',  // ✅ 修复：使用相对路径
                name: 'devices',
                meta: {
                    title: '设备管理',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/entry/devices.vue'),
            },
        ],
    },
    // 系统管理路由
    {
        path: '/system',
        component: LayoutIndex,
        meta: {
            title: '系统管理',
            keepAlive: true,
            requireAuth: true,
        },
        children: [
            {
                path: 'post',  // ✅ 修复：使用相对路径
                name: 'Post',
                meta: {
                    title: '岗位管理',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/system/post.vue'),
            },
            {
                path: 'staff',  // ✅ 修复：使用相对路径
                name: 'Staff',
                meta: {
                    title: '员工管理',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/system/staff.vue'),
            },
            {
                path: '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: 'menu',  // ✅ 修复：使用相对路径
                name: 'MenuPermission',
                meta: {
                    title: '菜单权限管理',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/permission/menuPermission.vue'),
            },
            {
                path: '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: 'collar',  // ✅ 修复：使用相对路径
                name: 'Collar',
                meta: {
                    title: '智能项圈',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/hardware/collar.vue'),
            },
            {
                path: 'eartag',  // ✅ 修复：使用相对路径
                name: 'Eartag',
                meta: {
                    title: '智能耳标',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/hardware/eartag.vue'),
            },
            {
                path: '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: 'user',  // ✅ 修复：使用相对路径
                name: 'UserManage',
                meta: {
                    title: '用户管理',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/userManage/user.vue'),
            },
            {
                path: 'driver',  // ✅ 修复：使用相对路径
                name: 'DriverManage',
                meta: {
                    title: '司机管理',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/userManage/driver.vue'),
            },
            {
                path: '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: '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: 'shippinglist',  // ✅ 修复：使用相对路径
                name: 'ShippingList',
                meta: {
                    title: '运送清单',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/entry/attestation.vue'),
            },
            {
                path: 'loadingOrder',  // ✅ 修复：使用相对路径
                name: 'LoadingOrder',
                meta: {
                    title: '订单信息',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/shipping/loadingOrder.vue'),
            },
            

           
        ],
        
    },
    // 销售概览路由
    {
        path: '/salesOverview',
        component: LayoutIndex,
        meta: {
            title: '销售概览',
            keepAlive: true,
            requireAuth: true,
        },
        children: [
            {
                path: 'list',  // ✅ 修复：使用相对路径
                name: 'SalesOverviewList',
                meta: {
                    title: '销售概览',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/salesOverview/list.vue'),
            },
        ],
    },
    // 中转仓管理路由
    {
        path: '/warehouse',
        component: LayoutIndex,
        meta: {
            title: '中转仓管理',
            keepAlive: true,
            requireAuth: true,
        },
        children: [
            {
                path: 'warehouseList',  // ✅ 修复：使用相对路径
                name: 'WarehouseList',
                meta: {
                    title: '中转仓管理',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/warehouse/warehouse.vue'),
            },
            {
                path: 'warehouseIn',  // ✅ 修复：使用相对路径
                name: 'WarehouseIn',
                meta: {
                    title: '进仓管理',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/warehouse/warehouseIn.vue'),
            },
            {
                path: 'warehouseOut',  // ✅ 修复：使用相对路径
                name: 'WarehouseOut',
                meta: {
                    title: '出仓管理',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/warehouse/warehouseOut.vue'),
            },
        ],
    },
    // 屠宰场管理路由
    {
        path: '/slaughter',
        component: LayoutIndex,
        meta: {
            title: '屠宰场管理',
            keepAlive: true,
            requireAuth: true,
        },
        children: [
            {
                path: 'house',
                name: 'house',
                meta: {
                    title: '屠宰场管理',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/slaughter/house.vue'),
            },
            {
                path: 'entry',
                name: 'entry',
                meta: {
                    title: '进场管理',
                    keepAlive: true,
                    requireAuth: true,
                },
                component: () => import('~/views/slaughter/entry.vue'),
            },
        ],
    },
    {
        path: '/datav',
        name: 'DataV',
        meta: {
            title: '数据大屏',
            keepAlive: true,
            requireAuth: false,
        },
        component: () => import('~/views/datav/ChinaMapParticle.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;