<template>
  <div class="navigation">
    <div 
      v-for="(group, index) in sortedGroups" 
      :key="group.name"
      class="group-container"
      :style="{ animationDelay: `${index * 0.1}s` }"
    >
      <div class="group-header">
        <h2 class="group-name">{{ group.name }}</h2>
        <p v-if="group.description" class="group-description">{{ group.description }}</p>
      </div>
      
      <div class="links-grid">
        <a
          v-for="(link, linkIndex) in sortedLinks(group.links)"
          :key="link.name"
          :href="link.autoLogin ? getAutoLoginUrl(link.autoLoginEndpoint) : (link.url.startsWith('http') ? link.url : `http://${link.url}`)"
          target="_blank"
          rel="noopener noreferrer"
          class="link-card"
          :style="{ animationDelay: `${(index * 0.1) + (linkIndex * 0.05)}s` }"
          @click="handleLinkClick($event, link)"
        >
          <div class="card-header">
            <div class="link-icon-wrapper">
              <img 
                v-if="link.icon" 
                :src="link.icon" 
                :alt="link.name"
                class="link-icon"
                @error="handleImageError"
              />
            </div>
            <div class="link-name">{{ link.name }}</div>
          </div>
          <p v-if="link.description" class="link-description">{{ link.description }}</p>
          <div class="link-footer">
            <span class="link-arrow">访问 →</span>
          </div>
        </a>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import navigationData from '../data/navigation.json'
import { getAutoLoginUrl } from '../config/backend.js'

const groups = ref([])

onMounted(() => {
  groups.value = navigationData.groups || []
})

const sortedGroups = computed(() => {
  return [...groups.value].sort((a, b) => (a.sort || 0) - (b.sort || 0))
})

const sortedLinks = (links) => {
  if (!links) return []
  return [...links].sort((a, b) => (a.sort || 0) - (b.sort || 0))
}

const handleImageError = (event) => {
  event.target.style.display = 'none'
}

const handleLinkClick = (event, link) => {
  if (link.autoLogin) {
    console.log('自动登录链接:', link.name);
  }
}
</script>

<style scoped>
.navigation {
  display: flex;
  flex-direction: column;
  gap: 40px;
}

.group-container {
  background: rgba(255, 255, 255, 0.98);
  border-radius: 16px;
  padding: 35px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  animation: fadeIn 0.5s ease-out both;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.group-header {
  margin-bottom: 25px;
  padding-bottom: 20px;
  border-bottom: 2px solid #e2e8f0;
}

.group-name {
  font-size: 32px;
  color: #1a202c;
  margin-bottom: 8px;
  font-weight: 600;
}

.group-description {
  font-size: 18px;
  color: #718096;
  font-weight: 400;
}

.links-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

.link-card {
  display: flex;
  flex-direction: column;
  background: #ffffff;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  padding: 25px;
  text-decoration: none;
  color: inherit;
  transition: all 0.3s ease;
  animation: fadeIn 0.5s ease-out both;
  min-height: 160px;
}

.link-card:hover {
  transform: translateY(-4px);
  border-color: #667eea;
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.2);
}

.card-header {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-bottom: 15px;
}

.link-icon-wrapper {
  width: 50px;
  height: 50px;
  border-radius: 10px;
  background: #f7fafc;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
  flex-shrink: 0;
}

.link-icon {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.link-name {
  font-size: 22px;
  color: #2d3748;
  font-weight: 600;
  flex: 1;
}

.link-description {
  font-size: 16px;
  color: #718096;
  line-height: 1.5;
  margin-bottom: 15px;
  flex: 1;
}

.link-footer {
  margin-top: auto;
  padding-top: 15px;
  border-top: 1px solid #e2e8f0;
}

.link-arrow {
  font-size: 14px;
  color: #667eea;
  font-weight: 500;
  transition: transform 0.3s ease;
}

.link-card:hover .link-arrow {
  transform: translateX(5px);
}

@media (max-width: 768px) {
  .links-grid {
    grid-template-columns: 1fr;
  }
  
  .group-container {
    padding: 25px 20px;
  }
  
  .group-name {
    font-size: 28px;
  }
  
  .link-card {
    min-height: 140px;
    padding: 20px;
  }
}
</style>

