<template>
  <template v-for="subItem in menuList" :key="subItem.path">
    <el-sub-menu v-if="subItem.children?.length" :index="subItem.path">
      <template #title>
        <img
          v-if="subItem.meta.icon"
          class="img-icons"
          :src="getCustomIcon(isActive(subItem) ? subItem.meta.iconActive : subItem.meta.icon)"
        />
        <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.png";
import alarmActive from "@/assets/images/alarm-active.png";
import analysis from "@/assets/images/analysis.png";
import analysisActive from "@/assets/images/analysis-active.png";
import query from "@/assets/images/query.png";
import queryActive from "@/assets/images/query-active.png";
import system from "@/assets/images/system.png";
import systemActive from "@/assets/images/system-active.png";
import maintenance from "@/assets/images/maintenance.png";
import maintenanceActive from "@/assets/images/maintenance-active.png";

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,
    alarmActive: alarmActive,
    analysis: analysis,
    analysisActive: analysisActive,
    query: query,
    queryActive: queryActive,
    system: system,
    systemActive: systemActive,
    maintenance: maintenance,
    maintenanceActive: maintenanceActive
  };
  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 {
  width: 24px;
  height: 24px;
  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;
      }
    }
  }
}
</style>
