import { createApp } from "vue";
import ElementPlus from "element-plus";
import "element-plus/dist/index.css";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import md5 from "md5";
import "./styles/index.css";

const app = createApp(App);

router.beforeEach(async (to, from, next) => {
  let userId = localStorage.userId;
  if (userId) {
    store.commit("changeLogin", true);
    if (0 === to.matched.length) {
      next("/voyage/voyageList");
    } else if (to.path == "/login" || to.path == "/") {
      next("/voyage/voyageList");
    } else {
      next();
    }
  } else {
    store.commit("changeLogin", false);
    if (to.path == "/login") {
      next();
    } else {
      next("/login");
    }
  }
});
router.afterEach((to, from) => {
  let { title } = to.meta;
  document.title = title;
  store.commit("setCurrentMenuItem", to.path);
  store.commit("changefirstTitle", title);
});
app.config.globalProperties.check = () => {
  console.log("check");
};

app.use(router).use(ElementPlus).use(store).mount("#app");
