<template>
  <template v-for="subItem in menuList" :key="subItem.path">
    <el-sub-menu v-if="subItem.children?.length" :index="subItem.path">
      <template #title>
        <img class="img-icons"
             v-if="subItem.meta.icon"
             :src="getCustomIcon(subItem.meta.icon)"
             alt="SVG Image"
             :class="isActive(subItem)?'menu-img':'menu-img2'"/>
        <span class="sle" :class="isActive(subItem) ? 'menu-title-active' : 'menu-title'">{{ subItem.meta.title }}</span>
      </template>
      <SubMenu :menu-list="subItem.children" />
    </el-sub-menu>
    <el-menu-item v-else :index="subItem.path" @click="handleClickMenu(subItem)">
      <el-icon v-if="subItem.meta.icon">
        <img
          v-if="subItem.meta.icon"
          class="img-icons"
          :src="getCustomIcon(isActive(subItem) ? subItem.meta.iconActive : subItem.meta.icon)"
        />
      </el-icon>
      <template #title>
        <span class="sle">{{ subItem.meta.title }}</span>
      </template>
    </el-menu-item>
  </template>
</template>

<script setup lang="ts">
import { useRouter } from "vue-router";

import alarm from "@/assets/images/alarm.svg"
import analysis from "@/assets/images/analysis.svg";
import query from "@/assets/images/query.svg";
import system from "@/assets/images/system.svg";
import maintenance from "@/assets/images/maintenance.svg";

defineProps<{ menuList: Menu.MenuOptions[] }>();

const router = useRouter();
const handleClickMenu = (subItem: Menu.MenuOptions) => {
  if (subItem.meta.isLink) return window.open(subItem.meta.isLink, "_blank");
  router.push(subItem.path);
};

const isActive = (subItem: Menu.MenuOptions) => {
  const currentPath = router.currentRoute.value.fullPath;
  return subItem.path === currentPath || subItem.children?.some(child => child.path === currentPath);
};

const getCustomIcon = (iconName: string) => {
  const icons = {
    alarm: alarm,
    analysis: analysis,
    query: query,
    system: system,
    maintenance: maintenance
  };
  return icons[iconName] || null;
};
</script>

<style lang="scss">
.el-sub-menu .el-sub-menu__title:hover {
  color: var(--el-menu-hover-text-color) !important;
  background-color: transparent !important;
}
.menu-title-active {
  color: var(--el-menu-active-color) !important;
}

.menu-title {
  color: var(--el-menu-hover-text-color) !important;
}

.img-icons {
  margin-right: 5px;
}

.el-menu--collapse {
  .is-active {
    .el-sub-menu__title {
      color: #ffffff !important;
      //background-color: var(--el-color-primary) !important;
    }
  }
}
.el-menu-item {
  &:hover {
    color: var(--el-menu-hover-text-color);
  }
  &.is-active {
    color: var(--el-menu-active-color) !important;
    background-color: var(--el-menu-active-bg-color) !important;
    &::before {
      position: absolute;
      top: 0;
      bottom: 0;
      width: 4px;
      content: "";
      background-color: var(--el-color-primary);
    }
  }
}
.vertical,
.classic,
.transverse {
  .el-menu-item {
    &.is-active {
      &::before {
        left: 0;
      }
    }
  }
}
.columns {
  .el-menu-item {
    &.is-active {
      &::before {
        right: 0;
      }
    }
  }
}

.menu-img{
  filter: drop-shadow(100px 0px 0px var(--el-menu-active-color));
  transform: translateX(-100px);
}

.menu-img2{
  filter: drop-shadow(100px 0px 0px var(--el-menu-hover-text-color));
  transform: translateX(-100px);
}
</style>
