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

Vue.use(Router);

const router = new Router({
    routes: [{
            path: '/',
            component: () =>
                import ('../pagesPre/App'),
            children: [{
                    path: '/',
                    redirect: 'index'
                },
                {
                    path: '/index',
                    name: 'index',
                    component: () =>
                        import ('../pagesPre/Index')
                },
                {
                    path: '/business',
                    name: 'business',
                    component: () =>
                        import ('../pagesPre/Business'),
                    meta: {
                        title: '修图商'
                    }
                },
                {
                    path: '/search',
                    name: 'search',
                    component: () =>
                        import ('../pagesPre/Search'),
                    meta: {
                        title: '搜索'
                    }
                },
                {
                    path: '/about',
                    name: 'about',
                    component: () =>
                        import ('../pagesPre/About'),
                    meta: {
                        title: '关于我们'
                    }
                },
                {
                    path: '/join',
                    name: 'join',
                    component: () =>
                        import ('../pagesPre/Join'),
                    meta: {
                        title: '加入我们'
                    }
                },
                {
                    path: '/contact',
                    name: 'contact',
                    component: () =>
                        import ('../pagesPre/Contact'),
                    meta: {
                        title: '联系我们'
                    }
                },
                {
                    path: '/shop',
                    name: 'shop',
                    component: () =>
                        import ('../pagesPre/Shop'),
                    meta: {
                        title: '店铺详情'
                    }
                },
                {
                    path: '/product',
                    name: 'product',
                    component: () =>
                        import ('../pagesPre/Product'),
                    meta: {
                        title: '商品详情'
                    }
                }
            ]
        }, {
            path: '/',
            component: () =>
                import ('../pagesPre/Order'),
            children: [{
                path: '/submit',
                name: 'submit',
                component: () =>
                    import ('../pagesPre/Submit'),
                meta: {
                    title: '确认订单',
                    checkLogin: true
                }
            }, {
                path: '/payOrder',
                name: 'payOrder',
                component: () =>
                    import ('../pagesPre/PayOrder'),
                meta: {
                    title: '支付订单',
                    checkLogin: true
                }
            }]
        }, {
            path: '/',
            component: () =>
                import ('../pagesPre/UserCenter'),
            children: [{
                path: '/userCenterHome',
                name: 'userCenterHome',
                component: () =>
                    import ('../pagesPre/UserCenterHome'),
                meta: {
                    title: '个人中心——首页',
                    checkLogin: true,
                    pageMenu: '个人中心',
                    pageMenu2: '首页',
                }
            }, {
                path: '/userOrder',
                name: 'userOrder',
                component: () =>
                    import ('../pagesPre/UserOrder'),
                meta: {
                    title: '个人中心——订单管理',
                    checkLogin: true,
                    pageMenu: '个人中心',
                    pageMenu2: '首页',
                }
            }, {
                path: '/userSet',
                name: 'userSet',
                component: () =>
                    import ('../pagesPre/UserSet'),
                meta: {
                    title: '账户设置',
                    checkLogin: true,
                    pageMenu: '个人中心',
                    pageMenu2: '账户设置',
                }
            }, {
                path: '/userSafe',
                name: 'userSafe',
                component: () =>
                    import ('../pagesPre/UserSafe'),
                meta: {
                    title: '安全设置',
                    checkLogin: true,
                    pageMenu: '个人中心',
                    pageMenu2: '账户设置',
                }
            }, {
                path: '/userCertification',
                name: 'userCertification',
                component: () =>
                    import ('../pagesPre/UserCertification'),
                meta: {
                    title: '认证开店',
                    checkLogin: true,
                    pageMenu: '个人中心',
                    pageMenu2: '账户设置',
                }
            }, {
                path: '/userCollection',
                name: 'userCollection',
                component: () =>
                    import ('../pagesPre/UserCollection'),
                meta: {
                    title: '用户收藏',
                    checkLogin: true,
                    pageMenu: '个人中心',
                    pageMenu2: '首页',
                }
            }, {
                path: '/memberPoint',
                name: 'memberPoint',
                component: () =>
                    import ('../pagesPre/MemberPoint'),
                meta: {
                    title: '我的积分',
                    checkLogin: true,
                    pageMenu: '个人中心',
                    pageMenu2: '首页',
                }
            }, {
                path: '/memberMoney',
                name: 'memberMoney',
                component: () =>
                    import ('../pagesPre/MemberMoney'),
                meta: {
                    title: '账户余额',
                    checkLogin: true,
                    pageMenu: '个人中心',
                    pageMenu2: '无',
                }
            }, {
                path: '/orderUpload',
                name: 'orderUpload',
                component: () =>
                    import ('../pagesPre/OrderUpload'),
                meta: {
                    title: '上传照片',
                    checkLogin: true,
                    pageMenu: '个人中心',
                    pageMenu2: '无',
                }
            }, {
                path: '/orderExamine',
                name: 'orderExamine',
                component: () =>
                    import ('../pagesPre/OrderExamine'),
                meta: {
                    title: '审核照片',
                    checkLogin: true,
                    pageMenu: '个人中心',
                    pageMenu2: '无',
                }
            }, {
                path: '/orderPreview',
                name: 'orderPreview',
                component: () =>
                    import ('../pagesPre/OrderPreview'),
                meta: {
                    title: '预览照片',
                    checkLogin: true,
                    pageMenu: '个人中心',
                    pageMenu2: '无',
                }
            }, {
                path: '/productComment',
                name: 'productComment',
                component: () =>
                    import ('../pagesPre/ProductComment'),
                meta: {
                    title: '评论订单',
                    checkLogin: true,
                    pageMenu: '个人中心',
                    pageMenu2: '无',
                }
            }, {
                path: '/orderDetail',
                name: 'orderDetail',
                component: () =>
                    import ('../pagesPre/OrderDetail'),
                meta: {
                    title: '订单详情',
                    checkLogin: true,
                    pageMenu: '个人中心',
                    pageMenu2: '无',
                }
            }, {
                path: '/userComment',
                name: 'userComment',
                component: () =>
                    import ('../pagesPre/UserComment'),
                meta: {
                    title: '我的评价',
                    checkLogin: true,
                    pageMenu: '个人中心',
                    pageMenu2: '首页',
                }
            }, {
                path: '/userShop',
                name: 'userShop',
                component: () =>
                    import ('../pagesPre/UserShop'),
                meta: {
                    title: '商家首页',
                    checkLogin: true,
                    pageMenu: '商家中心',
                    pageMenu2: '首页',
                }
            }, {
                path: '/shopOrder',
                name: 'shopOrder',
                component: () =>
                    import ('../pagesPre/ShopOrder'),
                meta: {
                    title: '商家首页',
                    checkLogin: true,
                    pageMenu: '商家中心',
                    pageMenu2: '首页',
                }
            }, {
                path: '/userStore',
                name: 'userStore',
                component: () =>
                    import ('../pagesPre/UserStore'),
                meta: {
                    title: '商家首页',
                    checkLogin: true,
                    pageMenu: '商家中心',
                    pageMenu2: '首页',
                }
            }, {
                path: '/userProductCon',
                name: 'userProductCon',
                component: () =>
                    import ('../pagesPre/UserProductCon'),
                meta: {
                    title: '商家首页',
                    checkLogin: true,
                    pageMenu: '商家中心',
                    pageMenu2: '首页',
                }
            }, {
                path: '/productInfo',
                name: 'productInfo',
                component: () =>
                    import ('../pagesPre/ProductInfo'),
                meta: {
                    title: '商家首页',
                    checkLogin: true,
                    pageMenu: '商家中心',
                    pageMenu2: '首页',
                }
            }, {
                path: '/orderUploadChange',
                name: 'orderUploadChange',
                component: () =>
                    import ('../pagesPre/OrderUploadChange'),
                meta: {
                    title: '上传修图',
                    checkLogin: true,
                    pageMenu: '商家中心',
                    pageMenu2: '无',
                }
            }, {
                path: '/storeComment',
                name: 'storeComment',
                component: () =>
                    import ('../pagesPre/StoreComment'),
                meta: {
                    title: '店铺评论',
                    checkLogin: true,
                    pageMenu: '商家中心',
                    pageMenu2: '首页',
                }
            }, {
                path: '/customerServices',
                name: 'customerServices',
                component: () =>
                    import ('../pagesPre/CustomerServices'),
                meta: {
                    title: '客服管理',
                    checkLogin: true,
                    pageMenu: '商家中心',
                    pageMenu2: '账户设置',
                }
            }, {
                path: '/customerService',
                name: 'customerService',
                component: () =>
                    import ('../pagesPre/CustomerService'),
                meta: {
                    title: '客服编辑',
                    checkLogin: true,
                    pageMenu: '商家中心',
                    pageMenu2: '账户设置',
                }
            }]
        }, {
            path: '/',
            component: () =>
                import ('../pagesPre/PageBottom'),
            children: [{
                path: '/register',
                name: 'register',
                component: () =>
                    import ('../pagesPre/Register'),
                meta: {
                    title: '账户注册',
                }
            }, {
                path: '/backPassword',
                name: 'backPassword',
                component: () =>
                    import ('../pagesPre/BackPassword'),
                meta: {
                    title: '找回密码',
                }
            }, {
                path: '/changePhone',
                name: 'changePhone',
                component: () =>
                    import ('../pagesPre/ChangePhone'),
                meta: {
                    title: '修改手机号',
                }
            }]
        }, {
            path: '/login',
            name: 'login',
            component: () =>
                import ('../pagesPre/Login')
        }, {
            path: '/storeChat',
            name: 'storeChat',
            component: () =>
                import ('../pagesPre/StoreChat'),
            meta: {
                checkLogin: true,
                title: '客服聊天',
            }
        },
        {
            path: '/404',
            name: '404',
            component: () =>
                import ('../pagesPre/404')
        },
        {
            path: '/orderExamineShare',
            name: 'orderExamineShare',
            component: () =>
                import ('../pagesPre/OrderExamineShare'),
            meta: {
                title: '审核照片',
            }
        }
    ]
});
router.beforeEach((to, from, next) => {
    console.log(to.path)
        //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 (from.name != 'login') {
        store.commit('updateBrefName', from.name)
    }

    if (store.state.userInfo) {
        axios.get('/usersSession/all', {
            params: {
                userId: store.state.userInfo.id,
                type: 1
            }
        }).then(res => {
            if (res.data.success) {
                store.commit('updateConversationList', res.data.data)
            }
        })
    }




    if (!store.state.userInfo && to.path !== '/login') {
        // next();
        axios.get('/userInfo/getUserInfo').then(res => {
            if (res.status === 200) {
                if (res.data.success && res.data.data) {
                    store.commit('updateUserInfo', res.data.data);
                    console.log(getQueryString('storeId'))
                    if (getQueryString('storeId')) {
                        axios.get('storeInfo/getOne?id=' + getQueryString('storeId'))
                            .then(res => {
                                if (res.data.success) {
                                    store.commit('updateStoreInfo', res.data.data);
                                }
                            })
                            .catch(e => {
                                console.log(e)
                            })
                    } else {

                        if (store.state.userInfo.storeFlag == 'Y') {

                            axios.get('storeInfo/getOne?userId=' + store.state.userInfo.id)
                                .then(res => {
                                    if (res.data.success) {
                                        store.commit('updateStoreInfo', res.data.data);
                                    }
                                })
                                .catch(e => {
                                    console.log(e)
                                })
                        } else {
                            axios.get('/usersSession/all', {
                                params: {
                                    userId: store.state.userInfo.id,
                                    type: 1
                                }
                            }).then(res => {
                                if (res.data.success) {
                                    store.commit('updateConversationList', res.data.data)
                                }
                            })
                            axios.get('rong/getToken?rongKey=' + store.state.userInfo.id)
                                .then(res => {
                                    if (res.data.appKey && res.data.token) {
                                        RongIM.init(res.data.appKey, res.data.token)
                                    }
                                })
                                .catch(e => {
                                    console.log(e)
                                })
                        }
                    }




                    next()
                    return;
                }
            }
            console.log(to.meta.checkLogin)
            if (to.meta.checkLogin) {
                next('/login');
            } else {
                next();
            }
        }).catch(() => {
            if (to.meta.checkLogin) {
                next('/login');
            } else {
                next();
            }
        })
    } else if (!to.matched.length) {
        next('/404');
    } else {
        next();
    }
});
router.afterEach((to, from) => {
    window.onresize();
});

function getQueryString(name) {
    let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
    let r = window.location.search.substr(1).match(reg)
    if (r != null) return unescape(r[2])
    return null
}


export default router;