import Vue from 'vue'
import Router from 'vue-router'
import store from './Store'
import menu from './config/menu'
import { $viewLoading } from './components/shared/Loading'

Vue.use(Router)

const routes = [];
menu.forEach((item, i) => {
    routes.push({
        name:item.name,
        path: `${item.link}`,
        meta: {
            loading: null
        },
        component: () => import(`./views/${item.name}/index.vue`)
    })
})

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

const $loading = $viewLoading({ duration: 100 })
router.beforeEach((to, from, next) => {
    if (to.meta.loading === null) {
        to.meta.loading = true
        $loading.show()
    }
    next()
})

router.afterEach((to, from) => {
    if (to.meta.loading) {
        to.meta.loading = false
        $loading.hide()
    }
})

export default router