import { createRouter, createWebHashHistory } from "vue-router";
import home from "@/views/home/index.vue";
import test from "@/views/home/test.vue";

const routes = [
  {
    path: "/",
    name: "home",
    // component: test,
    component: home,
  },
  {
    path: "/login",
    name: "login",
    meta: {
      index: 0,
    },
    component: () => import("@/views/login.vue"),
  },
  {
    path: "/register",
    name: "register",
    meta: {
      index: 1,
    },
    component: () => import("@/views/register.vue"),
  },
  {
    path: "/group_buy",
    name: "group_buy",
    meta: {
      index: 1,
    },
    component: () => import("@/views/group_buy/index.vue"),
  },
  {
    path: "/table",
    name: "table",
    meta: {
      index: 1,
    },
    component: () => import("@/views/table/index.vue"),
  },
  {
    path: "/order",
    name: "order",
    meta: {
      index: 1,
    },
    component: () => import("@/views/order/index.vue"),
  },
  {
    path: "/member",
    name: "member",
    meta: {
      index: 1,
    },
    component: () => import("@/views/member/index.vue"),
  },
  {
    path: "/queue",
    name: "queue",
    meta: {
      index: 1,
    },
    component: () => import("@/views/queue/index.vue"),
  },
  {
    path: "/work",
    name: "work",
    meta: {
      index: 1,
    },
    component: () => import("@/views/work/index.vue"),
  },
  {
    path: "/workrecord",
    name: "workrecord",
    meta: {
      index: 1,
    },
    component: () => import("@/views/work/record.vue"),
  },
  {
    path: "/device_list",
    name: "device_list",
    component: () => import("@/views/device/index.vue"),
  },
  {
    path: "/add_device",
    name: "add_device",
    component: () => import("@/views/device/add.vue"),
  },
  {
    path: "/add_label",
    name: "add_label",
    component: () => import("@/views/device/add_label.vue"),
  },
  {
    path: "/webview",
    name: "webview",
    component: () => import("@/views/webview/index.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach(async (to, from) => {
  const token = localStorage.getItem("token");
  if (!token && to.name !== "login" && to.name !== "register") {
    return { name: "login" };
  }
});

export default router;
