import Vue from "vue";
import VueRouter from "vue-router";
import store from "../store";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "pageMenu"
  },
  {
    path: "/home",
    name: "home",
    component: () => import("../views/Home.vue")
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/login.vue")
  },
  {
    path: "/loginHome",
    name: "loginHome",
    component: () => import("../views/loginHome.vue")
  },
  {
    path: "/pageMenu",
    name: "pageMenu",
    component: () => import("../views/PageMenu.vue"),
    meta: {
      checkLogin: true
    }
  },
  {
    path: "/arrangeList",
    name: "arrangeList",
    component: () => import("../views/programme/arrangeList.vue"),
    meta: {
      checkLogin: true
    }
  },
  {
    path: "/programmeList",
    name: "programmeList",
    component: () => import("../views/programme/programmeList.vue"),
    meta: {
      checkLogin: true
    }
  },
  {
    path: "/programmeDetail",
    name: "programmeDetail",
    component: () => import("../views/programme/programmeDetail.vue"),
    meta: {
      checkLogin: true
    }
  },
  {
    path: "/inspectorList",
    name: "inspectorList",
    component: () => import("../views/inspector/inspectorList.vue"),
    meta: {
      checkLogin: true
    }
  },
  {
    path: "/inspectorRecord",
    name: "inspectorRecord",
    component: () => import("../views/inspector/inspectorRecord.vue"),
    meta: {
      checkLogin: true
    }
  },
  {
    path: "/addRecord",
    name: "addRecord",
    component: () => import("../views/inspector/addRecord.vue"),
    meta: {
      checkLogin: true
    }
  },
  {
    path: "/expertList",
    name: "expertList",
    component: () => import("../views/expert/expertList.vue"),
    meta: {
      checkLogin: true
    }
  },
  {
    path: "/expertDetail",
    name: "expertDetail",
    component: () => import("../views/expert/expertDetail.vue"),
    meta: {
      checkLogin: true
    }
  },
  {
    path: "/expertAdd",
    name: "expertAdd",
    component: () => import("../views/expert/expertAdd.vue"),
    meta: {
      checkLogin: true
    }
  },
  {
    path: "/recordList",
    name: "recordList",
    component: () => import("../views/record/recordList.vue"),
    meta: {
      checkLogin: true
    }
  },
  {
    path: "/recordDetail",
    name: "recordDetail",
    component: () => import("../views/record/recordDetail.vue"),
    meta: {
      checkLogin: true
    }
  },
  {
    path: "/recordCheck",
    name: "recordCheck",
    component: () => import("../views/record/recordCheck.vue"),
    meta: {
      checkLogin: true
    }
  },
  {
    path: "/recordAdd",
    name: "recordAdd",
    component: () => import("../views/record/recordAdd.vue"),
    meta: {
      checkLogin: true
    }
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
  scrollBehavior(to, from, savedPosition) {
    console.log(savedPosition);
    if (savedPosition) {
      return new Promise(resolve => {
        setTimeout(() => {
          resolve(savedPosition);
        }, 200);
      });
    } else if (to.hash) {
      return {
        selector: to.hash
      };
    } else {
      return { x: 0, y: 0 };
    }
  }
});

router.beforeEach((to, from, next) => {
  let exports = ["ROLE_EXPERT", "ROLE_DISTRICT_STAFF"];
  let exportsInfo = store.state.roles.find(item => {
    return exports.includes(item);
  });
  let inspectors = ["ROLE_SUPERVISOR"];
  let inspectorsInfo = store.state.roles.find(item => {
    return inspectors.includes(item);
  });
  let loginPhone = window.localStorage.getItem("loginPhone");

  if (to.name === "home" && !loginPhone) {
    next({
      name: "loginHome",
      query: {
        ...to.query
      }
    });
  } else if (
    !store.state.userInfo &&
    to.path !== "/login" &&
    to.meta.checkLogin
  ) {
    store
      .dispatch("getUserInfo")
      .then(() => {
        next();
        return;
      })
      .catch(() => {
        next("/login");
      });
  } else if (!to.matched.length) {
    next("/404");
  } else {
    next();
  }
});

export default router;
