import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "login",
    meta: {
      title: "登录"
    },
    component: resolve => require(["../views/login/index.vue"], resolve)
  },
  {
    path: "/design",
    name: "design",
    meta: {
      title: "设计页面"
    },
    component: resolve => require(["../views/design/index.vue"], resolve)
  },

  // {
  //   path: "/design-detail",
  //   name: "design",
  //   meta: {
  //     title: "设计详情页面"
  //   },
  //   component: resolve => require(["../views/design/detail/index.vue"], resolve)
  // },
  {
    path: "/dtf-setting",
    name: "dtfSetting",
    meta: {
      title: "DTF排版设置"
    },
    component: resolve => require(["@/views/dtf-setting/index.vue"], resolve)
  },
  {
    path: "/dtf-batch-detail",
    name: "dtfBatchDetail",
    meta: {
      title: "DTF批次详情"
    },
    component: resolve =>
      require(["@/views/dtf-batch-detail/detail.vue"], resolve)
  },
  {
    path: "/dtf-file",
    name: "dtfFileDownLoad",
    meta: {
      title: "DTF下载文件详情"
    },
    component: resolve => require(["@/views/dtf-file/index.vue"], resolve)
  },
  {
    path: "*",
    redirect: "/"
  }
];

const router = new VueRouter({
  mode: "hash",
  base: process.env.BASE_URL,
  routes
});
router.beforeEach((to, from, next) => {
  let user = Vue.prototype.$dataStore.get("user");
  if (to.path.includes("design-detail")) {
    return next();
  }

  if (to.name === "login") {
    if (user) {
      return next({ path: "/design" });
    } else {
      return next();
    }
  } else {
    if (user) {
      return next();
    } else {
      console.log("router", user);

      return next("/");
    }
  }
});
export default router;
