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 tcb from "./apis/cloudLogin";
const db = tcb.database();
const cloudConfig = db.collection("huihenduo_manage_config");

const app = createApp(App);

router.beforeEach(async (to, from, next) => {
  let res = await cloudConfig.doc("18ed09686196068205eeb77612d641c6").get();
  let currentVersion = localStorage.version;
  let { version } = res.data[0];
  if (currentVersion == version) {
    let id = localStorage.id;
    if (id) {
      store.commit("changeLogin", true);
      if (0 === to.matched.length) {
        next("/cargoOwnerManage/cargoOwnerList");
      } else if (to.path == "/login" || to.path == "/") {
        next("/cargoOwnerManage/cargoOwnerList");
      } else {
        next();
      }
    } else {
      store.commit("changeLogin", false);
      if (to.path == "/login") {
        next();
      } else {
        next("/login");
      }
    }
  } else {
    localStorage.removeItem("staffPhone");
    localStorage.removeItem("id");
    localStorage.removeItem("status");
    localStorage.removeItem("userType");
    localStorage.removeItem("staffName");
    localStorage.setItem("version", version);
    location.reload();
    store.commit("changeLogin", false);
    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");
