import Vue from 'vue'
import VueRouter from 'vue-router'
import NavLayout from "@/components/layout/NavLayout"
import store from '@/store'
import { isSupportHtml5Plus } from '@/utils/platform'
import {
    setTitle
} from "@/utils/title"
import Login from '@/views/login/Login'
import Register from '@/views/login/Register'
import FotgetPassword from '@/views/login/FotgetPassword'
import Index from '@/views/index/Index'
import personal from './personal'
import order from './order'
import replenishment from './replenishment'
import device from './device'
Vue.use(VueRouter)

const routes = [
	{
        path: '/404',
        name: '404',
        component: () => import('@/views/404/404.vue')
    },
    {
        path: '/login',
        name: 'login',
        component: Login,
        meta: {
            title: '登录'
        }
    },
    {
        path: '/register',
        name: 'register',
        component: Register,
        meta: {
            title: '注册'
        }
    },
    {
        path: '/fotgetPassword',
        name: 'fotgetPassword',
        component: FotgetPassword,
        meta: {
            title: '忘记密码'
        }
    },
    {
        path: '/home',
        name: 'home',
        component: NavLayout,
        redirect: "/home/index",
        children: [{
            path: "index",
            name: "homeIndex",
            component: Index,
            meta: {
                title: '首页',
                hasBot: true,
                hasTop: false
            }
        }]
    },
]

const whiteList = [
	'/login', 
	'/register', 
	'/fotgetPassword',
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: routes.concat( personal, order, replenishment, device )
})


function redirectToLogin(to, from, next) {
    let isApp = to.query.isApp || isSupportHtml5Plus
    if(isApp) {
        next(`/login?isApp=1&redirectUrl=${encodeURIComponent(to.fullPath)}`)
    } else {
      next('/login')
    }
}

if(process.env.NODE_ENV==='production') {
    router.beforeEach(async (to, from, next) => {
        next()
        return
        if((store.getters.user && store.getters.user.accessToken)) {
            next()
        } else {
            if( whiteList.indexOf(to.path) > -1) {
                next()
            } else {
                redirectToLogin(to, from, next)
            }
        }
    })
} else {
    router.beforeEach(async (to, from, next) => {
        next()
        return
        if ((!store.getters.user || !store.getters.user.accessToken) && whiteList.indexOf(to.path) == -1) {
            next('/login')
            return
        }
        next()
    })
}

const noSetTitleList = []
router.afterEach((to, from) => {
    // 更新页面标题
    console.log(to,from)
    if (to.meta && to.meta.title && noSetTitleList.indexOf(to.path)==-1) {
        setTitle(to.meta.title)
        store.dispatch("SetPageTitle", to.meta.title)
    }
    else if(noSetTitleList.indexOf(to.path)>-1){
      
    }
    window.scrollTo(0, 0);
})

export default router
