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 = VueRouter.prototype.push;
VueRouter.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("/authentication", "login/authentication"),
    builder("/login", "login/login"),
    builder("/sso", "SSO"),
    {
      path: "/",
      name: "layout",
      component: Layout,
      meta: {
        requiresAuth: true,
      },
      children: [
        ...restBuilder("oper/log", "system/log"), // 系统管理-操作日志
        ...restBuilder("validcode", "system/validcode"), // 接入管理-接入信息
        ...restBuilder("menu", "system/menu"), // 系统管理--菜单管理
        ...restBuilder("resource", "system/resource"), // 系统管理--资源管理
        ...restBuilder("role", "system/role"), // 系统管理--角色管理
        ...restBuilder("user", "system/user"), // 用户管理 -- 管理用户
        ...restBuilder("param", "system/param"), // 系统管理--参数管理
        ...restBuilder("task", "system/task"), // 系统管理--任务管理
        // 绩效负责人
        ...restBuilder("workman", "workman"),

        //部门
        ...restBuilder("dept", "dept"), //部门
        ...restBuilder("staff", "staff"), //花名册 员工信息
        ...restBuilder("staff/care", "staff/care"), // 员工关怀信息
        ...restBuilder("care/template", "care/template"), // 关怀问候信息
        ...restBuilder("staff/black", "staff/black"), // 员工黑名单信息

        ...restBuilder("job", "job"), // 职位信息
        ...restBuilder("staff/adjust/log", "staff/adjust/log"), // 员工调岗信息
        ...restBuilder("staff/onboard", "staff/onboard"), // 员工入职信息
        ...restBuilder("staff/regular", "staff/regular"), // 员工转正信息
        ...restBuilder("staff/leave", "staff/leave"), // 员工离职信息
        ...restBuilder("staff/contract", "staff/contract"), // 员工合同信息

        ...restBuilder("attendance/record/hik", "attendance/record/hik"), // 海康考勤
        ...restBuilder("attendance/record/error", "attendance/record/error"), // 考勤打卡记录异常信息
        ...restBuilder("attendance/stat", "attendance/stat"), // 考勤汇总信息
        ...restBuilder("attendance/staff/stat", "attendance/staff/stat"), // 考勤人员汇总信息

        ...restBuilder("attendance/record", "attendance/record"), // 考勤打卡记录信息
        ...restBuilder("attendance/record/detail", "attendance/record/detail"), // 考勤打卡记录详细信息
        ...restBuilder("attendance/group", "attendance/group"), // 考勤组信息
        ...restBuilder("attendance/class", "attendance/class"), // 考勤班次信息
        ...restBuilder("attendance/leave/record", "attendance/leave/record"), // 请假记录信息
        ...restBuilder(
          "attendance/vacation/balance",
          "attendance/vacation/balance"
        ), // 员工假期余额信息

        ...restBuilder("staff/perform/stat", "staff/perform/stat"), //员工分数统计
        ...restBuilder("dept/perform/stat", "dept/perform/stat"), //部门绩效分数统计
        ...restBuilder(
          "perform/perpose/staff/stat",
          "perform/perpose/staff/stat"
        ), //员工目标统计
        ...restBuilder(
          "perform/perpose/dept/stat/stat",
          "perform/perpose/dept/stat/stat"
        ), //部门目标统计
        ...restBuilder("perform/perpose/staff", "perform/perpose/staff"), //员工考核目标详细信息表
        ...restBuilder(
          "perform/perpose/staff/stat",
          "perform/perpose/staff/stat"
        ), //员工考核目标详细信息表
        ...restBuilder("perform/perpose/dept", "perform/perpose/dept"), //部门考核目标详细信息表
        ...restBuilder(
          "perform/perpose/dept/stat",
          "perform/perpose/dept/stat"
        ), //部门考核目标详细信息表
        ...restBuilder("perform/perpose", "perform/perpose"), //考核目标表
        ...restBuilder("window/owner", "window/owner"), //窗口负责人表
        ...restBuilder(
          "perform/staff/conf/detail",
          "perform/staff/conf/detail"
        ), //人员考核内容详细信息表
        ...restBuilder("perform/staff/conf", "perform/staff/conf"), //人员自动考核信息表
        ...restBuilder("perform/dept/conf/detail", "perform/dept/conf/detail"), //部门考核内容详细信息表
        ...restBuilder("perform/dept/conf", "perform/dept/conf"), //部门自动考核信息表
        ...restBuilder("perform/rules/category", "perform/rules/category"), //绩效规则分类信息表
        ...restBuilder("perform/rules", "perform/rules"), //绩效规则信息表

        ...restBuilder("perform/rules/attend", "perform/rules/attend"), //考勤绩效规则信息表
        ...restBuilder("perform/rules/review", "perform/rules/review"), //评价绩效规则信息表
        ...restBuilder("perform/rules/gowork", "perform/rules/gowork"), //办件绩效规则信息表
        ...restBuilder("perform/rules/effect", "perform/rules/effect"), //效能绩效规则信息表
        ...restBuilder("perform/rules/other", "perform/rules/other"), //其它绩效规则信息表

        ...restBuilder("perform/attend/record", "perform/attend/record"), //考勤绩效记录信息
        ...restBuilder("perform/review/record", "perform/review/record"), //评价绩效记录信息
        ...restBuilder("perform/complain/record", "perform/complain/record"), //投诉绩效记录信息
        ...restBuilder("perform/gowork/record", "perform/gowork/record"), //办件绩效记录信息
        ...restBuilder("perform/effect/record", "perform/effect/record"), //效能绩效记录信息
        ...restBuilder("perform/other/record", "perform/other/record"), //其它绩效记录信息

        ...restBuilder("check/attend/record", "check/attend/record"), //考勤绩效核查信息
        ...restBuilder("check/review/record", "check/review/record"), //评价绩效核查信息
        ...restBuilder("check/complain/record", "check/complain/record"), //投诉绩效核查信息
        ...restBuilder("check/gowork/record", "check/gowork/record"), //办件绩效核查信息
        ...restBuilder("check/effect/record", "check/effect/record"), //效能绩效核查信息
        ...restBuilder("check/other/record", "check/other/record"), //其它绩效核查信息


        ...restBuilder("perform/attend/appeal", "perform/attend/appeal"), //绩效记录申诉信息
        ...restBuilder("perform/attend/appeal/files", "perform/attend/appeal/files"), //绩效记录申诉信息附件列表

        ...restBuilder("feedback", "feedback"), //反馈信息
        ...restBuilder("feedback/file", "feedback/file"), //反馈信息附件
        ...restBuilder("feedback/addQuestion", "feedback/addQuestion"), //新增反馈
        ...restBuilder("feedback/questionnaire", "feedback/questionnaire"), //问卷情况

        ...restBuilder("feedback/question", "feedback/question"), //反馈问题
        ...restBuilder("feedback/option", "feedback/option"), //反馈选项
        ...restBuilder("feedback/answer", "feedback/answer"), //回答问题
        ...restBuilder("feedback/answer/option", "feedback/answer/option"), //回答问题选项

        ...restBuilder("staff/perform/summary", "staff/perform/summary"), //员工绩效统计汇总


        // 360评估
        ...restBuilder("homeCharts/record", "homeCharts/record"),
        //以下为基础路由配置
        builder("blank", "blank"),
        builder("", "Home"),
        builder("index", "Home"),
        builder("login/updatePwd", "login/updatePwd"),
        builder("403", "errors/403"),
        builder("*", "errors/404"),
      ],
    },
  ],
});

/**
 * 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) {
  return {
    path: path,
    name: path || "homepage",
    component: getComponent(component),
    meta: {
      requiresAuth: requiresAuth,
    },
  };
}

function getComponent(fileName) {
  try {
    return require("./views/" + fileName).default;
  } catch (error) {
    return fileNotFound;
  }
}
//检查是否跳转到sso地址
function ssoCheck(to, from, 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;
