<!--
 * @Author: Billy
 * @Date: 2021-12-18 16:30:05
 * @LastEditors: Billy
 * @LastEditTime: 2022-01-07 17:16:09
 * @Description: 请输入
-->
<!--
props: 看下面注释
event: 
1.select index: 选中菜单项的index, item: 选中菜单项的MenuItem类型对象
-->

<template>
  <div class="menu" :class="arrangeMode">
    <div class="menu-item-area" v-for="(item, index) in menuItems" :key="index">
      <div
        class="menu-item"
        :class="[index === activeIndex ? 'active' : '']"
        @click="onItemClick(item, index)"
      >
        <el-tooltip
          v-if="displayMode === 'icon'"
          class="text-tip"
          effect="light"
          :content="item.title"
          :open-delay="750"
          :placement="arrangeMode === 'horizontal' ? 'bottom' : 'right'"
        >
          <i v-if="iconMode === 'elementUi'" :class="item.iconClass"></i>
          <img v-if="iconMode === 'img'" class="icon-img" :src="item.iconSrc" />
        </el-tooltip>

        <i
          v-if="iconMode === 'elementUi'"
          class="icon-ele"
          :class="item.iconClass"
        ></i>
        <img
          v-if="iconMode === 'img' && displayMode === 'both'"
          class="icon-img"
          :src="item.iconSrc"
        />
        <span v-if="displayMode !== 'icon'">{{ item.title }}</span>
      </div>
      <div class="sub-items" v-if="item.children && item.children.length">
        <div
          class="sub-item"
          v-for="(item, index) in item.children"
          :key="index"
        >
          {{ item.title }}
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    // 主菜单栏的信息数组
    menuItems: {
      type: Array,
      default: function () {
        return [];
      },
    },
    // 排列模式，横向还是竖向
    arrangeMode: {
      type: String,
      default: "horizontal",
      validator: function (value) {
        return ["horizontal", "vertical"].indexOf(value) !== -1;
      },
    },
    // 显示模式，只显示文字、只显示图标，还是都显示
    displayMode: {
      type: String,
      default: "both",
      validator: function (value) {
        return ["text", "icon", "both"].indexOf(value) !== -1;
      },
    },
    // 图标来源，elementUi自带的图标、png jpg等格式图片、svg图标(暂不支持)
    iconMode: {
      type: String,
      default: "elementUi",
      validator: function (value) {
        return ["elementUi", "img", "svg"].indexOf(value) !== -1;
      },
    },
    // 当前激活菜单的 index (支持.sync)
    defaultActive: { type: Number, default: 0 },
  },
  computed: {
    activeIndex: {
      get() {
        return this.activeIndex_;
      },
      set(val) {
        // this.$emit("update:defaultActive", val); // 暂时用不上
        this.activeIndex_ = val;
      },
    },
  },
  watch: {
    defaultActive: function (val, oldVal) {
      this.activeIndex_ = val;
    },
  },
  data() {
    return {
      activeIndex_: this.defaultActive,
    };
  },
  mounted() {},
  methods: {
    onItemClick(item, index) {
      this.activeIndex = index;
      this.$emit("select", item, index);
    },
  },
};
</script>
<style lang="scss" scoped>
@import "~@/scss/_variables";
.menu {
  height: 100%;
  display: flex;
  justify-content: flex-start;
  align-items: stretch;
  &.horizontal {
    flex-direction: row;
    .menu-item-area {
      .menu-item {
        flex-direction: row;
        border-bottom: solid 2px transparent;
        .icon-img {
          padding-right: 4px;
        }
      }
    }
  }
  &.vertical {
    flex-direction: column;
    .menu-item-area {
      .menu-item {
        flex-direction: row;
        justify-content: flex-start;
        // padding-left: 48px;
        // border-right: solid 2px transparent;
        .icon-img,
        .icon-ele {
          padding-right: 8px;
        }
      }
    }
  }
  .menu-item-area {
    .menu-item {
      display: flex;
      // justify-content: flex-start;
      align-items: center;
      cursor: pointer;
      color: $font-color-dark;
      padding: 8px;
      margin: 8px;
      border-radius: 4px;
      transition: border-color 0.3s, background-color 0.3s, color 0.3s;

      &.active {
        // border-bottom-color: $theme-main-color;
        // border-right-color: $theme-main-color;
        background-color: $theme-main-color;
        color: #fff;
      }
      .icon-img {
        width: 24px;
        height: 24px;
      }
    }
    .sub-items {
      .sub-item {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        cursor: pointer;
        padding: 8px;
        padding-left: 48px;
        margin: 8px;
      }
    }
  }
}
</style>