<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import type { AiRecommendation } from '../models/bid-item'

const props = defineProps<{
  api: any
}>()

const recommendations = ref<AiRecommendation[]>([])
const loading = ref(true)
const error = ref('')

const loadRecommendations = async () => {
  try {
    loading.value = true
    error.value = ''
    recommendations.value = await props.api.getAiRecommendations()
  } catch (err) {
    error.value = `加载失败: ${err}`
    console.error('加载AI推荐失败:', err)
  } finally {
    loading.value = false
  }
}

const getConfidenceColor = (confidence: number) => {
  if (confidence >= 80) return '#27ae60'
  if (confidence >= 60) return '#f39c12'
  return '#e74c3c'
}

const getConfidenceLabel = (confidence: number) => {
  if (confidence >= 80) return '高'
  if (confidence >= 60) return '中'
  return '低'
}

onMounted(() => {
  loadRecommendations()
})

defineExpose({
  loadRecommendations
})
</script>

<template>
  <div class="ai-recommendations-container">
    <div v-if="loading" class="loading">
      加载中...
    </div>

    <div v-else-if="error" class="error">
      {{ error }}
    </div>

    <div v-else-if="recommendations.length === 0" class="empty">
      暂无推荐项目
    </div>

    <div v-else class="recommendation-list">
      <div
        v-for="item in recommendations"
        :key="item.id"
        class="recommendation-item"
      >
        <div class="recommendation-header">
          <span
            class="confidence-badge"
            :style="{ backgroundColor: getConfidenceColor(item.confidence) }"
          >
            {{ getConfidenceLabel(item.confidence) }} ({{ item.confidence }}%)
          </span>
          <span class="date">{{ item.createdAt }}</span>
        </div>
        <h3 class="recommendation-title">{{ item.title }}</h3>
      </div>
    </div>
  </div>
</template>

<style scoped>
.ai-recommendations-container {
  padding: 8px;
}

.loading,
.error,
.empty {
  text-align: center;
  padding: 20px;
  color: #666;
  font-size: 12px;
}

.error {
  color: #e74c3c;
}

.recommendation-list {
  display: grid;
  gap: 8px;
}

.recommendation-item {
  background: #fff;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  padding: 8px;
  transition: all 0.2s ease;
}

.recommendation-item:hover {
  border-color: #3498db;
  box-shadow: 0 1px 4px rgba(52, 152, 219, 0.15);
  transform: translateY(-1px);
}

.recommendation-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.confidence-badge {
  color: #fff;
  padding: 2px 6px;
  border-radius: 3px;
  font-size: 10px;
  font-weight: 500;
}

.date {
  font-size: 10px;
  color: #999;
}

.recommendation-title {
  font-size: 12px;
  font-weight: 500;
  color: #333;
  margin: 0;
  line-height: 1.4;
}
</style>
