import * as ElIcons from "@element-plus/icons-vue";
import { createApp, reactive } from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import { getItem, removeAllItem } from "@/utils/storage";
import installElementPlus from "./plugins/element";
import plugins from "./plugins";
import "./assets/css/element-plus.scss";
import "./assets/css/layout.scss";
import "./assets/css/public.scss";
import ELMessage from "element-plus";
import InfiniteScroll from "./utils/lazyLoad";
import DictTag from "@/components/DictTag/index";
import * as echarts from "echarts";
import percentageDirective from "@/utils/percentage";
import VueUeditorWrap from "vue-ueditor-wrap";
import { ElMessageBox, ElMessage } from "element-plus"
const app = createApp(App);
app.directive("percentage", percentageDirective);
//注册自定义指令
// app.use(InfiniteScroll);
app.use(VueUeditorWrap);
Object.keys(ElIcons).forEach((key) => {
  //全局注册 element 图标
  app.component(key, ElIcons[key]);
});


//告诉前页面内的事件监听器内部是否会调用preventDefault函数来阻止事件的默认行为，以便浏览器根据这个信息更好地做出决策来优化页面性能
// import "default-passive-events";
import installDirective from "@/directives";

// 初始化样式表
import "@/styles/base.scss";

// import "@/styles/common.scss";
// 导入svgIcon
import installIcons from "@/icons";

//导入路由鉴权
import "./permission";

//全局属性(v2过滤器)
import installFilter from "@/filters";
//事件总线
import mitt from "mitt";

import { resetForm } from "@/utils/validate";
import { getDict, setCascader, getDictValue, setDict } from "@/utils/dict";
//挂载全局方法
app.config.globalProperties.resetForm = resetForm;
app.config.globalProperties.InfiniteScroll = InfiniteScroll;
app.config.globalProperties.$dict = getDict; //字典公共函数
app.config.globalProperties.$message = ELMessage; //element提示组件
app.config.globalProperties.$set = setCascader; //设置级联选择器回显
app.config.globalProperties.$getDictVal = getDictValue; //翻译字典数据
app.config.globalProperties.$setDict = setDict; //翻译字典数据
app.config.globalProperties.mittBus = new mitt(); //配置事件总线
app.component("DictTag", DictTag); //字典组件
// 将echarts挂载到全局中，这样组件就能通过 this.$echarts 访问了
app.config.globalProperties.$echarts = echarts; //echarts图表
installElementPlus(app);
installIcons(app);
installFilter(app);
installDirective(app);
app.use(store).use(router).use(plugins).mount("#app");

// 路由的导航守卫，主要防止未登录时跳转到其他页面
router.beforeEach((to, from, next) => {
  let token = getItem("token");
  if (to.path === "/login") {
    window.localStorage.removeItem("token");
    next();
  } else {
    if (!token) next("/login");
    else next();
  }
});
