<template>
  <div class="event-card" :class="{ registered: isRegistered, past: isPast }">
    <div class="event-image" :style="imageStyle">
      <div class="event-type-badge" :class="event.type">
        {{ eventTypeLabel }}
      </div>
      <div v-if="isPast" class="event-status past">Прошедшее</div>
      <div v-else-if="!event.registration_open" class="event-status closed">Регистрация закрыта</div>
      <div v-else-if="isFull" class="event-status full">Мест нет</div>
    </div>
    
    <div class="event-content">
      <h3 class="event-title">{{ event.title }}</h3>
      
      <div class="event-meta">
        <div class="meta-item">
          <span class="icon">📅</span>
          <span>{{ formatDate(event.date) }}</span>
        </div>
        <div class="meta-item">
          <span class="icon">📍</span>
          <span>{{ event.location }}</span>
        </div>
        <div v-if="event.distance" class="meta-item">
          <span class="icon">🏃</span>
          <span>{{ event.distance }}</span>
        </div>
      </div>
      
      <p class="event-description">{{ truncateDescription(event.description) }}</p>
      
      <div class="event-stats">
        <div class="stat">
          <span class="label">Участников:</span>
          <span class="value">{{ event.participants_count }}{{ event.max_participants > 0 ? `/${event.max_participants}` : '' }}</span>
        </div>
        <div v-if="isRegistered" class="registration-status confirmed">
          ✅ Вы зарегистрированы
        </div>
      </div>
      
      <div class="event-actions">
        <button 
          v-if="showActions && !isPast && event.registration_open && !isFull && !isRegistered"
          class="btn btn-primary btn-sm"
          @click="$emit('register', event)"
        >
          🎫 Зарегистрироваться
        </button>
        
        <button 
          v-if="showActions && isRegistered"
          class="btn btn-danger btn-sm"
          @click="$emit('cancel', getRegistrationId())"
        >
          ❌ Отменить
        </button>
        
        <button 
          class="btn btn-outline btn-sm"
          @click="$emit('view-details', event.id)"
        >
          👀 Подробнее
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import { computed } from 'vue'

export default {
  name: 'EventCard',
  props: {
    event: {
      type: Object,
      required: true
    },
    userRegistrations: {
      type: Array,
      default: () => []
    },
    showActions: {
      type: Boolean,
      default: true
    }
  },
  emits: ['register', 'cancel', 'view-details'],
  setup(props) {
    const isRegistered = computed(() => {
      return props.userRegistrations.some(reg => 
        reg.event_id === props.event.id && 
        (reg.status === 'confirmed' || reg.status === 'pending')
      )
    })
    
    const isPast = computed(() => {
      return new Date(props.event.date) <= new Date()
    })
    
    const isFull = computed(() => {
      return props.event.max_participants > 0 && 
             props.event.participants_count >= props.event.max_participants
    })
    
    const eventTypeLabel = computed(() => {
      const labels = {
        'race': 'Забег',
        'training': 'Тренировка',
        'social': 'Встреча',
        'workshop': 'Семинар'
      }
      return labels[props.event.type] || props.event.type
    })
    
    const imageStyle = computed(() => {
      if (props.event.image) {
        return {
          backgroundImage: `url(${props.event.image})`
        }
      }
      // Градиенты по умолчанию для разных типов событий
      const gradients = {
        'race': 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
        'training': 'linear-gradient(135deg, #f093fb 0%, #f5576c 100%)',
        'social': 'linear-gradient(135deg, #4facfe 0%, #00f2fe 100%)',
        'workshop': 'linear-gradient(135deg, #43e97b 0%, #38f9d7 100%)'
      }
      return {
        background: gradients[props.event.type] || 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)'
      }
    })
    
    const getRegistrationId = () => {
      const registration = props.userRegistrations.find(reg => 
        reg.event_id === props.event.id
      )
      return registration?.id
    }
    
    const formatDate = (dateString) => {
      return new Date(dateString).toLocaleDateString('ru-RU', {
        day: 'numeric',
        month: 'long',
        hour: '2-digit',
        minute: '2-digit'
      })
    }
    
    const truncateDescription = (description) => {
      if (description.length > 120) {
        return description.substring(0, 120) + '...'
      }
      return description
    }
    
    return {
      isRegistered,
      isPast,
      isFull,
      eventTypeLabel,
      imageStyle,
      getRegistrationId,
      formatDate,
      truncateDescription
    }
  }
}
</script>

<style scoped>
/* Стили из предыдущего ответа */
.event-card {
  background: white;
  border-radius: 15px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  border: 2px solid transparent;
}

.event-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.event-card.registered {
  border-color: #28a745;
}

.event-card.past {
  opacity: 0.7;
}

.event-image {
  height: 160px;
  background-size: cover;
  background-position: center;
  position: relative;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 1rem;
}

.event-type-badge {
  padding: 0.5rem 1rem;
  border-radius: 20px;
  color: white;
  font-size: 0.8rem;
  font-weight: 600;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
}

.event-type-badge.race {
  background: rgba(102, 126, 234, 0.9);
}

.event-type-badge.training {
  background: rgba(240, 147, 251, 0.9);
}

.event-type-badge.social {
  background: rgba(79, 172, 254, 0.9);
}

.event-type-badge.workshop {
  background: rgba(67, 233, 123, 0.9);
}

.event-status {
  padding: 0.5rem 1rem;
  border-radius: 20px;
  color: white;
  font-size: 0.8rem;
  font-weight: 600;
  background: rgba(0, 0, 0, 0.7);
}

.event-status.past {
  background: rgba(108, 117, 125, 0.9);
}

.event-status.closed {
  background: rgba(220, 53, 69, 0.9);
}

.event-status.full {
  background: rgba(253, 126, 20, 0.9);
}

.event-content {
  padding: 1.5rem;
}

.event-title {
  margin: 0 0 1rem;
  font-size: 1.2rem;
  font-weight: 600;
  color: #333;
  line-height: 1.4;
}

.event-meta {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: #666;
  font-size: 0.9rem;
}

.meta-item .icon {
  font-size: 1rem;
}

.event-description {
  margin: 0 0 1rem;
  color: #555;
  line-height: 1.5;
  font-size: 0.95rem;
}

.event-stats {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
  padding-top: 1rem;
  border-top: 1px solid #e9ecef;
}

.stat {
  display: flex;
  gap: 0.5rem;
  font-size: 0.9rem;
}

.stat .label {
  color: #666;
}

.stat .value {
  font-weight: 600;
  color: #333;
}

.registration-status {
  padding: 0.25rem 0.75rem;
  border-radius: 12px;
  font-size: 0.8rem;
  font-weight: 500;
}

.registration-status.confirmed {
  background: #d4edda;
  color: #155724;
}

.event-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.btn-sm {
  padding: 0.5rem 1rem;
  font-size: 0.85rem;
}

/* Адаптивность */
@media (max-width: 480px) {
  .event-actions {
    flex-direction: column;
  }
  
  .event-stats {
    flex-direction: column;
    gap: 0.5rem;
    align-items: flex-start;
  }
}
</style>