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: 'dashboard'
                },
                {
                    path: '/404',
                    name: '404',
                    component: () => import(/* webpackChunkName: "404" */ '@/views/404.vue')
                },
                {
                    path: '/dashboard',
                    name: 'dashboard',
                    component: () => import(/* webpackChunkName: "404" */ '@/views/Dashboard.vue'),
                    meta: {
                        title: '首页'
                    }
                },
                {
                    path: '/api',
                    name: 'api',
                    component: () => import(/* webpackChunkName: "api" */ '@/views/Api.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: '/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: '/sysConfigList',
                    name: 'sysConfigList',
                    component: () => import(/* webpackChunkName: "sysConfigList" */ '@/views/SysConfigList.vue'),
                    meta: {
                        title: '参数配置'
                    }
                },
                {
                    path: '/upload',
                    name: 'upload',
                    component: () => import(/* webpackChunkName: "upload" */ '@/views/Upload.vue'),
                    meta: {
                        title: '上传'
                    }
                },
                {
                    path: '/withdrawEdit',
                    name: 'WithdrawEdit',
                    component: () => import(/* webpackChunkName: "withdrawEdit" */ '@/views/WithdrawEdit.vue'),
                    meta: {
                        title: '提现申请编辑'
                    }
                },
                {
                    path: '/withdrawList',
                    name: 'WithdrawList',
                    component: () => import(/* webpackChunkName: "withdrawList" */ '@/views/WithdrawList.vue'),
                    meta: {
                        title: '提现申请'
                    }
                },
                {
                    path: '/commissionRecordEdit',
                    name: 'CommissionRecordEdit',
                    component: () =>
                        import(/* webpackChunkName: "commissionRecordEdit" */ '@/views/CommissionRecordEdit.vue'),
                    meta: {
                        title: '佣金记录编辑'
                    }
                },
                {
                    path: '/commissionRecordList',
                    name: 'CommissionRecordList',
                    component: () =>
                        import(/* webpackChunkName: "commissionRecordList" */ '@/views/CommissionRecordList.vue'),
                    meta: {
                        title: '佣金记录'
                    }
                },
                {
                    path: '/setMealEdit',
                    name: 'SetMealEdit',
                    component: () => import(/* webpackChunkName: "setInfoEdit" */ '@/views/SetMealEdit.vue'),
                    meta: {
                        title: '套餐信息编辑'
                    }
                },
                {
                    path: '/setMealList',
                    name: 'SetMealList',
                    component: () => import(/* webpackChunkName: "setInfoList" */ '@/views/SetMealList.vue'),
                    meta: {
                        title: '套餐信息'
                    }
                },
                {
                    path: '/goodsInfoList',
                    name: 'GoodsInfoList',
                    component: () => import(/* webpackChunkName: "goodsInfoList" */ '@/views/GoodsInfoList.vue'),
                    meta: {
                        title: '商品信息'
                    }
                },
                {
                    path: '/setGoodsEdit',
                    name: 'SetGoodsEdit',
                    component: () => import(/* webpackChunkName: "setGoodsEdit" */ '@/views/SetGoodsEdit.vue'),
                    meta: {
                        title: '套餐内商品编辑'
                    }
                },
                {
                    path: '/setGoodsList',
                    name: 'SetGoodsList',
                    component: () => import(/* webpackChunkName: "setGoodsList" */ '@/views/SetGoodsList.vue'),
                    meta: {
                        title: '套餐内商品'
                    }
                },
                {
                    path: '/orderInfoEdit',
                    name: 'OrderInfoEdit',
                    component: () => import(/* webpackChunkName: "orderInfoEdit" */ '@/views/OrderInfoEdit.vue'),
                    meta: {
                        title: '订单管理编辑'
                    }
                },
                {
                    path: '/orderInfoList',
                    name: 'OrderInfoList',
                    component: () => import(/* webpackChunkName: "orderInfoList" */ '@/views/OrderInfoList.vue'),
                    meta: {
                        title: '订单管理'
                    }
                },
                {
                    path: '/userSetEdit',
                    name: 'UserSetEdit',
                    component: () => import(/* webpackChunkName: "userSetEdit" */ '@/views/UserSetEdit.vue'),
                    meta: {
                        title: '用户套餐编辑'
                    }
                },
                {
                    path: '/userSetList',
                    name: 'UserSetList',
                    component: () => import(/* webpackChunkName: "userSetList" */ '@/views/UserSetList.vue'),
                    meta: {
                        title: '用户套餐'
                    }
                },
                {
                    path: '/userSetFlowEdit',
                    name: 'UserSetFlowEdit',
                    component: () => import(/* webpackChunkName: "userSetFlowEdit" */ '@/views/UserSetFlowEdit.vue'),
                    meta: {
                        title: '用户套餐流水编辑'
                    }
                },
                {
                    path: '/userSetFlowList',
                    name: 'UserSetFlowList',
                    component: () => import(/* webpackChunkName: "userSetFlowList" */ '@/views/UserSetFlowList.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;
