<template>
  <div class="ToolBar">
    <div class="ToolBar-Head">
      <div class="flex-wrap">
        <div
          v-for="(item, index) in dataArr"
          @click="routeLink(item, index)"
          :class="dialog == index ? 'toolbar-active' : ''"
          :key="index"
          class="toolbar-list"
        >
          <div class="flex-wrap toolbar-list-bar">
            <!-- <div class="toolbar-list-img">
              <img :src="item.src" :alt="item.meta.title" />
            </div> -->
            <img
              style="width: 18px; height: 18px"
              :src="dialog == index ? item.srcer : item.src"
              :alt="item.meta.title"
            />
            <div class="toolbar-list-name">{{ item.meta.title }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="ToolBar-Content">
      <router-view />
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  name: "ToolBar",
  data() {
    return {
      dataArr: [],
      flag: false,
    };
  },
  computed: {
    ...mapGetters(["dialog", "permission_routes"]),
  },
  created() {
    const path = this.$route.path;
    const pathArr = path.split("/");
    if (pathArr.length > 2) {
      //子路由
      const name = pathArr[pathArr.length - 2];
      const childName = pathArr[pathArr.length - 1];
      this.getRoutes(name, childName);
    } else {
      //一级路由
      const name = pathArr[pathArr.length - 1];
      this.getRoutes(name);
      this.$store.dispatch("app/toggleDialog", 0);
    }
  },
  methods: {
    // 子路由跳转
    routeLink(item, index) {
      this.$store.dispatch("app/toggleDialog", index);
      this.$router.push(item.path);
    },
    // 获取路由数据
    getRoutes(name, childName = "") {
      const routes = this.permission_routes;
      const datas = routes.filter(
        (item) => item.path === "/" + name || item.redirect === "/" + name
      );
      const dataArr = datas[0].children[0].children;
      const arrs = dataArr.filter((item) => item.hidden);
      if (childName) {
        const isAloneData = dataArr.filter((item) => item.name === childName);
        if (isAloneData.length > 0) {
          this.dataArr = arrs;
        }
      } else {
        this.dataArr = arrs;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.ToolBar {
  .toolbar-list {
    margin-right: 24px;
    cursor: pointer;
    background: #ffffff;
    box-shadow: 0px 6px 7px 0px rgba(0, 0, 0, 0.06);
    border-radius: 4px;
    min-width: 220px;
    .toolbar-list-bar {
      display: flex;
      align-items: center;
      padding: 6px 32px;
      padding-left: 24px;
      line-height: 40px;
    }
    .toolbar-list-img {
      width: 20px;
      height: 20px;
      margin-right: 16px;
      img {
        margin-right: 16px;
        // max-height: 40px;
        width: 100%;
        height: 100%;
      }
    }
    img {
      margin-right: 16px;
      // max-height: 40px;
      // width: 100%;
      // height: 100%;
    }
    .toolbar-list-name {
      font-size: 16px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      color: #303133;
    }
    &:last-child {
      margin-right: 0;
    }
  }
  .toolbar-active {
    background: #2d67e3;
    .toolbar-list-name {
      color: #ffffff;
    }
  }
}
</style>