<template>
  <div class="card" @click="$emit('select', item.id)">
    <div class="card-thumb" :class="typeBgClass">
      <div class="card-thumb-inner">{{ typeEmoji }}</div>
      <div class="type-badge" :class="typeBadgeClass">{{ typeLabel }}</div>
      <div class="fav-btn" @click.stop="$emit('toggleStar', item.id)">
        <svg width="13" height="13" viewBox="0 0 24 24" :fill="item.starred ? 'var(--accent)' : 'none'" :stroke="item.starred ? 'var(--accent)' : 'rgba(255,255,255,0.6)'" stroke-width="2">
          <polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2" />
        </svg>
      </div>
    </div>
    <div class="card-body">
      <div class="card-source" v-if="item.sourceHost">
        <div class="card-source-dot" :style="{ background: typeAccent + '22', color: typeAccent }">●</div>
        {{ item.sourceHost }}
      </div>
      <div class="card-title-text">{{ item.title }}</div>
      <div class="card-desc" v-if="item.description">{{ item.description }}</div>
      <div class="card-meta">
        <div class="card-tags">
          <TagBadge v-for="t in item.tags?.slice(0, 2)" :key="t.id || t.name" :name="t.name" :color="t.color" />
        </div>
        <div class="card-date">{{ formatDate(item.createdAt) }}</div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
const props = defineProps<{ item: any }>();
defineEmits<{ select: [id: number]; toggleStar: [id: number] }>();

const typeMap: Record<string, any> = {
  web: { label: '网页', emoji: '🌐', bg: 'bg-web', badge: 'badge-web', accent: 'var(--blue)' },
  text: { label: '文本', emoji: '📝', bg: 'bg-text', badge: 'badge-text', accent: 'var(--purple)' },
  image: { label: '图片', emoji: '📷', bg: 'bg-img', badge: 'badge-img', accent: 'var(--teal)' },
  video: { label: '视频', emoji: '▶️', bg: 'bg-vid', badge: 'badge-vid', accent: 'var(--red)' },
  file: { label: '文档', emoji: '📄', bg: 'bg-doc', badge: 'badge-doc', accent: 'var(--accent)' },
};

const typeLabel = computed(() => typeMap[props.item.type]?.label || props.item.type);
const typeEmoji = computed(() => typeMap[props.item.type]?.emoji || '📌');
const typeBgClass = computed(() => typeMap[props.item.type]?.bg || 'bg-web');
const typeBadgeClass = computed(() => typeMap[props.item.type]?.badge || 'badge-web');
const typeAccent = computed(() => typeMap[props.item.type]?.accent || 'var(--blue)');

function formatDate(ts: number) {
  if (!ts) return '';
  const d = new Date(ts);
  return `${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
}
</script>

<style scoped>
.card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  cursor: pointer;
  transition: border-color 0.2s, transform 0.15s, box-shadow 0.2s;
  animation: fadeUp 0.3s ease both;
}
.card:hover {
  border-color: var(--border2);
  transform: translateY(-2px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}
.card-thumb {
  height: 120px;
  position: relative;
  overflow: hidden;
}
.card-thumb-inner {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
}
.type-badge {
  position: absolute;
  top: 8px;
  left: 8px;
  font-size: 10px;
  font-weight: 500;
  padding: 2px 7px;
  border-radius: 4px;
  backdrop-filter: blur(8px);
}
.fav-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 26px;
  height: 26px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.15s;
}
.card:hover .fav-btn { opacity: 1; }
.card-body { padding: 12px 14px 14px; }
.card-title-text {
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  line-height: 1.4;
  margin-bottom: 6px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.card-desc {
  font-size: 12px;
  color: var(--text2);
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  margin-bottom: 10px;
}
.card-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.card-tags { display: flex; gap: 4px; flex-wrap: wrap; }
.card-date { font-size: 11px; color: var(--text3); font-family: var(--font-mono); white-space: nowrap; }
.card-source {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  color: var(--text3);
  margin-bottom: 8px;
}
.card-source-dot {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  background: var(--bg4);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 8px;
}
</style>
