<template>
  <div class="product-card">
    <div class="card-header" v-if="showCheckbox || showAction">
      <el-checkbox v-if="showCheckbox" v-model="checked" @change="handleCheck" />
      <slot name="action">
        <span v-if="actionText" class="action-btn" @click="$emit('action')">{{ actionText }}</span>
      </slot>
    </div>
    <div class="product-image">
      <el-image :src="product.image" fit="contain">
        <template #error>
          <div class="image-placeholder">
            <el-icon :size="40" color="#ccc"><Picture /></el-icon>
          </div>
        </template>
      </el-image>
    </div>
    <div class="product-info">
      <div class="product-name">{{ product.name }}</div>
      <div class="product-price">
        <span v-if="product.tag" class="price-tag">{{ product.tag }}</span>
        <span class="current-price">¥{{ product.price }}</span>
        <span v-if="product.originalPrice" class="original-price">¥{{ product.originalPrice }}</span>
        <div v-if="showAddCart" class="add-cart" @click="$emit('addCart')">
          <el-icon><Plus /></el-icon>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { Picture, Plus } from '@element-plus/icons-vue'

interface Product {
  image?: string
  name: string
  price: string | number
  originalPrice?: string | number
  tag?: string
}

const props = defineProps<{
  product: Product
  modelValue?: boolean
  showCheckbox?: boolean
  showAction?: boolean
  showAddCart?: boolean
  actionText?: string
}>()

const emit = defineEmits<{
  'update:modelValue': [value: boolean]
  'action': []
  'addCart': []
}>()

const checked = ref(props.modelValue || false)
watch(() => props.modelValue, (val) => { checked.value = val || false })

const handleCheck = (val: boolean | string | number) => {
  emit('update:modelValue', !!val)
}
</script>

<style scoped lang="scss">
.product-card {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid #eee;

  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 12px;
    .action-btn {
      font-size: 12px;
      color: #999;
      cursor: pointer;
      &:hover { color: #e60012; }
    }
  }

  .product-image {
    height: 160px;
    background: #f9f9f9;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 10px;
    .el-image { width: 100%; height: 100%; }
    .image-placeholder { width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; background: #f5f5f5; }
  }

  .product-info {
    padding: 12px;
    .product-name {
      font-size: 13px;
      color: #333;
      line-height: 1.4;
      height: 36px;
      overflow: hidden;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      margin-bottom: 8px;
    }
    .product-price {
      display: flex;
      align-items: center;
      gap: 5px;
      position: relative;
      .price-tag { font-size: 12px; color: #e60012; }
      .current-price { font-size: 16px; font-weight: bold; color: #e60012; }
      .original-price { font-size: 12px; color: #999; text-decoration: line-through; }
      .add-cart {
        position: absolute;
        right: 0;
        bottom: 0;
        width: 24px;
        height: 24px;
        border-radius: 50%;
        border: 1px solid #e60012;
        color: #e60012;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        transition: all 0.2s;
        &:hover { background: #e60012; color: #fff; }
      }
    }
  }
}
</style>
