<template>
  <div class="app-container">
    <div class="card">
      <div class="row">
        <div class="title">
          {{ newMenus[menusActive].name }}
        </div>
        <div class="menus_wrap">
          <div class="item" v-for="(val, index) in newMenus[menusActive].childrenList" :key="index" @click="to(val)">
            <img :src="getIconPath(val.icon)" class="icon" @error="handleImageError(val)" />
            <div class="info">
              <div class="name">{{ val.name }}</div>
              <div class="intro">
                {{ val.intro }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { newMenus } from './data.js'

const menusActive = ref(0)

const route = useRoute()
const router = useRouter();
const to = (item) => {
  if (!item.pathName) {
    ElMessage.warning("暂未开放");
    return;
  }
  router.push({
    name: item.pathName,
  });
};

// 动态获取PNG图标路径
const getIconPath = (iconName) => {
  try {
    // 直接导入对应PNG文件
    return new URL(`/src/assets/applocation/${iconName}.png`, import.meta.url).href;
  } catch (error) {
    console.warn(`图标 ${iconName}.png 不存在`);
    return defaultIcon; // 图标不存在时使用默认图标
  }
};

// 动态获取PNG图标路径
const getIconPathNew = (iconName) => {
  try {
    // 直接导入对应PNG文件
    return new URL(`/src/assets/marketing/${iconName}.png`, import.meta.url).href;
  } catch (error) {
    console.warn(`图标 ${iconName}.png 不存在`);
    return defaultIcon; // 图标不存在时使用默认图标
  }
};

// 处理图片加载失败
const handleImageError = (item) => {
  console.error(`图标 ${item.icon}.png 加载失败`);
};

onMounted(() => {
  menusActive.value = +route.query.index
})
</script>

<style scoped lang="scss">
.app-container {
  padding: 14px;

  .card {
    height: 81vh;
    overflow-y: auto;
    padding: 14px;
    background-color: #fff;
    border-radius: 10px;
    position: relative;
    overflow: hidden;

    .children_wrap {
      width: 100%;
      height: 81vh;
      position: absolute;
      top: 0;
      left: 0;
      z-index: 99;
      background-color: rgba(255, 255, 255, 0.8);
      backdrop-filter: blur(20px);
      padding: 14px;
      border-radius: 10px;
      transition: all .3s ease-in-out;
      transform: translateY(200%);

      &.active {
        transform: translateY(0);
      }

      .close {
        width: 30px;
        height: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        position: absolute;
        top: 10px;
        right: 10px;

        &:hover {
          cursor: pointer;
        }
      }
    }

    .row {
      &:not(:first-child) {
        padding-bottom: 14px;
      }

      .title {
        font-size: 16px;
      }

      .menus_wrap {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 14px;
        margin-top: 14px;

        .item {
          display: flex;
          align-items: center;
          padding: 10px;
          background-color: #f8f8f8;
          border-radius: 4px;
          transition: all 0.1s ease-in-out;

          &:hover {
            cursor: pointer;
            background-color: #d5ebff;
          }

          .icon {
            width: 48px;
            height: 48px;
            margin-right: 10px;
          }

          .info {
            padding-left: 10px;

            .name {
              font-size: 14px;
            }

            .intro {
              margin-top: 4px;
              font-size: 14px;
              color: #666;
              line-height: 1.4em;
              overflow: hidden;
              text-overflow: ellipsis;
              display: -webkit-box;
              -webkit-line-clamp: 1;
              line-clamp: 1;
              -webkit-box-orient: vertical;
              word-break: break-all;
              white-space: normal;
            }
          }
        }
      }
    }
  }
}
</style>
