<template>
  <div class="bj">
    <div class="masking">
    <el-menu
      :default-active="activeMenu"
      mode="horizontal"
      @select="handleSelect"
      class="_elMenu"
    >
      <template v-for="(item, index) in topMenus">
        <el-menu-item
          :key="index"
          :index="item.path"
          :style="{ '--theme': theme } "
        >
          <!-- <svg-icon
            v-if="item.meta && item.meta.icon && item.meta.icon !== '#'"
            :icon-class="item.meta.icon"
          /> -->
          <img :src="item.meta.img" alt="" style="height: 40px; width:auto;">
          {{ item.meta.title }}
        </el-menu-item>
      </template>
    </el-menu >
    </div>
  </div>

</template>

<script>
import {constantRoutes} from "@/router";
import {eventBus} from '@/main.js';

// 隐藏侧边栏路由
const hideList = ["/index", "/user/profile"];

export default {
  data() {
    return {
      // 顶部栏初始数
      visibleNumber: 50,

      // 当前激活菜单的 index
      currentIndex: undefined,
      img1: require('../assets/images/icon/menu (6).png'),
      img2: require('../assets/images/icon/menu (10).png'),
      img3: require('../assets/images/icon/menu (2).png'),
      img4: require('../assets/images/icon/menu (7).png'),
      img5: require('../assets/images/xzzf.png'),
      img6: require('../assets/images/icon/menu (8).png'),
      img7: require('../assets/images/icon/menu (9).png'),
      img8: require('../assets/images/icon/menu (1).png'),
      img9: require('../assets/images/icon/menu (11).png'),
      img10: require('../assets/images/icon/menu (5).png'),
      img11: require('../assets/images/icon/menu (12).png'),
      img12: require('../assets/images/icon/menu (4).png'),
    };
  },
  computed: {

    theme() {
      return this.$store.state.settings.theme;
    },
    // 顶部显示菜单
    topMenus() {
      let topMenus = [];
      //let imgArr= [this.img1,this.img2,this.img3,this.img4,this.img5,this.img6,this.img7,this.img8,this.img9,this.img10,this.img11,this.img12]
      let imgArr = {
        "/ehsRisk": this.img1,
        "/ehsMaterial": this.img2,
        "/ehsDocu": this.img3,
        "/ehsTeam": this.img4,
        "/ehsLawEnforce": this.img5,
        "/ehsNoticeMessage": this.img6,
        "/enterprise": this.img7,
        "/ehsNoticeMessage1": this.img8,
        "/ehsHiddenDangerMenu": this.img9,
        "/system": this.img10,
        "/monitor": this.img11,
        "/tool": this.img12
      };

      this.routers.map((menu, index) => {
        if (menu.hidden !== true) {
          console.log(menu.path);
          // 兼容顶部栏一级菜单内部跳转
          if (menu.path === "/") {
            topMenus.push(menu.children[0]);
          } else {
            //menu.meta.img = imgArr[index]
            menu.meta.img = imgArr[menu.path];
            topMenus.push(menu);
          }

        }
      });
      return topMenus;
    },
    // 所有的路由信息
    routers() {
      return this.$store.state.permission.topbarRouters;
    },
    // 设置子路由
    childrenMenus() {
      var childrenMenus = [];
      this.routers.map((router) => {
        for (var item in router.children) {
          if (router.children[item].parentPath === undefined) {
            if (router.path === "/") {
              router.children[item].path = "/" + router.children[item].path;
            } else {
              if (!this.ishttp(router.children[item].path)) {
                router.children[item].path =
                  router.path + "/" + router.children[item].path;
              }
            }
            router.children[item].parentPath = router.path;
          }
          childrenMenus.push(router.children[item]);
        }
      });
      return constantRoutes.concat(childrenMenus);
    },
    // 默认激活的菜单
    activeMenu() {
      const path = this.$route.path;
      let activePath = path;
      if (
        path !== undefined &&
        path.lastIndexOf("/") > 0 &&
        hideList.indexOf(path) === -1
      ) {
        const tmpPath = path.substring(1, path.length);
        activePath = "/" + tmpPath.substring(0, tmpPath.indexOf("/"));
        if (!this.$route.meta.link) {
          this.$store.dispatch("app/toggleSideBarHide", false);
        }
      } else if (!this.$route.children) {
        activePath = path;
        this.$store.dispatch("app/toggleSideBarHide", true);
      }
      this.activeRoutes(activePath);
      return activePath;
    },
  },
  created() {
    //消息提醒不放单首页了，放到右上角的消息提醒里，不再使用弹窗
    // this.openAlert();
    // this.openMaterialMsg();
  },
  /*  mounted() {
      //一秒后执行一次get方法，打印1
      //this.timer = setTimeout(this.get, 1000);
    },*/

  //销毁定时器，不然会一直存在
  /*  beforeDestroy() {
      clearTimeout(this.timer);
    },*/
  methods: {


    // 菜单选择事件
    handleSelect(key, keyPath) {
      this.currentIndex = key;
      const route = this.routers.find((item) => item.path === key);
      eventBus.$emit('toLogo', route);
      if (this.ishttp(key)) {
        // http(s):// 路径新窗口打开
        window.open(key, "_blank");
      } else if (!route || !route.children) {
        // 没有子路由路径内部打开
        const routeMenu = this.childrenMenus.find((item) => item.path === key);
        if (routeMenu && routeMenu.query) {
          let query = JSON.parse(routeMenu.query);
          this.$router.push({path: key, query: query});
        } else {
          this.$router.push({path: key});
        }
        this.$store.dispatch("app/toggleSideBarHide", true);
      } else {
        // 显示左侧联动菜单
        this.activeRoutes(key);
        this.$store.dispatch("app/toggleSideBarHide", false);

        let myRoutes = [];
        if (this.childrenMenus && this.childrenMenus.length > 0) {
          this.childrenMenus.map((item) => {
            if (key == item.parentPath || (key == "index" && "" == item.path)) {
              myRoutes.push(item);
            }
          });
        }
        setTimeout(() => {
          if(myRoutes[0].path != this.$route.path) {
            this.$router.replace({
              path: myRoutes[0].path,
              query:myRoutes[0].query && JSON.parse( myRoutes[0].query)
            })
          } else {
            this.$router.replace({
              path: '/index'
            })
          }
        }, 100)
      }
    },
    // 当前激活的路由
    activeRoutes(key) {
      var routes = [];
      if (this.childrenMenus && this.childrenMenus.length > 0) {
        this.childrenMenus.map((item) => {
          if (key == item.parentPath || (key == "index" && "" == item.path)) {
            routes.push(item);
          }
        });
      }
      if (routes.length > 0) {
        this.$store.commit("SET_SIDEBAR_ROUTERS", routes);
      } else {
        this.$store.dispatch("app/toggleSideBarHide", true);
      }
    },
    ishttp(url) {
      return url.indexOf("http://") !== -1 || url.indexOf("https://") !== -1;
    },
  },
};
</script>

<style lang="scss" scoped>
.bj{
  background-image: url("../assets/images/indexBj.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  height: calc(100vh - 120px);
}
.masking{
  height: 100%;
  background: rgba(255, 255, 255, 85%);
}
.el-menu-item {
  display: flex;
  text-align: center;
  width: 20%;
  line-height: 20px;
  height: 100px;
  border: 1px solid #c1c1c1 !important;
  color: #494848;
  font-weight: 600;
  font-size: 17px;
  letter-spacing: 1px;
  flex-direction: row;
  flex-wrap: wrap;
  align-content: center;
  align-items: center;
  justify-content: center;
  white-space: normal;
  word-break: break-all;
}

.el-menu.el-menu--horizontal {
  border-bottom: transparent;
  //  margin-left: 45px;
  //  margin-top: 40px;
  width: 95%;
  margin: 30px auto;
}

.el-menu-item.is-active {
  border: 1px solid #{'var(--theme)'} !important;
  color: #3b3d41;
}
.el-message {
  min-width: 580px;
}
ul.el-menu--horizontal.el-menu{
  background: transparent;
}
li.el-menu-item{
  background: rgb(255 255 255 / 60%);
  word-wrap: break-word;
  overflow-wrap: break-word;
}

_elMenu{
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-content: center;
  align-items: center;
}
</style>
