import Vue from 'vue'
import Router from 'vue-router'
import { routes } from './router'
import util from './../util/util'

Vue.use(Router)

export const router = new Router({
  routes: routes
})

function deviceReady(callback) {
  if (window.EbeiPlugins) {
    callback && callback()
  }
  else {
    document.addEventListener('deviceready', callback, false)
  }
}

/*
 全局路由钩子
 */
router.beforeEach((to, from, next) => {
  deviceReady(function(){
    util.title(to.meta.title); //修改标题
    EbeiPlugins.getLocalUserInfo(function (data) {
    	alert(json.stringify(data));
      localStorage.setItem('memberId', data.userid);
      localStorage.setItem('user_id', data.userid);
      localStorage.setItem('userName', data.userName);
      localStorage.setItem('userAccount', data.userAccount);
      localStorage.setItem('phoneNumber', data.telephone);
      localStorage.setItem('nickname', data.nickname);
      localStorage.setItem('fullName', data.nickname);
      localStorage.setItem('projectId', data.projectId);
      localStorage.setItem('projectName', data.projectName);
      localStorage.setItem('companyId', data.companyId);
      localStorage.setItem('areaId', data.areaId);
      localStorage.setItem('cBuildingId', data.cBuildingId);
      next()
    }, function () {
      alert("获取用户信息出错");
    });
  })
  next()
})

router.afterEach((to, from) => {
  // Vue.$vux.loading.hide(); //隐藏加载状态
//	uploadAccessLog(to.meta.title); //写log日志
});
