<template>
  <div class="bg-white rounded-lg shadow-lg p-6">
    <div class="flex justify-between items-center mb-6">
      <h2 class="text-2xl font-bold text-gray-800">📋 История анализов</h2>
      <button 
        @click="$emit('clear-history')"
        class="bg-red-500 hover:bg-red-600 text-white px-4 py-2 rounded-lg transition-colors"
        :disabled="history.length === 0"
      >
        Очистить историю
      </button>
    </div>

    <div v-if="history.length === 0" class="text-center py-12">
      <div class="text-6xl mb-4">📊</div>
      <h3 class="text-xl font-semibold text-gray-600 mb-2">История пуста</h3>
      <p class="text-gray-500">Выполните анализ поля, чтобы сохранить его в историю</p>
    </div>

    <div v-else class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
      <div 
        v-for="item in history" 
        :key="item.id"
        class="border border-gray-200 rounded-lg p-4 hover:shadow-md transition-shadow bg-white"
      >
        <div class="flex justify-between items-start mb-3">
          <h3 class="font-semibold text-lg text-gray-800 truncate">{{ item.fieldName }}</h3>
          <button 
            @click="$emit('delete-history', item.id)"
            class="text-red-500 hover:text-red-700 transition-colors"
            title="Удалить"
          >
            🗑️
          </button>
        </div>
        
        <div class="space-y-2 text-sm text-gray-600 mb-4">
          <div class="flex justify-between">
            <span>Дата анализа:</span>
            <span class="font-medium">{{ new Date(item.date).toLocaleDateString() }}</span>
          </div>
          <div class="flex justify-between">
            <span>Площадь:</span>
            <span class="font-medium">{{ item.area }} га</span>
          </div>
          <div class="flex justify-between">
            <span>Средний NDVI:</span>
            <span :class="getNDVIClass(item.result.stats.mean)" class="font-medium">
              {{ item.result.stats.mean }}
            </span>
          </div>
        </div>

        <div class="space-y-2 mb-4">
          <div class="flex justify-between text-xs">
            <span class="text-green-600">Здоровая: {{ item.result.stats.healthy }}%</span>
            <span class="text-yellow-600">Умеренная: {{ item.result.stats.moderate }}%</span>
            <span class="text-red-600">Слабая: {{ item.result.stats.poor }}%</span>
          </div>
        </div>

        <button 
          @click="$emit('load-analysis', item)"
          class="w-full bg-blue-500 hover:bg-blue-600 text-white py-2 px-4 rounded-lg transition-colors flex items-center justify-center"
        >
          <span>Загрузить анализ</span>
        </button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'HistoryComponent',
  props: {
    history: {
      type: Array,
      default: () => []
    }
  },
  emits: ['load-analysis', 'delete-history', 'clear-history'],
  methods: {
    getNDVIClass(ndvi) {
      if (ndvi > 0.6) return 'text-green-600';
      if (ndvi > 0.3) return 'text-yellow-600';
      return 'text-red-600';
    }
  }
};
</script>