import Vue from 'vue'
import VueRouter from 'vue-router'

// const files = require.context('@/views', true, /-router\.js$/)
// let allRouter = [];
// // 匹配成功的名字数组
// files.keys().map(key => {
//     allRouter = allRouter.concat(files(key).default)
// })
// allRouter.sort((a, b) => {
//     return a.meta.sort - b.meta.sort
// })

// console.log(allRouter)

Vue.use(VueRouter)


// 避免冗余导航 （重复点击菜单栏报错问题）
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

const routes = [
    {
        path: '/',
        redirect: {
            name: 'realTimeMonitoring'
        },
        component: () => import('@/views/index'),
        children: [
            {
                path: 'videoWall1`',
                name: 'videoWall1`',
                meta: {
                    icon: 'desktop',
                    name: '视频墙1'
                },
                component: () => import('@/views/videoWall/index'),
            },
            {
                path: 'videoWall2',
                name: 'videoWall2',
                meta: {
                    icon: 'desktop',
                    name: '视频墙2'
                },
                component: () => import('@/views/videoWall2/index'),
            },
            {
                path: 'videoWall3',
                name: 'videoWall3',
                meta: {
                    icon: 'desktop',
                    name: '视频墙3'
                },
                component: () => import('@/views/videoWall3/index'),
            },
            {
                path: 'videoWall4',
                name: 'videoWall4',
                meta: {
                    icon: 'desktop',
                    name: '视频墙4'
                },
                component: () => import('@/views/videoWall4/index'),
            },
            {
                path: 'videoWall5',
                name: 'videoWall5',
                meta: {
                    icon: 'desktop',
                    name: '视频墙5'
                },
                component: () => import('@/views/videoWall5/index'),
            },
            {
                path: 'videoWall6',
                name: 'videoWall6',
                meta: {
                    icon: 'desktop',
                    name: '视频墙6'
                },
                component: () => import('@/views/videoWall6/index'),
            },
            {
                path: 'videoWall7',
                name: 'videoWall7',
                meta: {
                    icon: 'desktop',
                    name: '视频墙7'
                },
                component: () => import('@/views/videoWall7/index'),
            },
            {
                path: 'realTimeMonitoring',
                name: 'realTimeMonitoring',
                meta: {
                    icon: 'video-camera',
                    name: '实时视频流'
                },
                component: () => import('@/views/realTimeMonitoring/index'),

            },

            {
                path: 'videoPlayback',
                name: 'videoPlayback',
                meta: {
                    icon: 'tag',
                    name: '记录回放',
                },
                component: () => import('@/views/videoPlayback/index'),
            },
            {
                path: 'historyMonitoring',
                name: 'historyMonitoring',
                meta: {
                    icon: 'book',
                    name: '历史数据'
                },
                component: () => import('@/views/historyMonitoring/index')
            },
            {
                path: 'alarmLog',
                name: 'alarmLog',
                meta: {
                    icon: 'eye',
                    name: '告警日志'
                },
                component: () => import('@/views/alarmVideos/alarmLog')
            },
            {
                path: 'checkManage',
                name: 'checkManage',
                meta: {
                    icon: 'form',
                    name: '盘点管理',
                },
                component: () => import('@/views/checkManage/index'),
            },
            {
                path: 'checkOperation',
                name: 'checkOperation',
                meta: {
                    name: '盘点操作'
                },
                component: () => import('@/views/checkManage/checkOperation'),
            },
            {
                path: 'checkHistory',
                name: 'checkHistory',
                meta: {
                    icon: 'tag',
                    name: '盘点历史',
                },
                component: () => import('@/views/historyCheck/index'),
            },
            // {
            //     path: 'stockHistory',
            //     name: 'stockHistory',
            //     meta: {
            //         icon: 'file-excel',
            //         name: '库位历史',
            //     },
            //     component: () => import('@/views/stockLog/index'),
            // },
            // {
            //     path: 'stockLogDetail',
            //     name: 'stockLogDetail',
            //     meta: {
            //         name: '库位历史详情',
            //     },
            //     component: () => import('@/views/stockLog/logPage'),
            // },
            {
                path: 'roadwayManage',
                name: 'roadwayManage',
                meta: {
                    icon: 'build',
                    name: '巷道管理'
                },
                component: () => import('@/views/roadwayManage/index')
            },
            {
                path: 'cameraManage',
                name: 'cameraManage',
                meta: {
                    icon: 'camera',
                    name: '球机管理'
                },
                component: () => import('@/views/cameraManage/index')
            },
            {
                path: 'cameraManage/model',
                name: 'cameraManageModel',
                meta: {
                    name: '球机管理测试or配置'
                },
                component: () => import('@/views/cameraManage/model')
            },
            // {
            //     path: 'productSpecManage',
            //     name: 'productSpecManage',
            //     meta: {
            //         icon: 'pushpin',
            //         name: '品规管理'
            //     },
            //     component: () => import('@/views/productSpecManage/index')
            // },
            // {
            //     path: 'repertoryManage',
            //     name: 'repertoryManage',
            //     meta: {
            //         icon: 'wallet',
            //         name: '库存管理'
            //     },
            //     component: () => import('@/views/repertoryManage/index')
            // },

            {
                path: 'cameraManage/ioTable',
                name: 'ioTable',
                meta: {
                    name: 'IO表配置'
                },
                component: () => import('@/views/cameraManage/ioTable')
            },
            {
                path: 'center',
                name: 'center',
                meta: {
                    name: '个人中心'
                },
                component: () => import('@/views/center')
            },
            {
                path: 'plcStatus',
                name: 'plcStatus',
                meta: {
                    name: 'PLC状态'
                },
                component: () => import('@/views/plcStatus')
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/login.vue')
    },
    {
        path: '/guide',
        name: 'guide',
        component: () => import('../views/guide.vue')
    },
    {
        path: '/updateStyles',
        name: '修改样式',
        component: () => import('../views/updateStyles/index')
    },

    {
        path: '/camera/*',
        name: '修改样式',
        component: () => import('../views/camera.vue')
    },
    {
        path: '*',
        name: '404',
        component: () => import('../views/404.vue')
    }
]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    next()
    // let userInfo = JSON.parse(sessionStorage.getItem('userInfo') || '{}')
    // // 校验是否登录
    // if (userInfo.id) {
    //     // 匹配不到 404
    //     if (!to.name) next();
    //     // 权限
    //     if (userInfo.permissionList.some((item) => {
    //         return item.rights === to.name
    //     })) {
    //         next()
    //     } else {
    //         if (to.name === 'login') {
    //             next()
    //         } else {
    //             Vue.prototype.$message.info('没有权限')
    //             next(false)
    //         }
    //     }
    // } else {
    //     if (to.name === 'login') {
    //         next()
    //     } else if (to.name !== 'login') {
    //         Vue.prototype.$message.info('请先登录')
    //         next('/login')
    //     } else {
    //         next('/login')
    //     }
    // }
})

export default router
