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/tabIndex.vue"], resolve),
    redirect: "/design/dtg",
    children: [
      // DTG
      {
        path: "dtg",
        name: "design-dtg",
        meta: { title: "DTG生产" },
        component: resolve => require(["../views/design/index.vue"], resolve)
      },
      // DTF 列表
      {
        path: "dtf",
        name: "design-dtf",
        meta: { title: "DTF生产" },
        component: resolve => require(["@/views/dtf-list/index.vue"], resolve)
      },
      // DTF 排版设置
      {
        path: "dtf/setting",
        name: "dtf-setting",
        meta: { title: "DTF排版设置" },
        component: resolve =>
          require(["@/views/dtf-setting/index.vue"], resolve)
      },
      // DTF 批次详情
      {
        path: "dtf/batch-detail",
        name: "dtf-batch-detail",
        meta: { title: "DTF批次详情" },
        component: resolve =>
          require(["@/views/dtf-batch-detail/detail.vue"], resolve)
      }
    ]
  },
  // 删掉原来独立的 /dtf-setting、/dtf-batch-detail、/dtf-file
  {
    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;
