<template>
  <div class="wish-card" @click="$emit('click')">
    <p class="wish-content">{{ wish.content }}</p>
    <div class="wish-images" v-if="wish.images?.length">
      <van-image
        v-for="(img, i) in wish.images.slice(0, 3)"
        :key="i"
        :src="img"
        width="80"
        height="80"
        fit="cover"
        radius="4"
      />
      <span v-if="wish.images.length > 3" class="more-images">
        +{{ wish.images.length - 3 }}
      </span>
    </div>
    <div class="wish-tags" v-if="wish.tags?.length">
      <van-tag v-for="t in wish.tags" :key="t" plain type="primary" size="medium">
        {{ t }}
      </van-tag>
    </div>
    <div class="wish-footer">
      <span class="wish-tree">{{ wish.treeName }}</span>
      <span class="wish-meta">
        ❤ {{ wish.likes }}
        <span class="wish-time">{{ formatTime(wish.createdAt) }}</span>
      </span>
    </div>
  </div>
</template>

<script setup lang="ts">
defineProps<{
  wish: {
    id: number
    content: string
    images: string[]
    tags: string[]
    likes: number
    treeName: string
    createdAt: string
  }
}>()

defineEmits<{ click: [] }>()

function formatTime(s: string) {
  const d = new Date(s)
  const now = new Date()
  const diff = now.getTime() - d.getTime()
  if (diff < 3600000) return Math.floor(diff / 60000) + '分钟前'
  if (diff < 86400000) return Math.floor(diff / 3600000) + '小时前'
  return s.substring(5, 10)
}
</script>

<style scoped>
.wish-card {
  background: #fff;
  border-radius: 12px;
  padding: 16px;
  margin: 8px 16px;
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.05);
}
.wish-content {
  font-size: 15px;
  line-height: 1.6;
  margin-bottom: 10px;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.wish-images {
  display: flex;
  gap: 6px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}
.more-images {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 80px;
  height: 80px;
  background: #f0f0f0;
  border-radius: 4px;
  font-size: 14px;
  color: #999;
}
.wish-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 4px;
  margin-bottom: 10px;
}
.wish-footer {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #999;
}
.wish-tree {
  color: #07c160;
}
.wish-meta {
  display: flex;
  gap: 8px;
}
</style>
