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 "./styles/index.css";
import Uploader from "./components/Uploader.vue";
import Certs from "./components/Certs.vue";
import RemoteSearch from "./components/RemoteSearch.vue";
import RemoteSelect from "./components/RemoteSelect.vue";
import zhCn from "element-plus/dist/locale/zh-cn.mjs";

const app = createApp(App);
app.use(ElementPlus, {
  locale: zhCn,
});
app.component("Certs", Certs);
app.component("Uploader", Uploader);
app.component("RemoteSearch", RemoteSearch);
app.component("RemoteSelect", RemoteSelect);
import * as ElementPlusIconsVue from "@element-plus/icons-vue";
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
router.beforeEach(async (to, from, next) => {
  let id = localStorage.id;
  if (id) {
    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 {
    localStorage.removeItem("staffPhone");
    localStorage.removeItem("id");
    localStorage.removeItem("status");
    localStorage.removeItem("userType");
    localStorage.removeItem("staffName");
    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");
