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: "promotionList" */ '@/views/PromotionList.vue'),
                    meta: {
                        title: '推广列表'
                    }
                },
                {
                    path: '/employeeList',
                    name: 'employeeList',
                    component: () => import(/* webpackChunkName: "employeeList" */ '@/views/employee/EmployeeList.vue'),
                    meta: {
                        title: '员工列表'
                    }
                },
                {
                    path: '/writeOffUserList',
                    name: 'WriteOffUserList',
                    component: () =>
                        import(/* webpackChunkName: "writeOffUserList" */ '@/views/attractions/WriteOffUserList.vue'),
                    meta: {
                        title: '员工列表'
                    }
                },
                {
                    path: '/writeOffUserList2',
                    name: 'WriteOffUserList2',
                    component: () =>
                        import(/* webpackChunkName: "writeOffUserList" */ '@/views/attractions/WriteOffUserList2.vue'),
                    meta: {
                        title: '员工列表'
                    }
                },
                {
                    path: '/employeeDashboard',
                    name: 'employeeDashboard',
                    component: () => import(/* webpackChunkName: "404" */ '@/views/employee/EmployeeDashboard.vue'),
                    meta: {
                        title: '员工数据总览'
                    }
                },
                {
                    path: '/childrenList',
                    name: 'childrenList',
                    component: () => import(/* webpackChunkName: "childrenList" */ '@/views/employee/ChildrenList.vue'),
                    meta: {
                        title: '下级列表'
                    }
                },
                {
                    path: '/employeeOrderList',
                    name: 'employeeOrderList',
                    component: () =>
                        import(/* webpackChunkName: "employeeOrderList" */ '@/views/employee/EmployeeOrderList.vue'),
                    meta: {
                        title: '下级列表'
                    }
                },
                {
                    path: '/orderInfoList2',
                    name: 'orderInfoList2',
                    component: () =>
                        import(/* webpackChunkName: "userEdit" */ '@/views/attractions/OrderInfoList2.vue'),
                    meta: {
                        title: '下级列表'
                    }
                },
                {
                    path: '/userPackageFlowList1',
                    name: 'UserPackageFlowList1',
                    component: () =>
                        import(/* webpackChunkName: "userPackageFlowList1" */ '@/views/UserPackageFlowList1.vue'),
                    meta: {
                        title: '充值核销记录'
                    }
                },
                {
                    path: '/brandList',
                    name: 'BrandList',
                    component: () => import(/* webpackChunkName: "brandList" */ '@/views/attractions/BrandList.vue'),
                    meta: {
                        title: '品牌管理'
                    }
                },
                {
                    path: '/brandEdit',
                    name: 'BrandEdit',
                    component: () => import(/* webpackChunkName: "brandEdit" */ '@/views/attractions/BrandEdit.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: '/goodsInfoEdit',
                    name: 'GoodsInfoEdit',
                    component: () => import(/* webpackChunkName: "goodsInfoEdit" */ '@/views/GoodsInfoEdit.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/manage/FeedbackEdit.vue'),
                    meta: {
                        title: '反馈编辑'
                    }
                },
                {
                    path: '/feedbackList',
                    name: 'FeedbackList',
                    component: () => import(/* webpackChunkName: "feedbackList" */ '@/views/manage/FeedbackList.vue'),
                    meta: {
                        title: '反馈'
                    }
                },
                {
                    path: '/orderRefundEdit',
                    name: 'OrderRefundEdit',
                    component: () => import(/* webpackChunkName: "orderRefundEdit" */ '@/views/OrderRefundEdit.vue'),
                    meta: {
                        title: '订单退款编辑'
                    }
                },
                {
                    path: '/orderRefundList',
                    name: 'OrderRefundList',
                    component: () => import(/* webpackChunkName: "orderRefundList" */ '@/views/OrderRefundList.vue'),
                    meta: {
                        title: '订单退款'
                    }
                },
                {
                    path: '/attractionsEdit',
                    name: 'AttractionsEdit',
                    component: () =>
                        import(/* webpackChunkName: "attractionsEdit" */ '@/views/attractions/AttractionsEdit.vue'),
                    meta: {
                        title: '景区管理编辑'
                    }
                },
                {
                    path: '/attractionsList',
                    name: 'AttractionsList',
                    component: () =>
                        import(/* webpackChunkName: "attractionsList" */ '@/views/attractions/AttractionsList.vue'),
                    meta: {
                        title: '景区管理'
                    }
                },
                {
                    path: '/categories',
                    name: 'Categories',
                    component: () => import(/* webpackChunkName: "categoryList" */ '@/views/manage/Categories.vue'),
                    meta: {
                        title: '分类管理'
                    }
                },
                {
                    path: '/stockEdit',
                    name: 'StockEdit',
                    component: () => import(/* webpackChunkName: "stockEdit" */ '@/views/StockEdit.vue'),
                    meta: {
                        title: '库存编辑'
                    }
                },
                {
                    path: '/stockList',
                    name: 'StockList',
                    component: () => import(/* webpackChunkName: "stockList" */ '@/views/StockList.vue'),
                    meta: {
                        title: '库存'
                    }
                },
                {
                    path: '/couponEdit',
                    name: 'CouponEdit',
                    component: () => import(/* webpackChunkName: "couponEdit" */ '@/views/CouponEdit.vue'),
                    meta: {
                        title: '优惠券编辑'
                    }
                },
                {
                    path: '/couponList',
                    name: 'CouponList',
                    component: () => import(/* webpackChunkName: "couponList" */ '@/views/CouponList.vue'),
                    meta: {
                        title: '优惠券'
                    }
                },
                {
                    path: '/userCouponEdit',
                    name: 'UserCouponEdit',
                    component: () =>
                        import(/* webpackChunkName: "userCouponEdit" */ '@/views/attractions/UserCouponEdit.vue'),
                    meta: {
                        title: '用户优惠券编辑'
                    }
                },
                {
                    path: '/userCouponList',
                    name: 'UserCouponList',
                    component: () =>
                        import(/* webpackChunkName: "userCouponList" */ '@/views/attractions/UserCouponList.vue'),
                    meta: {
                        title: '用户优惠券'
                    }
                },
                {
                    path: '/addressEdit',
                    name: 'AddressEdit',
                    component: () => import(/* webpackChunkName: "addressEdit" */ '@/views/AddressEdit.vue'),
                    meta: {
                        title: '用户地址编辑'
                    }
                },
                {
                    path: '/addressList',
                    name: 'AddressList',
                    component: () => import(/* webpackChunkName: "addressList" */ '@/views/AddressList.vue'),
                    meta: {
                        title: '用户地址'
                    }
                },
                {
                    path: '/shoppingCartEdit',
                    name: 'ShoppingCartEdit',
                    component: () => import(/* webpackChunkName: "shoppingCartEdit" */ '@/views/ShoppingCartEdit.vue'),
                    meta: {
                        title: '购物车编辑'
                    }
                },
                {
                    path: '/shoppingCartList',
                    name: 'ShoppingCartList',
                    component: () => import(/* webpackChunkName: "shoppingCartList" */ '@/views/ShoppingCartList.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;
