<template>
  <BaseDialog
    :model-value="modelValue"
    @update:model-value="$emit('update:modelValue', $event)"
    :title="`${$t('trainingDetails.title')}: ${member ? member.firstName + ' ' + member.lastName : ''}`"
    size="large"
    @close="handleClose"
  >
    <div v-if="member" class="details-modal-content">
      <div class="member-info">
        <div class="info-item">
          <strong>{{ $t('trainingDetails.birthdate') }}:</strong> {{ formatBirthdate(member.birthDate) }}
        </div>
        <div class="info-item">
          <strong>{{ $t('trainingDetails.birthYear') }}:</strong> {{ getBirthYear(member.birthDate) }}
        </div>
      </div>

      <div class="participation-summary">
        <div class="summary-item">
          <span class="label">{{ $t('trainingDetails.last12Months') }}:</span>
          <span class="value">{{ member.participation12Months }}</span>
        </div>
        <div class="summary-item">
          <span class="label">{{ $t('trainingDetails.last3Months') }}:</span>
          <span class="value">{{ member.participation3Months }}</span>
        </div>
        <div class="summary-item">
          <span class="label">{{ $t('trainingDetails.total') }}:</span>
          <span class="value">{{ member.participationTotal }}</span>
        </div>
      </div>

      <div class="training-details">
        <h4>{{ $t('trainingDetails.trainingParticipations') }}</h4>
        <div class="training-list">
          <div 
            v-for="training in member.trainingDetails" 
            :key="training.id" 
            class="training-item"
          >
            <div class="training-date">{{ formatDate(training.date) }}</div>
            <div class="training-activity">{{ training.activityName }}</div>
          </div>
        </div>
        <div v-if="!member.trainingDetails || member.trainingDetails.length === 0" class="no-trainings">
          <em>{{ $t('trainingDetails.noTrainings') }}</em>
        </div>
      </div>
    </div>
  </BaseDialog>
</template>

<script>
import BaseDialog from './BaseDialog.vue';

export default {
  name: 'TrainingDetailsDialog',
  components: {
    BaseDialog
  },
  props: {
    modelValue: {
      type: Boolean,
      default: false
    },
    member: {
      type: Object,
      default: null
    }
  },
  emits: ['update:modelValue', 'close'],
  methods: {
    handleClose() {
      this.$emit('update:modelValue', false);
      this.$emit('close');
    },
    formatBirthdate(dateString) {
      if (!dateString) return '-';
      const date = new Date(dateString);
      return date.toLocaleDateString('de-DE');
    },
    getBirthYear(dateString) {
      if (!dateString) return '-';
      const date = new Date(dateString);
      return date.getFullYear();
    },
    formatDate(dateString) {
      if (!dateString) return '';
      const date = new Date(dateString);
      return date.toLocaleDateString('de-DE', {
        weekday: 'short',
        year: 'numeric',
        month: '2-digit',
        day: '2-digit'
      });
    }
  }
};
</script>

<style scoped>
.details-modal-content {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.member-info {
  display: flex;
  gap: 2rem;
  padding: 1rem;
  background: var(--background-light);
  border-radius: 4px;
}

.info-item {
  display: flex;
  gap: 0.5rem;
}

.info-item strong {
  color: var(--text-color);
}

.participation-summary {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 1rem;
}

.summary-item {
  padding: 1rem;
  background: var(--background-light);
  border-radius: 4px;
  text-align: center;
}

.summary-item .label {
  display: block;
  font-size: 0.875rem;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}

.summary-item .value {
  display: block;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--primary-color);
}

.training-details h4 {
  margin: 0 0 1rem 0;
  color: var(--text-color);
}

.training-list {
  max-height: 400px;
  overflow-y: auto;
  border: 1px solid var(--border-color);
  border-radius: 4px;
}

.training-item {
  display: flex;
  gap: 1rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border-color);
  transition: background-color 0.2s;
}

.training-item:last-child {
  border-bottom: none;
}

.training-item:hover {
  background: var(--background-light);
}

.training-date {
  font-weight: 600;
  min-width: 120px;
}

.training-activity {
  flex: 1;
}

.no-trainings {
  padding: 2rem;
  text-align: center;
  color: var(--text-muted);
}

@media (max-width: 768px) {
  .member-info {
    flex-direction: column;
    gap: 0.5rem;
  }
  
  .participation-summary {
    grid-template-columns: 1fr;
  }
  
  .training-item {
    flex-direction: column;
    gap: 0.25rem;
  }
  
  .training-date {
    min-width: auto;
  }
}
</style>
