import Vue from "vue";

import Router from "vue-router";
import Layout from "@/layout";
import store from "@/store";
import Cookies from "js-cookie";
Vue.use(Router);
//不管是超级管理员还是普通用户都可以看到的页面
export const constantRoutes = [
  // 登录页
  {
    path: "/login",
    // meta: { isAuth: true },
    component: () => import("@/views/login/index"),
    hidden: true
  },
  {
    path: "/",
    name: "Layout",
    // redirect: '/login',
    // alwaysShow: true,
    component: Layout,
    children: [
      {
        path: "inform",
        name: "Inform",
        meta: { isAuth: true, title: "系统通知" },
        component: () => import("@/views/inform")
      },
      {
        path: "home/ICcard",
        name: "icCard",
        meta: { isAuth: true, title: "IC卡管理" },
        component: () => import("@/views/icCard")
      },
      {
        path: "home/fingerprint",
        name: "fingerprint",
        meta: { isAuth: true, title: "指纹管理" },
        component: () => import("@/views/fingerprint")
      }
    ]
  }
  // {
  //     path: '*',
  //     redirect: '/login',
  //     // meta: { isAuth: true },
  //     // component: () => import('@/views/login/index'),
  //     hidden: true
  // }
];
const router = new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
});

export default router;
router.beforeEach((to, from, next) => {
  //判断 如果cook是否存在，可以进去
  if (Cookies.get("cook") == store.state.user.token) {
    // console.log(1111)
    next(); //放行
  } else {
    if (to.path === "/") {
      // console.log(3333);
      next({ path: "/login" });
    } else {
      // console.log(4444444);
      if (
        Cookies.get("cook") &&
        store.state.user.statRoutes[0].children.length === 3
      ) {
        // console.log(rsaPassWord);
        store
          .dispatch("user/login", {
            username: Cookies.get("user"),
            password: Cookies.get("password")
          })
          .then(() => {
            // console.log('重新请求token');
            next({ ...to });
          })
          .catch(error => {
            // console.log('重新请求失败');
            Cookies.remove("cook");
          });
      } else {
        // console.log(666666);
        // console.log(to);
        if (to.path !== "/login") {
          next({ path: "/login" });
        } else {
          next();
        }
      }
    }
  }
});

//全局前置路由守卫————初始化的时候被调用、每次路由切换之前被调用
// router.beforeEach((to, from, next) => {
//     console.log('000000');
//     if (to.meta.isAuth) {
//         //判断 如果cook是否存在，可以进去
//         if (Cookies.get('cook') == store.state.user.token) {
//             console.log(1111)
//             next()  //放行
//         } else {
//             // alert('登录身份失效,请重新登录')
//             // router.addRoute("Layout", {
//             //     path: item,
//             //     name: item,
//             //     meta: { isAuth: true },
//             //     component: () => import(`@/views/${item}`),
//             // });
//             console.log(22222);
//             next({ path: "/login" })
//         }
//     } else {
//         // 否则，放行
//         console.log(3333);
//         if (Cookies.get('cook')) {
//             if (store.state.user.statRoutes[0].children.length === 3) {
//                 // console.log(rsaPassWord);
//                 store.dispatch("user/login", {
//                     username: Cookies.get('user'),
//                     password: Cookies.get('password')
//                 })
//                     .then(() => {
//                         console.log(444444);
//                         next({ ...to })
//                         // next()
//                     })
//                     .catch((error) => {
//                         console.log(55555);
//                         next()
//                     });

//             } else {
//                 console.log(666666);
//                 next()
//             }
//         } else {
//             console.log(777777);
//             next()
//         }

//         // next()

//     }
// })
