<template>
  <div class="modal-overlay" @click.self="$emit('close')">
    <div class="modal">
      <div class="modal-header">
        <h3>{{ $t('myTischtennisHistory.title') }}</h3>
      </div>
      
      <div class="modal-body">
        <div v-if="loading" class="loading">
          {{ $t('myTischtennisHistory.loading') }}
        </div>
        
        <div v-else-if="history.length === 0" class="no-history">
          <p>{{ $t('myTischtennisHistory.noHistory') }}</p>
        </div>
        
        <div v-else class="history-list">
          <div v-for="entry in history" :key="entry.id" class="history-entry">
            <div class="history-header">
              <span class="history-date">{{ formatDate(entry.createdAt) }}</span>
              <span class="history-status" :class="entry.success ? 'success' : 'error'">
                {{ entry.success ? $t('myTischtennisHistory.successful') : $t('myTischtennisHistory.failed') }}
              </span>
            </div>
            <div v-if="entry.message" class="history-message">
              {{ entry.message }}
            </div>
            <div v-if="entry.errorDetails" class="history-error">
              {{ entry.errorDetails }}
            </div>
            <div v-if="entry.updatedCount !== undefined" class="history-stats">
              {{ entry.updatedCount }} {{ $t('myTischtennisHistory.ratingsUpdated') }}
            </div>
          </div>
        </div>
      </div>
      
      <div class="modal-footer">
        <button class="btn-secondary" @click="$emit('close')">
          {{ $t('myTischtennisHistory.close') }}
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import apiClient from '../apiClient.js';

export default {
  name: 'MyTischtennisHistoryDialog',
  data() {
    return {
      loading: true,
      history: []
    };
  },
  async mounted() {
    await this.loadHistory();
  },
  methods: {
    async loadHistory() {
      try {
        this.loading = true;
        const response = await apiClient.get('/mytischtennis/update-history');
        this.history = response.data.history || [];
      } catch (error) {
        console.error('Fehler beim Laden der History:', error);
        this.history = [];
      } finally {
        this.loading = false;
      }
    },
    
    formatDate(dateString) {
      if (!dateString) return '-';
      const date = new Date(dateString);
      return date.toLocaleString('de-DE', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit'
      });
    }
  }
};
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal {
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
  max-width: 800px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.modal-header {
  padding: 1.5rem;
  border-bottom: 1px solid #dee2e6;
}

.modal-header h3 {
  margin: 0;
  color: #495057;
}

.modal-body {
  padding: 1.5rem;
  flex: 1;
}

.modal-footer {
  padding: 1rem 1.5rem;
  border-top: 1px solid #dee2e6;
  display: flex;
  justify-content: flex-end;
  gap: 1rem;
}

.loading {
  text-align: center;
  padding: 2rem;
  color: #6c757d;
}

.no-history {
  text-align: center;
  padding: 2rem;
  color: #6c757d;
}

.history-list {
  max-height: 400px;
  overflow-y: auto;
}

.history-entry {
  border: 1px solid #dee2e6;
  border-radius: 4px;
  padding: 1rem;
  margin-bottom: 1rem;
  background: #f8f9fa;
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}

.history-date {
  font-weight: 600;
  color: #495057;
}

.history-status {
  padding: 0.25rem 0.5rem;
  border-radius: 3px;
  font-size: 0.875rem;
  font-weight: 600;
}

.history-status.success {
  background-color: #d4edda;
  color: #155724;
}

.history-status.error {
  background-color: #f8d7da;
  color: #721c24;
}

.history-message {
  margin-top: 0.5rem;
  color: #495057;
  font-size: 0.875rem;
}

.history-error {
  margin-top: 0.5rem;
  color: #dc3545;
  font-size: 0.875rem;
  font-style: italic;
}

.history-stats {
  margin-top: 0.5rem;
  color: #28a745;
  font-size: 0.875rem;
  font-weight: 600;
}

.btn-secondary {
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
  transition: background-color 0.2s ease;
  background-color: #6c757d;
  color: white;
}

.btn-secondary:hover {
  background-color: #545b62;
}
</style>
