import Vue from "vue";
import Router from "vue-router";
import Store from "./store";
import Layout from "./views/Layout.vue";
import fileNotFound from "./views/errors/fileNotFound.vue";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
NProgress.configure({ showSpinner: false });

Vue.use(Router);

const originalPush = Router.prototype.push;
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch((err) => err);
};

const router = new Router({
  routes: [
      //不需要鉴权的页面
    builder("/", "portal/IndexView"),
    builder("/index", "portal/IndexView"),



    builder("/product", "portal/ProductView"),



    builder("/product/productId/:productId", "portal/ProductDetailsView"),
    builder("/example", "portal/ExampleView"),
    builder("/news", "portal/NewsView"),
    builder("/news/newsId/:newsId", "portal/NewsDetailsView"),
    builder("/about", "portal/AboutView"),


  ],
});

/**
 * rest路由生成器
 *
 * @param {string} path 路径
 * @param {string} [component=path] 文件地址
 * @returns {array} [] reset路由数组，包含list/edit/add/view
 */
function restBuilder(path, component = path) {
  let arr = [];
  arr.push(builder(`${path}/list`, `${component}/list`));
  arr.push(builder(`${path}/edit`, `${component}/show`));
  arr.push(builder(`${path}/add`, `${component}/show`));
  arr.push(builder(`${path}/view`, `${component}/view`));
  return arr;
}

/**
 * 路由生成器
 *
 * @param {string} path 路径
 * @param {string} [component=path] 文件地址
 * @param {boolean} [requiresAuth=false] 是否登录后才能访问
 * @returns {any} {} 路由对象
 */
function builder(path, component = path, requiresAuth = false, activeMenu = '') {
  return {
    path: path,
    name: path || "homepage",
    component: getComponent(component),
    meta: {
      requiresAuth: requiresAuth,
      activeMenu:activeMenu
    },
  };
}

function getComponent(fileName) {
  try {
    return require("./views/" + fileName).default;
  } catch (error) {
    return fileNotFound;
  }
}
//检查是否跳转到sso地址
function ssoCheck(to, from, next) {
  console.log("to", to);
  console.log("from", from);
  console.log("next", next);
  let redirect = to.path === "/login/updatePwd" ? "/#/updatePwd" : "";
  redirect =
    redirect === "" && to.path === "/login"
      ? location.protocol + "//" + location.host + "/#" + from.fullPath
      : redirect;
  if (redirect != "") {
    next(false);
    window.location.href =
      "//" +
      location.host +
      "/m/login/logout?redirect=" +
      encodeURIComponent(redirect);
    return true;
  }
  return false;
}

router.afterEach(() => {
  document.body.scrollTop = 0;
  document.documentElement.scrollTop = 0;
  NProgress.done();
});

// 路由鉴权
router.beforeEach((to, from, next) => {
  // if (ssoCheck(to, from, next)) { //sso鉴权检查
  //   return
  // }
  NProgress.start();
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    if (Store.state.isLogin) {
      next();
    } else {
      next({
        path: "/authentication",
        query: {
          redirect: to.fullPath,
        },
      });
    }
  } else {
    next();
  }
});

export default router;
