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: '/promotionList',
                    name: 'promotionList',
                    component: () => import(/* webpackChunkName: "userEdit" */ '@/views/PromotionList.vue'),
                    meta: {
                        title: '推广列表'
                    }
                },
                {
                    path: '/employeeList',
                    name: 'employeeList',
                    component: () => import(/* webpackChunkName: "userEdit" */ '@/views/EmployeeList.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: '/packageEdit',
                    name: 'PackageEdit',
                    component: () => import(/* webpackChunkName: "packageEdit" */ '@/views/PackageEdit.vue'),
                    meta: {
                        title: '套餐信息编辑'
                    }
                },
                {
                    path: '/packageList',
                    name: 'PackageList',
                    component: () => import(/* webpackChunkName: "packageList" */ '@/views/PackageList.vue'),
                    meta: {
                        title: '套餐信息'
                    }
                },
                {
                    path: '/goodsInfoList',
                    name: 'GoodsInfoList',
                    component: () => import(/* webpackChunkName: "goodsInfoList" */ '@/views/GoodsInfoList.vue'),
                    meta: {
                        title: '商品信息'
                    }
                },
                {
                    path: '/packageGoodsEdit',
                    name: 'PackageGoodsEdit',
                    component: () => import(/* webpackChunkName: "packageGoodsEdit" */ '@/views/PackageGoodsEdit.vue'),
                    meta: {
                        title: '套餐内商品编辑'
                    }
                },
                {
                    path: '/packageGoodsList',
                    name: 'PackageGoodsList',
                    component: () => import(/* webpackChunkName: "packageGoodsList" */ '@/views/PackageGoodsList.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: '/userPackageEdit',
                    name: 'UserPackageEdit',
                    component: () => import(/* webpackChunkName: "userPackageEdit" */ '@/views/UserPackageEdit.vue'),
                    meta: {
                        title: '用户套餐编辑'
                    }
                },
                {
                    path: '/userPackageList',
                    name: 'UserPackageList',
                    component: () => import(/* webpackChunkName: "userPackageList" */ '@/views/UserPackageList.vue'),
                    meta: {
                        title: '用户套餐'
                    }
                },
                {
                    path: '/userPackageFlowEdit',
                    name: 'UserPackageFlowEdit',
                    component: () =>
                        import(/* webpackChunkName: "userPackageFlowEdit" */ '@/views/UserPackageFlowEdit.vue'),
                    meta: {
                        title: '用户套餐流水编辑'
                    }
                },
                {
                    path: '/userPackageFlowList',
                    name: 'UserPackageFlowList',
                    component: () =>
                        import(/* webpackChunkName: "userPackageFlowList" */ '@/views/UserPackageFlowList.vue'),
                    meta: {
                        title: '用户套餐流水'
                    }
                },
                {
                    path: '/companyEdit',
                    name: 'CompanyEdit',
                    component: () => import(/* webpackChunkName: "companyEdit" */ '@/views/CompanyEdit.vue'),
                    meta: {
                        title: '团队信息编辑'
                    }
                },
                {
                    path: '/companyList',
                    name: 'CompanyList',
                    component: () => import(/* webpackChunkName: "companyList" */ '@/views/CompanyList.vue'),
                    meta: {
                        title: '团队信息'
                    }
                },
                {
                    path: '/articleEdit',
                    name: 'ArticleEdit',
                    component: () => import(/* webpackChunkName: "articleEdit" */ '@/views/ArticleEdit.vue'),
                    meta: {
                        title: '文章编辑编辑'
                    }
                },
                {
                    path: '/articleList',
                    name: 'ArticleList',
                    component: () => import(/* webpackChunkName: "articleList" */ '@/views/ArticleList.vue'),
                    meta: {
                        title: '文章编辑'
                    }
                },
                {
                    path: '/bannerEdit',
                    name: 'BannerEdit',
                    component: () => import(/* webpackChunkName: "bannerEdit" */ '@/views/BannerEdit.vue'),
                    meta: {
                        title: 'banner编辑'
                    }
                },
                {
                    path: '/bannerList',
                    name: 'BannerList',
                    component: () => import(/* webpackChunkName: "bannerList" */ '@/views/BannerList.vue'),
                    meta: {
                        title: 'banner'
                    }
                },
                {
                    path: '/feedbackEdit',
                    name: 'FeedbackEdit',
                    component: () => import(/* webpackChunkName: "feedbackEdit" */ '@/views/FeedbackEdit.vue'),
                    meta: {
                        title: '反馈编辑'
                    }
                },
                {
                    path: '/feedbackList',
                    name: 'FeedbackList',
                    component: () => import(/* webpackChunkName: "feedbackList" */ '@/views/FeedbackList.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;
