import Vue from 'vue'
import Router from 'vue-router'
import store from '../vuex'
import axios from 'axios'

Vue.use(Router);

const router = new Router({
    routes: [{
            path: '/',
            component: () =>
                import ('../pages/App'),
            children: [{
                    path: '/',
                    redirect: 'dashboard'
                },
                {
                    path: '/404',
                    name: '404',
                    component: () =>
                        import ('../pages/404')
                },
                {
                    path: '/dashboard',
                    name: 'dashboard',
                    component: () =>
                        import ('../pages/Dashboard')
                },
                {
                    path: '/users',
                    name: 'users',
                    component: () =>
                        import ('../pages/Users')
                },
                {
                    path: '/user',
                    name: 'user',
                    component: () =>
                        import ('../pages/User')
                },
                {
                    path: '/menus',
                    name: 'menus',
                    component: () =>
                        import ('../pages/Menus')
                },
                {
                    path: '/roles',
                    name: 'roles',
                    component: () =>
                        import ('../pages/Roles')
                },
                {
                    path: '/role',
                    name: 'role',
                    component: () =>
                        import ('../pages/Role')
                },
                {
                    path: '/api',
                    name: 'api',
                    component: () =>
                        import ('../pages/Api')
                },
                {
                    path: '/genCodes',
                    name: 'genCodes',
                    component: () =>
                        import ('../pages/GenCodes')
                },
                {
                    path: '/genCode',
                    name: 'genCode',
                    component: () =>
                        import ('../pages/GenCode')
                },
                {
                    path: '/permissions',
                    name: 'permissions',
                    component: () =>
                        import ('../pages/Permissions')
                },
                {
                    path: '/permission',
                    name: 'permission',
                    component: () =>
                        import ('../pages/Permission')
                },
                {
                    path: '/departs',
                    name: 'departs',
                    component: () =>
                        import ('../pages/Departs')
                },
                {
                    path: '/lineChart',
                    name: 'lineChart',
                    component: () =>
                        import ('../pages/LineChart')
                },
                {
                    path: '/barChart',
                    name: 'barChart',
                    component: () =>
                        import ('../pages/BarChart')
                },
                {
                    path: '/polarAreaChart',
                    name: 'polarAreaChart',
                    component: () =>
                        import ('../pages/PolarAreaChart')
                },
                {
                    path: '/pieChart',
                    name: 'pieChart',
                    component: () =>
                        import ('../pages/PieChart')
                },
                {
                    path: '/radarChart',
                    name: 'radarChart',
                    component: () =>
                        import ('../pages/RadarChart')
                },
                {
                    path: '/bubbleChart',
                    name: 'bubbleChart',
                    component: () =>
                        import ('../pages/BubbleChart')
                },
                {
                    path: '/processModels',
                    name: 'processModels',
                    component: () =>
                        import ('../pages/ProcessModels')
                },
                {
                    path: '/processDefinitions',
                    name: 'processDefinitions',
                    component: () =>
                        import ('../pages/ProcessDefinitions')
                },
                {
                    path: '/myProcesses',
                    name: 'myProcesses',
                    component: () =>
                        import ('../pages/MyProcesses')
                },
                {
                    path: '/myTasks',
                    name: 'myTasks',
                    component: () =>
                        import ('../pages/MyTasks')
                },
                {
                    path: '/completeTaskDynamic',
                    name: 'CompleteTaskDynamic',
                    component: () =>
                        import ('../pages/CompleteTaskDynamic')
                },
                {
                    path: '/commonQuerys',
                    name: 'commonQuerys',
                    component: () =>
                        import ('../pages/CommonQuerys')
                },
                {
                    path: '/commonQuery',
                    name: 'commonQuery',
                    component: () =>
                        import ('../pages/CommonQuery')
                },
                {
                    path: '/commonIframe',
                    name: 'commonIframe',
                    component: () =>
                        import ('../pages/CommonIframe')
                },
                {
                    path: '/queryConfigs',
                    name: 'queryConfigs',
                    component: () =>
                        import ('../pages/QueryConfigs')
                },
                {
                    path: '/queryConfig',
                    name: 'queryConfig',
                    component: () =>
                        import ('../pages/QueryConfig')
                },
                {
                    path: '/dataSourceInfos',
                    name: 'dataSourceInfos',
                    component: () =>
                        import ('../pages/DataSourceInfos')
                },
                {
                    path: '/dataSourceInfo',
                    name: 'dataSourceInfo',
                    component: () =>
                        import ('../pages/DataSourceInfo')
                },
                {
                    path: '/deploySystem',
                    name: 'DeploySystem',
                    component: () =>
                        import ('../pages/DeploySystem')
                },
                {
                    path: '/myCanvas',
                    name: 'MyCanvas',
                    component: () =>
                        import ('../pages/MyCanvas')
                }
                /**INSERT_LOCATION**/
            ]
        },
        {
            path: '/login',
            name: 'login',
            component: () =>
                import ('../pages/Login')
        }
    ]
});
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') {
        axios.get('/userInfo/getUserInfo').then(res => {
            if (res.status === 200) {
                if (res.data.success && res.data.data) {
                    store.commit('updateUserInfo', res.data.data);
                    next();
                    return;
                }
            }
            next('/login');
        }).catch(() => {
            next('/login');
        })
    } else if (!to.matched.length) {
        next('/404');
    } else {
        next();
    }
});
router.afterEach((to, from) => {
    window.onresize();
});

export default router;