<script setup>
import { computed } from 'vue';
import { Card, Image, Button } from 'ant-design-vue';

const props = defineProps({
  content: {
    type: Object,
    required: true,
  },
});

const emit = defineEmits(['click-product']);

// 解析内容
const productData = computed(() => {
  if (typeof props.content === 'string') {
    try {
      return JSON.parse(props.content);
    } catch {
      return {};
    }
  }
  return props.content;
});

const handleClick = () => {
  emit('click-product', productData.value);
};
</script>

<template>
  <Card class="product-card" :bordered="true" style="max-width: 300px">
    <div class="product-content" @click="handleClick">
      <div v-if="productData.image" class="product-image">
        <Image :src="productData.image" :preview="false" style="width: 100%; height: 150px; object-fit: cover;" />
      </div>
      
      <div class="product-info">
        <div class="product-name">{{ productData.drug_name || '商品名称' }}</div>
        
        <div v-if="productData.specification" class="product-spec">
          规格：{{ productData.specification }}
        </div>

        <div v-if="productData.function" class="product-function">
          功效：{{ productData.function }}
        </div>

        <div class="product-price">
          <span class="price-label">价格：</span>
          <span class="price-value">¥{{ productData.price || '0.00' }}</span>
        </div>
      </div>
    </div>
  </Card>
</template>

<style scoped>
.product-card {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s;
}

.product-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transform: translateY(-2px);
}

.product-content {
  padding: 8px;
}

.product-image {
  margin-bottom: 8px;
  border-radius: 4px;
  overflow: hidden;
}

.product-info {
  padding: 0 4px;
}

.product-name {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 8px;
  color: #333;
}

.product-spec,
.product-function {
  font-size: 12px;
  color: #666;
  margin-bottom: 4px;
}

.product-price {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid #eee;
}

.price-label {
  font-size: 12px;
  color: #666;
}

.price-value {
  font-size: 18px;
  font-weight: 600;
  color: #ff4d4f;
  margin-left: 4px;
}

/* 暗色模式适配 */
.dark .product-card {
  background: #1e293b;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.dark .product-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}

.dark .product-name {
  color: #e2e8f0;
}

.dark .product-spec,
.dark .product-function {
  color: #94a3b8;
}

.dark .product-price {
  border-top-color: #334155;
}

.dark .price-label {
  color: #94a3b8;
}

.dark .price-value {
  color: #f87171;
}
</style>
