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";

const app = createApp(App);
app.component("Certs", Certs);
app.component("Uploader", Uploader);
app.component("RemoteSearch", RemoteSearch);

router.beforeEach(async (to, from, next) => {
  let id = localStorage.id;
  if (id) {
    store.commit("changeLogin", true);
    if (0 === to.matched.length) {
      next("/cargoOwnerManage/cargoOwnerCompanyList");
    } else if (to.path == "/login" || to.path == "/") {
      next("/cargoOwnerManage/cargoOwnerCompanyList");
    } 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");
