<!-- 非递归渲染侧边栏菜单 license by http://eleadmin.com -->
<template>
  <a-menu
    :mode="mode"
    :theme="theme"
    :open-keys="openKeys"
    :selected-keys="selectedKeys"
    :inline-indent="inlineIndent"
    :inline-collapsed="inlineCollapsed"
    @openChange="onOpenChange">
    <template v-for="m1 in data">
      <template v-if="!m1.meta.hide">
        <a-sub-menu v-if="haveChildren(m1)" :key="m1.path" :popup-class-name="firstPopClass">
          <template v-if="m1.meta.icon" #icon>
            <component :is="m1.meta.icon"/>
          </template>
          <template #title>
            <span>{{ m1.meta.title }}</span>
          </template>
          <template v-for="m2 in m1.children">
            <template v-if="!m2.meta.hide">
              <a-sub-menu v-if="haveChildren(m2)" :key="m2.path">
                <template v-if="m2.meta.icon" #icon>
                  <component :is="m2.meta.icon"/>
                </template>
                <template #title>
                  <span>{{ m2.meta.title }}</span>
                </template>
                <template v-for="m3 in m2.children">
                  <template v-if="!m3.meta.hide">
                    <a-sub-menu v-if="haveChildren(m3)" :key="m3.path">
                      <template v-if="m3.meta.icon" #icon>
                        <component :is="m3.meta.icon"/>
                      </template>
                      <template #title>
                        <span>{{ m3.meta.title }}</span>
                      </template>
                      <template v-for="m4 in m3.children">
                        <template v-if="!m4.meta.hide">
                          <a-sub-menu v-if="haveChildren(m4)" :key="m4.path">
                            <template v-if="m4.meta.icon" #icon>
                              <component :is="m4.meta.icon"/>
                            </template>
                            <template #title>
                              <span>{{ m4.meta.title }}</span>
                            </template>
                            <template v-for="m5 in m4.children">
                              <template v-if="!m5.meta.hide">
                                <a-sub-menu v-if="haveChildren(m5)" :key="m5.path">
                                  <template v-if="m5.meta.icon" #icon>
                                    <component :is="m5.meta.icon"/>
                                  </template>
                                  <template #title>
                                    <span>{{ m5.meta.title }}</span>
                                  </template>
                                  <template v-for="m6 in m5.children">
                                    <template v-if="!m6.meta.hide">
                                      <a-sub-menu v-if="haveChildren(m6)" :key="m6.path">
                                        <template v-if="m6.meta.icon" #icon>
                                          <component :is="m6.meta.icon"/>
                                        </template>
                                        <template #title>
                                          <span>{{ m6.meta.title }}</span>
                                        </template>
                                        <template v-for="m7 in m6.children">
                                          <template v-if="!m7.meta.hide">
                                            <a-sub-menu v-if="haveChildren(m7)" :key="m7.path">
                                              <template v-if="m7.meta.icon" #icon>
                                                <component :is="m7.meta.icon"/>
                                              </template>
                                              <template #title>
                                                <span>{{ m7.meta.title }}</span>
                                              </template>
                                              <template v-for="m8 in m7.children">
                                                <a-menu-item v-if="!m8.meta.hide" :key="m8.path">
                                                  <template v-if="m8.meta.icon" #icon>
                                                    <component :is="m8.meta.icon"/>
                                                  </template>
                                                  <span>{{ m8.meta.title }}</span>
                                                  <a v-if="isUrl(m8)" :href="m8.path" target="_blank"></a>
                                                  <router-link v-else :to="m8.path"></router-link>
                                                </a-menu-item>
                                              </template>
                                            </a-sub-menu>
                                            <a-menu-item v-else :key="m7.path">
                                              <template v-if="m7.meta.icon" #icon>
                                                <component :is="m7.meta.icon"/>
                                              </template>
                                              <span>{{ m7.meta.title }}</span>
                                              <a v-if="isUrl(m7)" :href="m7.path" target="_blank"></a>
                                              <router-link v-else :to="m7.path"></router-link>
                                            </a-menu-item>
                                          </template>
                                        </template>
                                      </a-sub-menu>
                                      <a-menu-item v-else :key="m6.path">
                                        <template v-if="m6.meta.icon" #icon>
                                          <component :is="m6.meta.icon"/>
                                        </template>
                                        <span>{{ m6.meta.title }}</span>
                                        <a v-if="isUrl(m6)" :href="m6.path" target="_blank"></a>
                                        <router-link v-else :to="m6.path"></router-link>
                                      </a-menu-item>
                                    </template>
                                  </template>
                                </a-sub-menu>
                                <a-menu-item v-else :key="m5.path">
                                  <template v-if="m5.meta.icon" #icon>
                                    <component :is="m5.meta.icon"/>
                                  </template>
                                  <span>{{ m5.meta.title }}</span>
                                  <a v-if="isUrl(m5)" :href="m5.path" target="_blank"></a>
                                  <router-link v-else :to="m5.path"></router-link>
                                </a-menu-item>
                              </template>
                            </template>
                          </a-sub-menu>
                          <a-menu-item v-else :key="m4.path">
                            <template v-if="m4.meta.icon" #icon>
                              <component :is="m4.meta.icon"/>
                            </template>
                            <span>{{ m4.meta.title }}</span>
                            <a v-if="isUrl(m4)" :href="m4.path" target="_blank"></a>
                            <router-link v-else :to="m4.path"></router-link>
                          </a-menu-item>
                        </template>
                      </template>
                    </a-sub-menu>
                    <a-menu-item v-else :key="m3.path">
                      <template v-if="m3.meta.icon" #icon>
                        <component :is="m3.meta.icon"/>
                      </template>
                      <span>{{ m3.meta.title }}</span>
                      <a v-if="isUrl(m3)" :href="m3.path" target="_blank"></a>
                      <router-link v-else :to="m3.path"></router-link>
                    </a-menu-item>
                  </template>
                </template>
              </a-sub-menu>
              <a-menu-item v-else :key="m2.path">
                <template v-if="m2.meta.icon" #icon>
                  <component :is="m2.meta.icon"/>
                </template>
                <span>{{ m2.meta.title }}</span>
                <a v-if="isUrl(m2)" :href="m2.path" target="_blank"></a>
                <router-link v-else :to="m2.path"></router-link>
              </a-menu-item>
            </template>
          </template>
        </a-sub-menu>
        <a-menu-item v-else :key="m1.path">
          <template v-if="m1.meta.icon" #icon>
            <component :is="m1.meta.icon"/>
          </template>
          <span>{{ m1.meta.title }}</span>
          <a v-if="isUrl(m1)" :href="m1.path" target="_blank"></a>
          <router-link v-else :to="m1.path"></router-link>
        </a-menu-item>
      </template>
    </template>
  </a-menu>
</template>

<script>
import * as antIcons from '@ant-design/icons-vue';
import {isUrl} from '../../util.js';

export default {
  name: 'EleProSidebarMenus',
  components: {...antIcons},
  emits: ['openChange'],
  props: {
    // 菜单数据
    data: Array,
    // 导航模式
    mode: String,
    // 主题, light | dark
    theme: String,
    // 展开的key
    openKeys: Array,
    // 选中的key
    selectedKeys: Array,
    // 子级缩进
    inlineIndent: Number,
    // 是否折叠
    inlineCollapsed: Boolean,
    // 一级pop菜单class
    firstPopClass: String
  },
  methods: {
    /* 子级展开事件 */
    onOpenChange(e) {
      this.$emit('openChange', e);
    },
    /* 是否有子级 */
    haveChildren(item) {
      return !!(item.children && item.children.filter((d) => !d.meta.hide).length);
    },
    /* 是否是网址 */
    isUrl(item) {
      return isUrl(item.path);
    }
  }
}
</script>
