import Vue from 'vue';
import Router from 'vue-router';
import Admin from '@/views/Admin';
import store from './store';
import http from './plugins/http';

Vue.use(Router);

const router = new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: [
        {
            path: '/',
            component: Admin,
            children: [
                {
                    path: '/',
                    redirect: 'chart1'
                },
                {
                    path: '/404',
                    name: '404',
                    component: () => import(/* webpackChunkName: "404" */ '@/views/404.vue')
                },
                {
                    path: '/dashboard',
                    name: 'dashboard',
                    component: () => import(/* webpackChunkName: "404" */ '@/views/chart1.vue'),
                    meta: {
                        title: '首页'
                    }
                },
                {
                    path: '/menus',
                    name: 'menus',
                    component: () => import(/* webpackChunkName: "menus" */ '@/views/Menus.vue'),
                    meta: {
                        title: '菜单配置'
                    }
                },
                {
                    path: '/menuAuthority',
                    name: 'menuAuthority',
                    component: () => import(/* webpackChunkName: "menuAuthority" */ '@/views/MenuAuthority.vue'),
                    meta: {
                        title: '菜单权限'
                    }
                },
                {
                    path: '/userEdit',
                    name: 'userEdit',
                    component: () => import(/* webpackChunkName: "userEdit" */ './views/UserEdit.vue'),
                    meta: {
                        title: '用户编辑'
                    }
                },
                {
                    path: '/userList',
                    name: 'userList',
                    component: () => import(/* webpackChunkName: "userList" */ '@/views/UserList.vue'),
                    meta: {
                        title: '用户管理'
                    }
                },
                {
                    path: '/orgNumStatList',
                    name: 'orgNumStatList',
                    component: () => import(/* webpackChunkName: "orgNumStatList" */ './views/OrgNumStatList.vue'),
                    meta: {
                        title: '企业数量'
                    }
                },
                {
                    path: '/orgNumStatEdit',
                    name: 'orgNumStatEdit',
                    component: () => import(/* webpackChunkName: "orgNumStatEdit" */ './views/OrgNumStatEdit.vue'),
                    meta: {
                        title: '企业数量编辑'
                    }
                },
                {
                    path: '/projectNumStatList',
                    name: 'projectNumStatList',
                    component: () =>
                        import(/* webpackChunkName: "projectNumStatList" */ './views/ProjectNumStatList.vue'),
                    meta: {
                        title: '项目数量'
                    }
                },
                {
                    path: '/projectNumStatEdit',
                    name: 'projectNumStatEdit',
                    component: () =>
                        import(/* webpackChunkName: "projectNumStatEdit" */ './views/ProjectNumStatEdit.vue'),
                    meta: {
                        title: '项目数量编辑'
                    }
                },
                {
                    path: '/cityIndexStatList',
                    name: 'cityIndexStatList',
                    component: () =>
                        import(/* webpackChunkName: "cityIndexStatList" */ './views/CityIndexStatList.vue'),
                    meta: {
                        title: '城市指数'
                    }
                },
                {
                    path: '/cityIndexStatEdit',
                    name: 'cityIndexStatEdit',
                    component: () =>
                        import(/* webpackChunkName: "cityIndexStatEdit" */ './views/CityIndexStatEdit.vue'),
                    meta: {
                        title: '城市指数编辑'
                    }
                },
                {
                    path: '/baseStatList',
                    name: 'baseStatList',
                    component: () => import(/* webpackChunkName: "baseStatList" */ './views/BaseStatList.vue'),
                    meta: {
                        title: '基地分布'
                    }
                },
                {
                    path: '/baseStatEdit',
                    name: 'baseStatEdit',
                    component: () => import(/* webpackChunkName: "baseStatEdit" */ './views/BaseStatEdit.vue'),
                    meta: {
                        title: '基地分布编辑'
                    }
                },
                {
                    path: '/techTradeStatList',
                    name: 'techTradeStatList',
                    component: () =>
                        import(/* webpackChunkName: "techTradeStatList" */ './views/TechTradeStatList.vue'),
                    meta: {
                        title: '技术交易'
                    }
                },
                {
                    path: '/techTradeStatEdit',
                    name: 'techTradeStatEdit',
                    component: () =>
                        import(/* webpackChunkName: "techTradeStatEdit" */ './views/TechTradeStatEdit.vue'),
                    meta: {
                        title: '技术交易编辑'
                    }
                },
                {
                    path: '/chart1',
                    name: 'chart1',
                    component: () => import(/* webpackChunkName: "chart1" */ './views/chart1.vue'),
                    meta: {
                        title: '数据可视化'
                    }
                },
                {
                    path: '/chart2',
                    name: 'chart2',
                    component: () => import(/* webpackChunkName: "chart2" */ './views/chart2'),
                    meta: {
                        title: '数据输出'
                    }
                },
                {
                    path: '/chart3',
                    name: 'chart3',
                    component: () => import(/* webpackChunkName: "chart3" */ './views/chart3'),
                    meta: {
                        title: '数据分析'
                    }
                },
                {
                    path: '/geoserver',
                    name: 'geoserver',
                    component: () => import(/* webpackChunkName: "chart3" */ './views/Geoserver'),
                    meta: {
                        title: 'GIS数据'
                    }
                },
                {
                    path: '/operationLogList',
                    name: 'OperationLogList',
                    component: () => import(/* webpackChunkName: "operationLogList" */ '@/views/OperationLogList.vue'),
                    meta: {
                        title: '操作日志'
                    }
                },
                {
                    path: '/genCodeEdit',
                    name: 'genCodeEdit',
                    component: () => import(/* webpackChunkName: "genCodeEdit" */ '@/views/GenCodeEdit.vue'),
                    meta: {
                        title: '存储管理编辑'
                    }
                },
                {
                    path: '/genCodeList',
                    name: 'genCodeList',
                    component: () => import(/* webpackChunkName: "genCodeList" */ '@/views/GenCodeList.vue'),
                    meta: {
                        title: '存储管理'
                    }
                },
                {
                    path: '/sysConfigList',
                    name: 'sysConfigList',
                    component: () => import(/* webpackChunkName: "sysConfigList" */ '@/views/SysConfigList.vue'),
                    meta: {
                        title: '参数配置'
                    }
                },
                {
                    path: '/categoryEdit',
                    name: 'CategoryEdit',
                    component: () => import(/* webpackChunkName: "peakEdit" */ '@/views/CategoryEdit.vue'),
                    meta: {
                        title: '系统模块编辑'
                    }
                },
                {
                    path: '/categoryList',
                    name: 'CategoryList',
                    component: () => import(/* webpackChunkName: "peakList" */ '@/views/CategoryList.vue'),
                    meta: {
                        title: '系统模块列表'
                    }
                },
                {
                    path: '/thresholdEdit',
                    name: 'ThresholdEdit',
                    component: () => import(/* webpackChunkName: "thresholdEdit" */ '@/views/ThresholdEdit.vue'),
                    meta: {
                        title: '预警阀值编辑'
                    }
                },
                {
                    path: '/thresholdList',
                    name: 'ThresholdList',
                    component: () => import(/* webpackChunkName: "thresholdList" */ '@/views/ThresholdList.vue'),
                    meta: {
                        title: '预警阀值'
                    }
                },
                {
                    path: '/authorityEdit',
                    name: 'AuthorityEdit',
                    component: () => import(/* webpackChunkName: "authorityEdit" */ './views/AuthorityEdit.vue'),
                    meta: {
                        title: '角色编辑'
                    }
                },
                {
                    path: '/authorityList',
                    name: 'AuthorityList',
                    component: () => import(/* webpackChunkName: "authorityList" */ './views/AuthorityList.vue'),
                    meta: {
                        title: '角色管理'
                    }
                },
                {
                    path: '/metricEdit',
                    name: 'MetricEdit',
                    component: () => import(/* webpackChunkName: "metricEdit" */ '@/views/MetricEdit.vue'),
                    meta: {
                        title: '指标管理编辑'
                    }
                },
                {
                    path: '/metricList',
                    name: 'MetricList',
                    component: () => import(/* webpackChunkName: "metricList" */ '@/views/MetricList.vue'),
                    meta: {
                        title: '指标管理'
                    }
                },
                {
                    path: '/mapdata1',
                    name: 'mapdata1',
                    component: () => import(/* webpackChunkName: "mapdata1" */ '@/views/gis1.vue'),
                    meta: {
                        title: 'mapdata1'
                    }
                },
                {
                    path: '/mapdata2',
                    name: 'mapdata2',
                    component: () => import(/* webpackChunkName: "mapdata2" */ '@/views/gis2.vue'),
                    meta: {
                        title: 'mapdata2'
                    }
                },
                {
                    path: '/summaryStateEdit',
                    name: 'SummaryStateEdit',
                    component: () => import(/* webpackChunkName: "summaryStateEdit" */ '@/views/SummaryStateEdit.vue'),
                    meta: {
                        title: '数据统计编辑'
                    }
                },
                {
                    path: '/summaryCharts',
                    name: 'SummaryCharts',
                    component: () => import(/* webpackChunkName: "summaryStateEdit" */ '@/views/SummaryCharts.vue'),
                    meta: {
                        title: '图表统计'
                    }
                },
                {
                    path: '/summaryStateList',
                    name: 'SummaryStateList',
                    component: () => import(/* webpackChunkName: "summaryStateList" */ '@/views/SummaryStateList.vue'),
                    meta: {
                        title: '数据统计'
                    }
                },
                {
                    path: '/summaryTypeEdit',
                    name: 'SummaryTypeEdit',
                    component: () => import(/* webpackChunkName: "summaryTypeEdit" */ '@/views/SummaryTypeEdit.vue'),
                    meta: {
                        title: '数据表名编辑'
                    }
                },
                {
                    path: '/summaryTypeList',
                    name: 'SummaryTypeList',
                    component: () => import(/* webpackChunkName: "summaryTypeList" */ '@/views/SummaryTypeList.vue'),
                    meta: {
                        title: '数据表名'
                    }
                }
                /**INSERT_LOCATION**/
            ]
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('@/views/Login'),
            meta: {
                title: '登录'
            }
        }
    ]
});
router.beforeEach((to, from, next) => {
    //console.log(to);
    if (/^\/http/.test(to.path)) {
        console.log(Vue.$baseUrl);
        let url = to.path.replace('/', '');
        let params = [];
        if (to.query) {
            for (let key in to.query) {
                if (to.query.hasOwnProperty(key)) {
                    params.push(`${key}=${to.query[key]}`);
                }
            }
        }
        if (params.length > 0) {
            url += `?${params.join('&')}`;
        }
        window.open(url);
        return;
    }
    if (!store.state.userInfo && to.path !== '/login') {
        http.axios
            .get('/user/my')
            .then(res => {
                store.commit('updateUserInfo', res.data);
                next();
                return;
            })
            .catch(() => {
                localStorage.removeItem('token');
                next('/login');
            });
    } else if (!to.matched.length) {
        next('/404');
    } else {
        next();
    }
});

export default router;
