import Vue from 'vue'
import Router from 'vue-router'

//import util from '../utils/util.js'
import ubt from '../utils/ubt'
import system from '../utils/system'
// import HelloWorld from '@/components/HelloWorld'

import Login from '@/pages/login/app.vue'
import Im from '@/pages/im/app.vue'
import AdviserList from '@/pages/im/adviserList.vue'
import MessageList from '@/pages/im/messageList.vue'
import GetTel from '@/pages/im/getTel.vue'
import label from '@/pages/labelPage/label.vue'
import detail from '@/pages/detail/detail.vue'
import templateSlide from '@/pages/templateSlide/templateSlide.vue'
import updateTemp from '@/pages/updateTemp/updateTemp.vue'
import preview from '@/pages/previewPages/preview.vue'
import template10 from '@/previewComponents/template10/app.vue'

Vue.use(Router)
const router = new Router({
    routes: [
        // 页面初始化
        {
            path: '/',
            redirect: '/pages/login'
        },
        {
            path: '/pages/login',
            name:'Login',
            component: Login
        },
        {
            path: '/pages/im',
            name:'im',
            component: Im,
            meta:{
                pageid:'01011092',
                enterKey:'sfc.nb.app.home.videoshow.adviserList.item.enter',
                statetimeKey:'sfc.nb.app.home.videoshow.adviserList.item.statetime'
            }
        },
        {
            path: '/pages/template10',
            name:'template10',
            component: template10,
        },
        {
            path: '/pages/getTel',
            name:'getTel',
            component: GetTel,
        },
        {
            path: '/pages/messageList',
            name:'messageList',
            component: MessageList,
            meta:{
                pageid:'01011091',
                enterKey:'sfc.nb.app.home.videoshow.adviserList.enter',
                statetimeKey:'sfc.nb.app.home.videoshow.adviserList.statetime'
            }
        },
        {
            path: '/pages/adviserList',
            name:'AdviserList',
            component: AdviserList,
            meta:{
                pageid:'01011093',
                enterKey:'sfc.nb.app.home.videoshow.enter',
                statetimeKey:'sfc.nb.app.home.videoshow.statetime'
            }
        },

        {
            path: '/pages/label',
            component: label,
            name:'label',
            meta: {
                requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path: '/pages/detail',
            name:'detail',
            component: detail,
            meta: {
                requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
            },

        },
        {
            path: '/pages/templateSlide',
            component: templateSlide,
            name:'templateSlide',
            meta: {
                requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path: '/pages/updateTemp',
            component: updateTemp,
            name:'updateTemp',
            meta: {
                requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path: '/pages/preview',
            component: preview,
            name:'preview'
        },
        // ...Assistant
    ],
    // components:
    //     {'elab-tempOne':tempOne}

    // ,
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            return { x: 0, y: 0 }
        }
    }
})

router.beforeEach((to, from, next) => {
    //获取native数据
    system.getSessionId();
    system.getNativeUuid();
    system.getMobile();
    system.getNativeToken();


    window.fromPageID =   (from && from.meta && from.meta.pageid) || (to && to.meta && to.meta.pageid);
    window.stateTime = new Date().getTime();
    window.fromPageName = (from && from.meta && from.meta.pagename) || (to && to.meta && to.meta.pagename);

    setTimeout(function() {
        //debugger;
        //util.trackPageview(to.path, from.path);
        ubt.trackUserTime(to.meta.pageid, to.meta.eventKey);
        console.log("router:start");

        if (to && to.meta.pageid) {
            let referPageid = (from && from.meta && from.meta.pageid) || (to && to.query && to.query.referPageid);
            if (to.meta.enterKey) {
                if(to.meta.pageid=='01011092'){
                    ubt.trackEvent(to.meta.pageid, to.meta.enterKey, referPageid,'',JSON.parse(sessionStorage.getItem('adviserInfo')).id,'','','0','','','',JSON.parse(sessionStorage.getItem('adviserInfo')).tel)
                    ubt.trackUserTime(to.meta.pageid, to.meta.statetimeKey, referPageid)
                }
                else if(to.meta.pageid=='01011091'){
                    ubt.trackEvent(to.meta.pageid, to.meta.enterKey, referPageid,'','','','','0','','','','')
                    ubt.trackUserTime(to.meta.pageid, to.meta.statetimeKey, referPageid)
                }
                else{
                    ubt.trackPageview(to.meta.pageid, to.meta.enterKey, referPageid, to.params.id);

                    ubt.trackUserTime(to.meta.pageid, to.meta.statetimeKey, referPageid,'');

                }



            }

            system.initPageMeta({pageid:to.meta.pageid});
        }
    }, 500);
    if(to.meta.requireAuth){
        if(localStorage.getItem('_accout')){
            next()
        }else{
            next({
                path: '/pages/login',
                //query: {redirect: to.fullPath}
            });
        }
    }else{
        next();
    }

})

export default router;

