<template>
  <div class="sidderBg">
    <div v-if="collectionsData.length > 0">
      <div
        v-for="elem in collectionsData"
        :key="elem.id"
        flex="cross:center"
        :style="{
          color: settings.sideTheme === 'theme-dark' ? '#fff' : '#4c4c4c',
        }"
        class="newsSider"
        :class="{ active: elem.id === activeIndex }"
        @mouseenter="openCollectChildren(elem)"
      >
        <i class="newsSider-icon el-icon-star-off"></i>
        <p class="newsSider-title" :class="{ active: elem.id === activeIndex }">
          {{ elem.meta.title }}
        </p>
      </div>
    </div>
    <div v-for="(item, index) in sidebarRouters" :key="index">
      <div
        v-if="!item.hidden && item.meta"
        flex="cross:center"
        :class="{ active: index === activeIndex }"
        :style="{
          color: settings.sideTheme === 'theme-dark' ? '#fff' : '#4c4c4c',
        }"
        class="newsSider"
        @mouseenter="openChildren(item, index)"
        @click="goRouter(item, index)"
      >
        <i class="newsSider-icon iconfont" :class="item.meta.icon"> </i>
        <p class="newsSider-title" :class="{ active: index === activeIndex }">
          {{ item.meta.title }}
        </p>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters, mapState } from "vuex";
export default {
  props: {
    collectionsData: {
      type: Array,
      required: [],
    },
    sidebarRouters: {
      type: Array,
      required: [],
    },
    routePath: {
      type: String,
      required: "",
    },
  },
  computed: {
    ...mapState(["settings", "sidebar"]),
  },
  data() {
    return {
      activeIndex: null,
    };
  },
  mounted() {
    this.activeIndex = null;
  },
  methods: {
    goRouter(item, index) {
      if (item.children.length <= 1) {
        this.$router.push({ path: `${item.path}/${item.children[0].path}` });
      }
    },
    // 打开弹窗
    openChildren(item, index) {
      if (item.children.length <= 1) {
        this.$emit("closeWin");
        return;
      }
      if (!item.children) {
        this.$store.dispatch("app/changeChildrenHide", false);
      } else {
        // this.$emit("openChildren", item);
      }
      this.$emit("openChildren", item);
      this.activeIndex = index;
    },
    openCollectChildren(data) {
      this.activeIndex = data.id;
      this.$emit("openChildren", data);
    },
  },
};
</script>

<style scoped lang="scss">
$activeColor: var(--bg-color, "#00aaff");

.sidderBg {
  padding-top: 20px;
  width: 128px;
  overflow-y: auto;
  height: 100%;
  // border-right: 2px solid #d6d1d169;
}

::v-deep .newsSider {
  -webkit-user-select: none;
  -moz-user-select: none;
  user-select: none;
  -ms-user-select: none;
  height: 50px;
  line-height: 50px;
  cursor: pointer;

  &-icon {
    font-size: 16px;
    padding: 0 15px;
    text-align: center;
  }

  &-title {
    font-size: 15px;
  }
}

::-webkit-scrollbar {
  width: 0 !important;
}

::-webkit-scrollbar {
  width: 0 !important;
  height: 0;
}

::v-deep .newsSider-icon {
  background: none;
  // color: #147af8;
}

.newsSider:hover .newsSider-icon {
  color: #fff;
}

.newsSider:hover .newsSider-title {
  color: #fff;
}

.newsSider.active .newsSider-icon {
  color: #fff;
}

.newsSider.active .newsSider-title {
  color: #fff;
}

::v-deep .newsSider:hover {
  background-color: $activeColor;
  color: #fff;

  &-icon:hover {
    color: #fff;
  }
}

::v-deep.newsSider.active {
  background-color: $activeColor;
  color: #fff;
}

::v-deep.newsSider + .newsSider {
  border-top: 1px solid #747474;
}
</style>
