import Vue from 'vue'
import store from "@/store"
import Index from '@/pages/Index'
import utils from '@/utils'
import axios from 'axios'
import Router from 'vue-router'

Vue.use(Router)
const vue = new Vue();
const router = new Router({
    routes: [{
        path: '/',
        name: 'index',
        component: Index,
        children: [{
            path: "/",
            redirect: "home"
        }, {
            path: "/home",
            name: "home",
            component: resolve => require(["../pages/Home"], resolve),
            meta: {
                title: '享居屋',
            }
        }, {
            path: "/user",
            name: "user",
            component: resolve => require(["../pages/User"], resolve),
            meta: {
                title: '享居屋',
            }
        }, {
            path: "/strategy",
            name: "strategy",
            component: resolve => require(["../pages/Strategy"], resolve),
            meta: {
                title: '攻略',
            }
        }]
    }, {
        path: '/login',
        name: 'login',
        component: resolve => {
            require(["../pages/Login"], resolve)
        },
        meta: {
            title: ''
        }
    }, {
        path: "/loginSe",
        name: "loginSe",
        component: resolve => require(["../pages/LoginSe"], resolve),
        meta: {
            title: '',
        }
    }, {
        path: "/search",
        name: "search",
        component: resolve => require(["../pages/Search"], resolve),
        meta: {
            title: '',
        }
    }, {
        path: "/houseDetail",
        name: "houseDetail",
        component: resolve => require(["../pages/HouseDetail"], resolve),
        meta: {
            title: '房源详情',
        }
    }, {
        path: "/set",
        name: "set",
        component: resolve => require(["../pages/Set"], resolve),
        meta: {
            title: '修改资料',
        }
    }, {
        path: "/changeNick",
        name: "changeNick",
        component: resolve => require(["../pages/ChangeNick"], resolve),
        meta: {
            title: '修改昵称',
        }
    }, {
        path: "/feedback",
        name: "feedback",
        component: resolve => require(["../pages/Feedback"], resolve),
        meta: {
            title: '意见反馈',
        }
    }, {
        path: "/about",
        name: "about",
        component: resolve => require(["../pages/About"], resolve),
        meta: {
            title: '关于享居屋',
        }
    }, {
        path: "/getMore",
        name: "getMore",
        component: resolve => require(["../pages/GetMore"], resolve),
        meta: {
            title: '整租/合租/品牌公寓',
        }
    }, {
        path: "/getRecommend",
        name: "getRecommend",
        component: resolve => require(["../pages/getRecommend"], resolve),
        meta: {
            title: '整租/合租/品牌公寓',
        }
    }, {
        path: "/appoint",
        name: "appoint",
        component: resolve => require(["../pages/Appoint"], resolve),
        meta: {
            title: '预约看房',
        }
    }, {
        path: "/myCollection",
        name: "myCollection",
        component: resolve => require(["../pages/MyCollection"], resolve),
        meta: {
            title: '我的收藏',
        }
    }, {
        path: "/myappoint",
        name: "myappoint",
        component: resolve => require(["../pages/MyAppoint"], resolve),
        meta: {
            title: '预约管理',
        }
    }, {
        path: "/myContact",
        name: "myContact",
        component: resolve => require(["../pages/MyContact"], resolve),
        meta: {
            title: '联系记录',
        }
    }, {
        path: "/housingTrust",
        name: "housingTrust",
        component: resolve => require(["../pages/HousingTrust"], resolve),
        meta: {
            title: '房屋托管',
        }
    }, {
        path: "/location",
        name: "location",
        component: resolve => require(["../pages/Location"], resolve),
        meta: {
            title: '选择地址',
        }
    }],

})

router.beforeEach((to, from, next) => {
    store.commit("updateLoading", true);
    var list = ['/home', '/strategy', '/user']
    if (list.indexOf(to.path) != -1) {
        const toTab = list.indexOf(to.path);
        const fromTab = list.indexOf(from.path);
        if (fromTab < toTab && fromTab != -1) {
            store.commit("updateDirection", "forward");
        } else {
            store.commit("updateDirection", "reverse");
        }
    } else {
        if (to.params.isNext) {
            store.commit("updateDirection", "forward");
        } else {
            store.commit("updateDirection", "reverse");
        }
    }



    if (/\/http/.test(to.path)) {
        let url = to.path.split("http")[1];
        window.location.href = `http${url}`;
    } else {
        let userId
        if (!store.state.userInfo.id) {
            userId = utils.getQueryString('userId')

        } else {
            userId = store.state.userInfo.id
        }


        if (to.path == '/login' || to.path == '/loginSe') {
            next();
        } else {

            axios.get('/userInfo/getUserInfo').then(res => {
                if (res.status === 200) {
                    if (res.data.success) {
                        if (!res.data.data) {
                            // next('/login')
                            if (isWeiXin) {
                                window.open('http://xjw.izouma.com/wx/auth?redirectUri=' + to.name);
                            } else {
                                next('/login')
                            }

                            return;
                        }
                        store.commit('updateUserInfo', res.data.data);
                        next();
                        return;
                    }
                }
                next('/login');
                // if (isWeiXin) {
                //     window.open('http://xjw.izouma.com/wx/auth?redirectUri=' + to.name);

                // } else {
                //     next('/login')
                // }
            }).catch(() => {
                // next('/login');
            })
        }


    }
});

function isWeiXin() {
    //window.navigator.userAgent属性包含了浏览器类型、版本、操作系统类型、浏览器引擎类型等信息，这个属性可以用来判断浏览器类型
    var ua = window.navigator.userAgent.toLowerCase();
    //通过正则表达式匹配ua中是否含有MicroMessenger字符串
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        return true;
    } else {
        return false;
    }
}

router.afterEach(function(to) {
    if (window.cordova && cordova.plugins && cordova.plugins.navigationbar) {
        cordova.plugins.navigationbar.create();
        if (/^order$|^appoint$|^user$/.test(to.name)) {
            cordova.plugins.navigationbar.hideBack();
        } else {
            cordova.plugins.navigationbar.showBack();
        }
    }
    axios.get('/wx/getSignature', {
            params: {
                shareUrl: location.href.split('#')[0]
            }
        })
        .then(response => {
            console.log(response)
            wx.config({
                debug: false,
                appId: response.data.appId,
                timestamp: response.data.timestamp,
                nonceStr: response.data.nonceStr,
                signature: response.data.signature,
                jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'openLocation', 'getLocation']
            })
        })

    store.commit("updateLoading", false);
});
export default router