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'),
                },
                {
                    path: '/api',
                    name: 'api',
                    component: () => import(/* webpackChunkName: "api" */ '@/views/Api.vue'),
                },
                {
                    path: '/genCodeEdit',
                    name: 'genCodeEdit',
                    component: () => import(/* webpackChunkName: "genCodeEdit" */ '@/views/GenCodeEdit.vue'),
                },
                {
                    path: '/genCodeList',
                    name: 'genCodeList',
                    component: () => import(/* webpackChunkName: "genCodeList" */ '@/views/GenCodeList.vue'),
                },
                {
                    path: '/menus',
                    name: 'menus',
                    component: () => import(/* webpackChunkName: "menus" */ '@/views/Menus.vue'),
                },
                {
                    path: '/userEdit',
                    name: 'userEdit',
                    component: () => import(/* webpackChunkName: "userEdit" */ '@/views/user/UserEdit.vue'),
                },
                {
                    path: '/userList',
                    name: 'userList',
                    component: () => import(/* webpackChunkName: "userList" */ '@/views/user/UserList.vue'),
                },
                {
                    path: '/sysConfigList',
                    name: 'sysConfigList',
                    component: () => import(/* webpackChunkName: "sysConfigList" */ '@/views/SysConfigList.vue'),
                },
                {
                    path: '/merchantEdit',
                    name: 'MerchantEdit',
                    component: () => import(/* webpackChunkName: "merchantEdit" */ '@/views/merchant/MerchantEdit.vue'),
                    meta: {
                       title: '商户编辑',
                    },
                },
                {
                    path: '/merchantList',
                    name: 'MerchantList',
                    component: () => import(/* webpackChunkName: "merchantList" */ '@/views/merchant/MerchantList.vue'),
                    meta: {
                       title: '商户',
                    },
               },
                {
                    path: '/goodsEdit',
                    name: 'GoodsEdit',
                    component: () => import(/* webpackChunkName: "goodsEdit" */ '@/views/merchant/GoodsEdit.vue'),
                    meta: {
                       title: '商品编辑',
                    },
                },
                {
                    path: '/goodsList',
                    name: 'GoodsList',
                    component: () => import(/* webpackChunkName: "goodsList" */ '@/views/merchant/GoodsList.vue'),
                    meta: {
                       title: '商品',
                    },
               },
                {
                    path: '/salesEdit',
                    name: 'SalesEdit',
                    component: () => import(/* webpackChunkName: "salesEdit" */ '@/views/merchant/SalesEdit.vue'),
                    meta: {
                       title: '商品每日销售编辑',
                    },
                },
                {
                    path: '/salesList',
                    name: 'SalesList',
                    component: () => import(/* webpackChunkName: "salesList" */ '@/views/merchant/SalesList.vue'),
                    meta: {
                       title: '商品每日销售',
                    },
               },
                {
                    path: '/categoryEdit',
                    name: 'CategoryEdit',
                    component: () => import(/* webpackChunkName: "categoryEdit" */ '@/views/merchant/CategoryEdit.vue'),
                    meta: {
                       title: '类别编辑',
                    },
                },
                {
                    path: '/categoryList',
                    name: 'CategoryList',
                    component: () => import(/* webpackChunkName: "categoryList" */ '@/views/merchant/CategoryList.vue'),
                    meta: {
                       title: '类别',
                    },
               },
                {
                    path: '/tagEdit',
                    name: 'TagEdit',
                    component: () => import(/* webpackChunkName: "tagEdit" */ '@/views/TagEdit.vue'),
                    meta: {
                       title: '标签编辑',
                    },
                },
                {
                    path: '/tagList',
                    name: 'TagList',
                    component: () => import(/* webpackChunkName: "tagList" */ '@/views/TagList.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: '/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: '/appraisalEdit',
                    name: 'AppraisalEdit',
                    component: () => import(/* webpackChunkName: "appraisalEdit" */ '@/views/AppraisalEdit.vue'),
                    meta: {
                       title: '评价表编辑',
                    },
                },
                {
                    path: '/appraisalList',
                    name: 'AppraisalList',
                    component: () => import(/* webpackChunkName: "appraisalList" */ '@/views/AppraisalList.vue'),
                    meta: {
                       title: '评价表',
                    },
               },
                {
                    path: '/blackListEdit',
                    name: 'BlackListEdit',
                    component: () => import(/* webpackChunkName: "blackListEdit" */ '@/views/backstage/BlackListEdit.vue'),
                    meta: {
                       title: '黑名单表编辑',
                    },
                },
                {
                    path: '/blackListList',
                    name: 'BlackListList',
                    component: () => import(/* webpackChunkName: "blackListList" */ '@/views/backstage/BlackListList.vue'),
                    meta: {
                       title: '黑名单表',
                    },
               },
                {
                    path: '/emailEdit',
                    name: 'EmailEdit',
                    component: () => import(/* webpackChunkName: "emailEdit" */ '@/views/backstage/EmailEdit.vue'),
                    meta: {
                       title: '邮箱编辑',
                    },
                },
                {
                    path: '/emailList',
                    name: 'EmailList',
                    component: () => import(/* webpackChunkName: "emailList" */ '@/views/backstage/EmailList.vue'),
                    meta: {
                       title: '邮箱',
                    },
               },
                {
                    path: '/riderEdit',
                    name: 'RiderEdit',
                    component: () => import(/* webpackChunkName: "riderEdit" */ '@/views/rider/RiderEdit.vue'),
                    meta: {
                       title: '骑手编辑',
                    },
                },
                {
                    path: '/riderList',
                    name: 'RiderList',
                    component: () => import(/* webpackChunkName: "riderList" */ '@/views/rider/RiderList.vue'),
                    meta: {
                       title: '骑手',
                    },
               },
                {
                    path: '/timeTagEdit',
                    name: 'TimeTagEdit',
                    component: () => import(/* webpackChunkName: "timeTagEdit" */ '@/views/backstage/TimeTagEdit.vue'),
                    meta: {
                       title: '时间类别编辑',
                    },
                },
                {
                    path: '/timeTagList',
                    name: 'TimeTagList',
                    component: () => import(/* webpackChunkName: "timeTagList" */ '@/views/backstage/TimeTagList.vue'),
                    meta: {
                       title: '时间类别',
                    },
               },
                {
                    path: '/shoppingCartEdit',
                    name: 'ShoppingCartEdit',
                    component: () => import(/* webpackChunkName: "shoppingCartEdit" */ '@/views/user/ShoppingCartEdit.vue'),
                    meta: {
                       title: '购物车编辑',
                    },
                },
                {
                    path: '/shoppingCartList',
                    name: 'ShoppingCartList',
                    component: () => import(/* webpackChunkName: "shoppingCartList" */ '@/views/user/ShoppingCartList.vue'),
                    meta: {
                       title: '购物车',
                    },
               },
                {
                    path: '/merchantNatureEdit',
                    name: 'MerchantNatureEdit',
                    component: () => import(/* webpackChunkName: "merchantNatureEdit" */ '@/views/merchant/MerchantNatureEdit.vue'),
                    meta: {
                       title: '商家性质编辑',
                    },
                },
                {
                    path: '/merchantNatureList',
                    name: 'MerchantNatureList',
                    component: () => import(/* webpackChunkName: "merchantNatureList" */ '@/views/merchant/MerchantNatureList.vue'),
                    meta: {
                       title: '商家性质',
                    },
               },
                {
                    path: '/chatWordsEdit',
                    name: 'ChatWordsEdit',
                    component: () => import(/* webpackChunkName: "chatWordsEdit" */ '@/views/ChatWordsEdit.vue'),
                    meta: {
                       title: '聊天框话术编辑',
                    },
                },
                {
                    path: '/chatWordsList',
                    name: 'ChatWordsList',
                    component: () => import(/* webpackChunkName: "chatWordsList" */ '@/views/ChatWordsList.vue'),
                    meta: {
                       title: '聊天框话术',
                    },
               },
                {
                    path: '/complaintEdit',
                    name: 'ComplaintEdit',
                    component: () => import(/* webpackChunkName: "complaintEdit" */ '@/views/user/ComplaintEdit.vue'),
                    meta: {
                       title: '投诉编辑',
                    },
                },
                {
                    path: '/complaintList',
                    name: 'ComplaintList',
                    component: () => import(/* webpackChunkName: "complaintList" */ '@/views/user/ComplaintList.vue'),
                    meta: {
                       title: '投诉',
                    },
               },
                {
                    path: '/voiceEdit',
                    name: 'VoiceEdit',
                    component: () => import(/* webpackChunkName: "voiceEdit" */ '@/views/merchant/VoiceEdit.vue'),
                    meta: {
                       title: '语音配置编辑',
                    },
                },
                {
                    path: '/voiceList',
                    name: 'VoiceList',
                    component: () => import(/* webpackChunkName: "voiceList" */ '@/views/merchant/VoiceList.vue'),
                    meta: {
                       title: '语音配置',
                    },
               },
                {
                    path: '/cooperateApplyEdit',
                    name: 'CooperateApplyEdit',
                    component: () => import(/* webpackChunkName: "cooperateApplyEdit" */ '@/views/user/CooperateApplyEdit.vue'),
                    meta: {
                       title: '合作申请编辑',
                    },
                },
                {
                    path: '/cooperateApplyList',
                    name: 'CooperateApplyList',
                    component: () => import(/* webpackChunkName: "cooperateApplyList" */ '@/views/user/CooperateApplyList.vue'),
                    meta: {
                       title: '合作申请',
                    },
               },
                {
                    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: '意见反馈',
                    },
               },
                {
                    path: '/bannerEdit',
                    name: 'BannerEdit',
                    component: () => import(/* webpackChunkName: "bannerEdit" */ '@/views/backstage/BannerEdit.vue'),
                    meta: {
                       title: 'Banner设置编辑',
                    },
                },
                {
                    path: '/bannerList',
                    name: 'BannerList',
                    component: () => import(/* webpackChunkName: "bannerList" */ '@/views/backstage/BannerList.vue'),
                    meta: {
                       title: 'Banner设置',
                    },
               },
                {
                    path: '/promoteEdit',
                    name: 'PromoteEdit',
                    component: () => import(/* webpackChunkName: "promoteEdit" */ '@/views/backstage/PromoteEdit.vue'),
                    meta: {
                       title: '首页推荐位编辑',
                    },
                },
                {
                    path: '/promoteList',
                    name: 'PromoteList',
                    component: () => import(/* webpackChunkName: "promoteList" */ '@/views/backstage/PromoteList.vue'),
                    meta: {
                       title: '首页推荐位',
                    },
               },
                {
                    path: '/priorityEdit',
                    name: 'PriorityEdit',
                    component: () => import(/* webpackChunkName: "priorityEdit" */ '@/views/merchant/PriorityEdit.vue'),
                    meta: {
                       title: '优先级编辑',
                    },
                },
                {
                    path: '/priorityList',
                    name: 'PriorityList',
                    component: () => import(/* webpackChunkName: "priorityList" */ '@/views/merchant/PriorityList.vue'),
                    meta: {
                       title: '优先级',
                    },
               },
                {
                    path: '/deliveryFeeEdit',
                    name: 'DeliveryFeeEdit',
                    component: () => import(/* webpackChunkName: "deliveryFeeEdit" */ '@/views/rider/DeliveryFeeEdit.vue'),
                    meta: {
                       title: '配送管理编辑',
                    },
                },
                {
                    path: '/deliveryFeeList',
                    name: 'DeliveryFeeList',
                    component: () => import(/* webpackChunkName: "deliveryFeeList" */ '@/views/rider/DeliveryFeeList.vue'),
                    meta: {
                       title: '配送管理',
                    },
               },
                {
                    path: '/riderSignEdit',
                    name: 'RiderSignEdit',
                    component: () => import(/* webpackChunkName: "riderSignEdit" */ '@/views/rider/RiderSignEdit.vue'),
                    meta: {
                       title: '骑手签到编辑',
                    },
                },
                {
                    path: '/riderSignList',
                    name: 'RiderSignList',
                    component: () => import(/* webpackChunkName: "riderSignList" */ '@/views/rider/RiderSignList.vue'),
                    meta: {
                       title: '骑手签到',
                    },
               }
                /**INSERT_LOCATION**/,
                {
                    path: '/categories',
                    name: 'Categories',
                    component: () => import(/* webpackChunkName: "tagList" */ '@/views/Categories.vue'),
                    meta: {
                        title: '分类',
                    },
                },
                {
                    path: '/backUserEdit',
                    name: 'BackUserEdit',
                    component: () => import(/* webpackChunkName: "blackUserEdit" */ '@/views/backstage/BackUserEdit.vue'),
                    meta: {
                        title: '管理员编辑',
                    },
                },
                {
                    path: '/backUserList',
                    name: 'BackUserList',
                    component: () => import(/* webpackChunkName: "blackUserList" */ '@/views/backstage/BackUserList.vue'),
                    meta: {
                        title: '管理员列表',
                    },
                },
                {
                    path: '/orderRefundApplyEdit',
                    name: 'OrderRefundApplyEdit',
                    component: () => import(/* webpackChunkName: "orderRefundApplyEdit" */ '@/views/backstage/OrderRefundApplyEdit.vue'),
                    meta: {
                        title: '退款申请编辑',
                    },
                },
                {
                    path: '/orderRefundApplyList',
                    name: 'OrderRefundApplyList',
                    component: () => import(/* webpackChunkName: "orderRefundApplyList" */ '@/views/backstage/OrderRefundApplyList.vue'),
                    meta: {
                        title: '退款申请',
                    },
                },
                {
                    path: '/withdrawApplyEdit',
                    name: 'WithdrawApplyEdit',
                    component: () => import(/* webpackChunkName: "withdrawApplyEdit" */ '@/views/backstage/WithdrawApplyEdit.vue'),
                    meta: {
                        title: '退款申请编辑',
                    },
                },
                {
                    path: '/withdrawApplyList',
                    name: 'WithdrawApplyList',
                    component: () => import(/* webpackChunkName: "withdrawApplyList" */ '@/views/backstage/WithdrawApplyList.vue'),
                    meta: {
                        title: '提现申请',
                    },
                },
            ],
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('@/views/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') {
        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;