<template>
  <scroll-bar class="side-bar">
    <ul class="nav-list">
      <li class="nav-item" v-for="(nav, navNo) in menus" :key="navNo">
        <p class="nav-item-content">
          <i :class="['icon', `${nav.icon}`]"></i>
          <span>{{ nav.title }}</span>
          <span></span>
        </p>
        <ul class="nav-sublist">
          <li
            v-for="(subnav, subNo) in nav.children"
            :key="subNo"
            :class="[
              'nav-sub-item',
              { 'nav-item-act': subnav.name === curRouterName },
            ]"
            @click="toRouter(subnav)"
          >
            <p>
              <span>{{ subnav.title }}</span>
              <span
                v-if="navHasTips(subnav) && navTips[subnav.name].val"
                :class="['nav-item-tips', navTips[subnav.name].classes]"
                >{{ navTips[subnav.name].val }}</span
              >
            </p>
          </li>
        </ul>
      </li>
    </ul>
  </scroll-bar>
</template>

<script>
import ScrollBar from "./ScrollBar";
import { mapState } from "vuex";

export default {
  name: "SideBar",
  components: { ScrollBar },
  props: {
    menus: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    return {};
  },
  computed: {
    ...mapState("invigilation", ["navTips"]),
    curRouterName() {
      return this.$route.meta.relate || this.$route.name;
    },
  },
  methods: {
    toRouter(nav) {
      // this.curRouterName = nav.name;
      this.$router.push({ name: nav.name });
    },
    navHasTips(nav) {
      return Object.keys(this.navTips).includes(nav.name);
    },
  },
};
</script>

<style lang="scss" scoped>
.side-bar {
  line-height: 20px;
  font-size: 14px;
  p {
    margin: 0;
  }
  li {
    list-style: none;
  }
}
.nav-list {
  padding: 30px;
  margin: 0;
}
.nav-item {
  margin-bottom: 24px;
  font-weight: 400;

  &-content {
    padding: 6px 0;
    margin: 0;
    color: #202b4b;
    font-weight: 600;

    > * {
      display: inline-block;
      vertical-align: middle;
    }
    > i {
      margin-right: 10px;
    }
  }
}
.nav-sublist {
  padding-left: 26px;
  color: #626a82;
  .nav-sub-item {
    padding: 6px 0;
    font-weight: 400;
    cursor: pointer;

    &:hover {
      color: #1886fe;
    }
  }

  .nav-item-act {
    color: #1886fe;
    font-weight: 600;
  }
  .nav-sub-item {
    span {
      display: inline-block;
      vertical-align: middle;
    }

    span.nav-item-tips {
      height: 18px;
      line-height: 18px;
      padding: 0 6px;
      color: #fff;
      font-size: 12px;
      border-radius: 3px;
      margin-left: 10px;
    }

    span.nav-item-tips-num {
      background: #5fc9fa;
    }
    span.nav-item-tips-popover {
      background: #ff9f43;
      position: relative;
      margin-left: 15px;

      &::before {
        content: "";
        display: block;
        position: absolute;
        left: -5px;
        top: 50%;
        margin-top: -4px;
        width: 0;
        height: 0;
        border-style: solid;
        border-width: 4px;
        border-color: transparent #ff9f43 #ff9f43 transparent;
      }
    }
  }
}
</style>
