import Vue from 'vue'
import VueRouter from 'vue-router'
// 加载条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.use(VueRouter);


const routes = [
    {
        path: '/',
        name: 'Index',
        component: () => import( '../views/Index.vue'),
        children: [
            {
                path: '/',
                name: 'Main',
                component: () => import('@/views/Main.vue'),
                meta: {
                    title: "首页",
                }
            }, {
                path: '/phoneSign',
                name: 'PhoneSign',
                component: () => import('@/views/PhoneSign.vue'),
                meta: {
                    title: "WiFi信号记录",
                }
            }, {
                path: '/wifiSign',
                name: 'WifiSign',
                component: () => import('@/views/WifiSign.vue'),
                meta: {
                    title: "WiFi信号记录",
                }
            }, {
                path: '/white',
                name: 'white',
                component: () => import('@/views/white.vue'),
                meta: {
                    title: "可信设备",
                }
            }, {
                path: '/GSign',
                name: 'GSign',
                component: () => import('@/views/GSign.vue'),
                meta: {
                    title: "0-6G扫描记录",
                }
            }, {
                path: '/equip',
                name: 'Equip',
                component: () => import('@/views/Equip.vue'),
                meta: {
                    title: "设备管理",
                }
            }, {
                path: '/qrcodemanage',
                name: 'qrcodemanage',
                component: () => import('@/views/Qrcodemanage.vue'),
                meta: {
                    title: "二维码管理",
                }
            }, {
                path: '/record',
                name: 'record',
                component: () => import('@/views/Record.vue'),
                meta: {
                    title: "监测记录",
                }
            }, {
                path: '/region',
                name: 'Region',
                component: () => import('@/views/Region.vue'),
                meta: {
                    title: "区域管理",
                }
            }, {
                path: '/hardware',
                name: 'Hardware',
                component: () => import('@/views/Hardware.vue'),
                meta: {
                    title: "固件管理",
                }
            }, {
                path: '/police',
                name: 'Police',
                component: () => import('@/views/Police.vue'),
                meta: {
                    title: "警务通管理",
                }
            }, {
                path: '/pwd',
                name: 'Pwd',
                component: () => import('@/views/Pwd.vue'),
                meta: {
                    title: "密码修改",
                }
            }, {
                path: '/basic',
                name: 'Basic',
                component: () => import('@/views/Basic.vue'),
                meta: {
                    title: "no use",
                }
            }, {
                path: '/3d',
                name: '3d',
                component: () => import('@/views/3d.vue'),
                meta: {
                    title: "no use",
                }
            },
            {
                path: '/3d2',
                name: '3d2',
                component: () => import('@/views/3d2.vue'),
                meta: {
                    title: "no use",
                }
            }
            , {
                path: '/createmeetingqrcode',
                name: 'Createmeetingqrcode',
                component: () => import('@/views/Createmeetingqrcode.vue'),
                meta: {
                    title: "二维码设置",
                }
            },{
                path: '/signEquip',
                name: 'signEquip',
                component: () => import('@/views/signEquip.vue'),
                meta: {
                    title: "无线设备管理",
                }
            },{
                path: '/tactics',
                name: 'tactics',
                component: () => import('@/views/tactics.vue'),
                meta: {
                    title: "扫描策略管理",
                }
            },{
                path: '/plan',
                name: 'plan',
                component: () => import('@/views/plan.vue'),
                meta: {
                    title: "任务计划管理",
                }
            },{
                path: '/task',
                name: 'task',
                component: () => import('@/views/task.vue'),
                meta: {
                    title: "扫描任务管理",
                }
            },{
                path: '/statistics',
                name: 'statistics',
                component: () => import('@/views/statistics.vue'),
                meta: {
                    title: "监控记录统计",
                }
            },{
                path: '/wirelessStatics',
                name: 'wirelessStatics',
                component: () => import('@/views/wirelessStatics.vue'),
                meta: {
                    title: "无线信号统计",
                }
            },{
                path: '/wirelessStatistics',
                name: 'wirelessStatistics',
                component: () => import('@/views/wirelessStatistics.vue'),
                meta: {
                    title: "无线信号统计",
                }
            },{
                path: '/addTactics',
                name: 'addTactics',
                component: () => import('@/views/addTactics.vue'),
                meta: {
                    title: "新增策略",
                }
            },{
                path: '/setting',
                name: 'setting',
                component: () => import('@/views/Setting.vue'),
                meta: {
                    title: "系统设定",
                }
            },
        ]
    }, {
        path: '/login',
        name: 'Login',
        component: () => import( '../views/Login.vue')
    }, {
        path: '/wifiSign',
        name: 'wifiSign',
        component: () => import( '../components/WifiSign.vue')
    }, {
        path: '/equipInfo',
        name: 'equipInfo',
        component: () => import( '../components/EquipInfo.vue')
    }, {
        path: '/poster',
        name: 'Poster',
        component: () => import( '../views/Poster.vue')
    },  {
        path: '/test',
        name: 'test',
        component: () => import( '../views/test.vue')
    }, {
        path: '*',
        name: '404',
        component: () => import( '../views/404.vue')
    },
];

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
};

const router = new VueRouter({
    // mode: 'history',
    mode: 'hash',
    base: process.env.BASE_URL,
    routes
});

// 路由守卫
router.beforeEach((to, from, next) => {
    NProgress.start();
    const isLogin = localStorage.token ? true : false;

    if (to.path == '/login' || to.path == '/register' || to.path == '/poster') {//'login'和'register'相当于是路由可信设备
        next();
    } else {
        //如果token存在，就正常跳转，如果不存在，则说明未登陆，则跳转到'login'
        isLogin ? next() : next("/login");
        // next();
    }
});

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

export default router
