<template>
  <div class="home-page">
    <!-- Header -->
    <div class="header">
      <div class="logo">
        <img src="@/assets/images/home/logo@2x.png" alt="STORLEAD" />
      </div>
      <div class="user-info">
        <img src="@/assets/images/home/Group@2x.png" alt="User" class="user-avatar" />
        <span class="username">张小明</span>
      </div>
    </div>

    <!-- Main Content -->
    <div class="main-content">
      <div class="section" v-for="app in appList" :key="app.appId">
        <h2 class="section-title">{{ app.appName }}</h2>
        <div class="icon-grid">
          <div class="icon-item" v-for="page in app.appPages" :key="page.pageId" @click="handleClick(page)">
            <div class="icon-wrapper">
                            <img :src="getIconPath(page.pageIcon || page.appName)" :alt="page.appName" />

















            </div>
            <span class="icon-label">{{ page.appName }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="Index" lang="ts">
import { ref, onMounted } from 'vue';
import { getHomeApp, jumpToPage } from '@/api/app';

interface AppPage {
  pageId: number;
  appName: string;
}

interface AppInfo {
  appId: number;
  appName: string;
    pageIcon?: string;
  appPages: AppPage[];
}

const appList = ref<AppInfo[]>([]);

// Helper to get icon path
const getIconPath = (name: string | undefined) => {
    if (!name) {
        return '';
    }
    console.log('name', name);
    return new URL(`/src/assets/homeIcon/${name}@2x.png`, import.meta.url).href;
};

const handleClick = async (item: AppPage) => {
  console.log('Clicked:', item.appName, item.pageId);
  try {
    const res = await jumpToPage(item.pageId);
    if (res.result) {
        window.open(res.result, '_blank');
    }
  } catch (error) {
    console.error('Jump to page failed:', error);
  }
};

const initData = async () => {
  try {
    const res = await getHomeApp();
    if (res.result) {
      appList.value = res.result;
    }
  } catch (error) {
    console.error('Failed to get home app list:', error);
  }
};

onMounted(() => {
  initData();
});
</script>

<style scoped lang="scss">
.home-page {
  min-height: 100vh;
  background: linear-gradient(180deg, #F5F7FA 0%, #FFFFFF 100%);
  overflow-y: scroll;
}

.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 40px;
  background: linear-gradient(90deg, #1E88E5 0%, #42A5F5 100%);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

  .logo {
    img {
      height: 32px;
    }
  }

  .user-info {
    display: flex;
    align-items: center;
    gap: 12px;
    color: white;
    cursor: pointer;

    .user-avatar {
      width: 32px;
      height: 32px;
      border-radius: 50%;
      border: 2px solid rgba(255, 255, 255, 0.3);
    }

    .username {
      font-size: 14px;
      font-weight: 500;
    }
  }
}

.main-content {
  max-width: 1400px;
  margin: 0 auto;
  padding: 92px 20px 40px 20px; // 72px header height + 20px spacing
}

.section {
  margin-bottom: 48px;

  .section-title {
    font-size: 18px;
    font-weight: 600;
    color: #333;
    margin-bottom: 24px;
    padding-left: 12px;
    border-left: 4px solid #1E88E5;
  }

  .icon-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    gap: 32px 24px;
    background: white;
    padding: 32px;
    border-radius: 12px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);

    @media (max-width: 768px) {
      grid-template-columns: repeat(4, 1fr);
      gap: 24px 16px;
      padding: 24px;
    }

    @media (max-width: 480px) {
      grid-template-columns: repeat(3, 1fr);
      gap: 20px 12px;
      padding: 20px;
    }
  }

  .icon-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    cursor: pointer;
    transition: transform 0.2s ease;

    &:hover {
      transform: translateY(-4px);
    }

    .icon-wrapper {
      width: 64px;
      height: 64px;
      border-radius: 16px;
      display: flex;
      align-items: center;
      justify-content: center;
      transition: all 0.3s ease;

      img {
        width: 64px;
        height: 64px;
        object-fit: contain;
      }
    }

    .icon-label {
      font-size: 14px;
      color: #666;
      text-align: center;
      font-weight: 500;
      white-space: nowrap;
    }
  }
}
</style>
