import { route } from 'quasar/wrappers';
import { getMenus } from 'src/router/routerControl/index';
import {
  createMemoryHistory,
  createRouter,
  createWebHashHistory,
  createWebHistory
} from 'vue-router';
import routes from './routes';
import { LocalStorage, Notify } from 'quasar';
// import { format } from 'path';
import { useZebraStore } from '../stores/zebra-store';

export default route(function (/* { store, ssrContext } */) {
  const createHistory = process.env.SERVER
    ? createMemoryHistory
    : process.env.VUE_ROUTER_MODE === 'history'
    ? createWebHistory
    : createWebHashHistory;
  const Router = createRouter({
    scrollBehavior: () => ({ left: 0, top: 0 }),
    routes,
    history: createHistory(process.env.VUE_ROUTER_BASE)
  });

  //每次刷新页面路由丢失后，重新加载路由
  if (LocalStorage.has('token')) {
    getMenus(Router);
  }

  Router.beforeEach((to, form, next) => {
    if (to.path === '/loginIndex' || to.path === '/scannerzebra') {
      next();
    } else if (!LocalStorage.has('token') && !LocalStorage.has('noSignin')) {
      Notify.create({
        progress: true,
        type: 'warning',
        icon: 'warning',
        position: 'top',
        message: '未登录，请登录！'
      });
      next('/loginIndex');
    } else {
      next();
    }
  });

  Router.afterEach((to, form) => {
    const zebraStore = useZebraStore();
    const fullPath = to.fullPath; // 获取路由前往的地址
    if (fullPath === '/scannerzebra/zebra' || fullPath === '/scannerzebra') {
      zebraStore.showBackBtnFlag = false; // 隐藏返回按钮
    } else {
      zebraStore.showBackBtnFlag = true; // 显示返回按钮
    }
  });
  return Router;
});
