<template>
  <div class="menu-list-main" :class="[p_level > 2 ? 'right' : 'bottom']">
    <div class="menu-list" v-if="children && children.length > 0 && p_level <= 3">
      <div
        class="menu-item"
        :class="{ hasChildren: item.children && item.children.length > 0 }"
        @click.stop="childMenuAction(item)"
        v-show="item.menuType !== 2"
        v-for="item in children"
        :key="item.id">
        <div :title="item.menuName">
          <span>{{ item.menuName }}</span>
          <div class="menu-icon" v-if="item.children && item.children.length > 0 && item.level <= 3">
            <bg-icon style="font-size: 10px" v-if="item.menuType == 0" icon="#bg-ic-arrow-right" />
          </div>
        </div>
        <menu-list
          v-if="children && children.length > 0"
          :p_level="item.level"
          :children="item.children"
          :parent="item">
        </menu-list>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "menuList", //给组件命名
};
</script>
<script setup>
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
const router = useRouter();
const props = defineProps({
  parent: {
    type: Object,
    default: () => ({}),
  },
  children: {
    type: Array,
    default: () => [],
  },
  p_level: {
    type: Number,
    default: 0,
  },
});
const childMenuAction = (v) => {
  if (v.menuType !== 1 && v.children && v.children.length) {
    for (let index = 0; index < v.children.length; index++) {
      const e = v.children[index];
      if (e.menuType !== 2) {
        pathToUrl(e);
        break;
      }
    }
  } else {
    pathToUrl(v);
  }
};

const pathToUrl = (item) => {
  //判断是否是新窗口打开
  if (item.new_window) {
    window.open(item.sourcePath ? item.sourcePath : item.path, "_blank");
  } else {
    // 当前窗口打开
    if (item.path.indexOf("#") > -1) {
      // 携带#说明是其它项目，使用window.location.href
      window.location.href = item.path;
    } else {
      // 不携带#说明是本项目，使用router.push
      router.push(item.path);
    }
  }
};
</script>

<style lang="scss" scoped>
.menu-list-main {
  height: auto;
  position: absolute;
  z-index: 9;
  display: none;
  &.right {
    width: auto;
    left: 100%;
    padding-left: 5px;
    top: -4px;
    > .menu-list {
      width: auto !important;
    }
  }
  &.bottom {
    min-width: 100%;
    width: auto;
    padding-top: 5px;
    top: 100%;
    left: 0;
    > .menu-list {
      width: 100%;
    }
  }
}
.menu-list {
  box-shadow: 0 4px 12px 0 rgba(18, 30, 63, 0.1);
  background-color: #ffffff;
  border-radius: 4px;
  padding: 4px 0;
  //   z-index: 9;
  text-align: center;

  .menu-item {
    width: auto;
    height: 34px;
    font-size: 14px;
    line-height: 34px;
    padding: 0 16px;
    background-color: #fff;
    color: #202531;
    text-align: left;
    white-space: nowrap;
    position: relative;
    &.hasChildren {
      padding: 0 34px 0 16px;
    }
    .menu-icon {
      width: 34px;
      height: 34px;
      display: flex;
      align-items: center;
      justify-content: center;
      position: absolute;
      right: 0;
      top: 0;
      transform: rotate(0);
      transition: all 300ms;
    }
    &:hover {
      background-color: #eff2fa;
      > .menu-list-main {
        display: block;
      }
      > div > .menu-icon {
        transform: rotate(180deg);
      }
    }

    .width-num {
      display: inline-block;
      vertical-align: middle;
      min-width: 30px;
      background-color: #ff6a00;
      font-size: 10px;
      line-height: 16px;
      color: #fff;
      padding: 0 8px;
      margin-left: 5px;
      box-sizing: border-box;
      border-radius: 8px;
      text-align: center;
      overflow: hidden;
    }
  }

  .menu-item {
    cursor: pointer;
  }
}
</style>
