<template>
  <div class="min-h-full py-16 bg-gray-50">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
      <h1 class="text-4xl sm:text-5xl font-display font-bold text-gray-900 mb-6">
        Trainingszeiten
      </h1>
      <div class="w-24 h-1 bg-primary-600 mb-8" />
      
      <!-- Trainingsort -->
      <div v-if="config" class="bg-white rounded-xl shadow-lg p-8 mb-12">
        <div class="flex items-start space-x-4 mb-6">
          <MapPin :size="32" class="text-primary-600 flex-shrink-0" />
          <div>
            <h2 class="text-2xl font-display font-bold text-gray-900 mb-4">Trainingsort</h2>
            <h3 class="text-lg font-semibold text-gray-900 mb-2">
              {{ config.training.ort.name }}
            </h3>
            <p class="text-gray-700 mb-1">{{ config.training.ort.strasse }}</p>
            <p class="text-gray-700 mb-4">{{ config.training.ort.plz }} {{ config.training.ort.ort }}</p>
            <a
              :href="`https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(config.training.ort.strasse + ' ' + config.training.ort.plz + ' ' + config.training.ort.ort)}`"
              target="_blank"
              class="inline-flex items-center px-4 py-2 bg-primary-600 hover:bg-primary-700 text-white font-medium rounded-lg transition-colors text-sm"
            >
              <MapPin :size="16" class="mr-2" />
              Anfahrtsplan anzeigen
            </a>
          </div>
        </div>
      </div>

      <!-- Trainingszeiten -->
      <h2 class="text-2xl font-display font-bold text-gray-900 mb-6">
        Trainingszeiten
      </h2>

      <div v-if="config" class="grid gap-6 mb-12">
        <div
          v-for="(zeiten, gruppe) in groupedZeiten"
          :key="gruppe"
          class="bg-white p-6 rounded-xl shadow-lg border-l-4 border-primary-600"
        >
          <div class="flex items-start justify-between">
            <div>
              <h3 class="text-xl font-display font-bold text-gray-900 mb-2">{{ gruppe }}</h3>
              <div class="space-y-2">
                <p
                  v-for="zeit in zeiten"
                  :key="zeit.id"
                  class="text-lg font-semibold text-primary-600"
                >
                  {{ zeit.tag }}: {{ zeit.von }} - {{ zeit.bis }} Uhr
                </p>
              </div>
            </div>
            <Clock :size="32" class="text-primary-600" />
          </div>
        </div>
      </div>

      <div class="mt-12 bg-primary-50 p-8 rounded-xl border border-primary-100">
        <h3 class="text-2xl font-display font-bold text-gray-900 mb-4">
          Interessiert?
        </h3>
        <p class="text-gray-600 mb-6">
          Komm einfach zum Schnuppertraining vorbei oder kontaktiere uns für weitere Informationen!
        </p>
        <div class="flex flex-wrap gap-4">
          <NuxtLink
            to="/training/anfaenger"
            class="inline-flex items-center px-6 py-3 bg-primary-600 hover:bg-primary-700 text-white font-semibold rounded-lg transition-colors"
          >
            Infos für Anfänger
          </NuxtLink>
          <NuxtLink
            to="/kontakt"
            class="inline-flex items-center px-6 py-3 bg-white hover:bg-gray-50 text-primary-600 border-2 border-primary-600 font-semibold rounded-lg transition-colors"
          >
            Kontakt
          </NuxtLink>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { Clock, MapPin } from 'lucide-vue-next'

const config = ref(null)

const loadConfig = async () => {
  try {
    const response = await $fetch('/api/config')
    config.value = response.config
  } catch (error) {
    console.error('Fehler beim Laden der Config:', error)
  }
}

// Group training times by group
const groupedZeiten = computed(() => {
  if (!config.value?.training?.zeiten) return {}
  
  const groups = {}
  config.value.training.zeiten.forEach(zeit => {
    if (!groups[zeit.gruppe]) {
      groups[zeit.gruppe] = []
    }
    groups[zeit.gruppe].push(zeit)
  })
  return groups
})

onMounted(() => {
  loadConfig()
})

useHead({
  title: 'Trainingszeiten - Harheimer TC',
})
</script>

