<template>
  <component :is="menuComponent" :item="item" :index="item.id" :level="level">
    <template v-if="item.menuType === 'category'" slot="title">
      <div
        class="title-container"
        :style="{ 'margin-left': (level - 1) * 15 + 'px' }"
      >
        <i v-show="item.icon" class="ht-fa-icon" :class="item.icon"></i>
        <span
          class="sub-menu-title"
          :class="item.parentId === '0' ? '' : 'second-level-menu'"
        >
          {{ item.name }}
        </span>
      </div>
    </template>
    <template v-else>
      <div
        class="title-container"
        :style="{ 'margin-left': (level - 1) * 15 + 'px' }"
      >
        <i
          v-show="item.icon && level === 1"
          class="ht-fa-icon"
          :class="item.icon"
        ></i>
        <span
          slot="title"
          class="sub-menu-title"
          :class="item.parentId === '0' ? '' : 'second-level-menu'"
        >
          {{ item.name }}
        </span>
      </div>
    </template>
    <template
      v-if="
        item.children && item.children.length && item.menuType === 'category'
      "
    >
      <sidebar-item
        v-for="route in item.children"
        :key="route.id"
        :item="route"
        :level="level + 1"
      />
    </template>
  </component>
</template>

<script>
  import SidebarItem from './SidebarItem'

  export default {
    name: 'SidebarItem',
    components: { SidebarItem },
    props: {
      item: {
        type: Object,
        required: true,
      },
      index: {
        type: String,
        default: '',
      },
      level: {
        type: Number,
        default: 1,
      },
    },
    computed: {
      menuComponent() {
        if (this.item.menuType === 'category') {
          return 'ElSubmenu'
        } else {
          return 'ElMenuItem'
        }
      },
    },
  }
</script>

<style lang="scss" scoped>
  ::v-deep {
    .el-tooltip {
      padding-left: 28px !important;
      display: flex !important;
      align-items: center !important;
    }
    .el-icon-arrow-down {
      transform: rotate(-90deg);
      color: var(--menuColor);
    }
  }

  .second-level-menu {
    font-size: 14px;
  }
  .ht-fa-icon {
    width: 18px;
    height: 18px;
  }
  .title-container {
    display: flex;
    align-items: center;
  }
</style>
