<script setup lang="jsx">
import { h, watch, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router';
import { NMenu, NScrollbar } from 'naive-ui'
import SvgIcon from '@/components/SvgIcon/index.vue'

const route = useRoute();
const router = useRouter();

const activeMenuKey = ref(route.path);

watch(() => route.path, currentPath => {
  activeMenuKey.value = currentPath;
})

function renderIcon (props) {
  return () => h(SvgIcon, { ...props })
}

function renderChildrenIcon ({ name, size = '20' }) {
  return () => (
    <div class="flex items-center justify-center">
      {renderIcon({ name, size, class: ['icon-default'] })()}
      {renderIcon({ name: name + '-active', size, class: ['icon-active', 'hidden'] })()}
    </div>
  )
}

function renderLabel (val, url) {
  return url ? (<a href={ url } target="_blank" class="pl-1">{ val }</a>) : (<span class="">{ val }</span>) ;
}

const menuOptions = [
  {
    label: () => renderLabel('数据报表分析'),
    icon: renderIcon({ name: 'menu-xiht-analyse' }),
    key: '/'
  },
  {
    label: () => renderLabel('智适应碳源投加'),
    icon: renderIcon({ name: 'menu-xiht-control' }),
    key: '/medicinal',
  },
  {
    label: () => renderLabel('用户中心'),
    icon: renderIcon({ name: 'menu-user' }),
    key: '/user',
  },
]

const handleUpdateValue = (key, { url }) => {
  if (url) return window.open(url);
  if (key.includes('normal')) return;
  router.push(key);
}

</script>

<template>
  <n-scrollbar style="height: 100%">
    <n-menu :options="menuOptions" :icon-size="30" v-model:value="activeMenuKey" @update:value="handleUpdateValue">
    </n-menu>
  </n-scrollbar>
</template>

<style scoped lang="scss"></style>

<style lang="scss">
@mixin sub-menu-active {
  .n-menu-item-content__icon {
    .icon-default {
      display: none;
    }

    .icon-active {
      display: block;
    }
  }

  .n-menu-item-content-header {
    font-weight: bold;
  }

}

.n-menu .n-menu-item-content:not(.n-menu-item-content--disabled):hover::before {
  border: 1px solid #fff;
  background: #FCFDFE;
}

// 子菜单hover
.n-submenu-children {
  .n-menu-item-content:hover {
    @include sub-menu-active();
  }
}

// reset - 选中状态
.n-menu-item-content--selected {
  @include sub-menu-active();

  &:not(.n-menu-item-content--disabled)::before {
    border: 1px solid #fff;
    background: #FCFDFE;
  }
}

// reset - 选中状态
// .n-menu-item-content--selected {
//   font-weight: bold;
// }
// .n-base-icon {
//   background: url('@/assets/svgs/menu/arrow.svg') center center no-repeat;
//   svg {
//     display: none;
//   }
// }
</style>