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 ShipInfo from "./components/ShipInfo.vue";
import ShipCerts from "./components/ShipCerts.vue";
import RemoteSearch from "./components/RemoteSearch.vue";
import RemoteSelect from "./components/RemoteSelect.vue";
import zhCn from "element-plus/dist/locale/zh-cn.mjs";
import Layout from "./layout/Layout.vue";
import { createPinia } from "pinia";
const pinia = createPinia();
const app = createApp(App);
app.use(ElementPlus, {
  locale: zhCn,
});
app.use(pinia);
app.component("Layout", Layout);
app.component("Certs", Certs);
app.component("ShipInfo", ShipInfo);
app.component("ShipCerts", ShipCerts);
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.loginAccountId;
  if (id) {
    store.commit("changeLogin", true);
    if (0 === to.matched.length) {
      next("/index");
    } else if (to.path == "/login" || to.path == "/") {
      next("/index");
    } else {
      next();
    }
  } else {
    localStorage.removeItem("loginAccountId");
    localStorage.removeItem("loginName");
    localStorage.removeItem("loginPhone");
    localStorage.removeItem("shippingCompany");
    localStorage.removeItem("shippingId");
    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(store).mount("#app");
