import Report from "./../pages/report.vue"
import Login from "./../pages/login.vue"
import AccountsReceivable from "./../pages/accountsReceivable.vue"
import Results from "./../pages/results.vue"
import NewReport from "./../pages/newReport.vue"
import Index from "../pages/index.vue"

import { getParameterByName } from "../utils/auth"

const routes = [
  { path:"/", component: Index },
  { path: '/report', component: Report},
  { path:'/login', component: Login},
  { path:'/newReport', component: NewReport},
  { path:'/results', component: Results},
  { path:'/accountsReceivable', component: AccountsReceivable}
]

const router = new VueRouter({routes ,mode:'hash'})

router.beforeEach((to, _, next) => {
  const path = getParameterByName('path')
  if(to.path === "/" && !path){
    next()
  }else if (to.path !== '/login' && !getParameterByName('code')) {
    next({
      path:'login',
      query:{ path }
    })
  } else {
    next()
  }
})

export default router
