<!--赠品记录卡片组件-->
<template>
  <view class="gift-record-card">
    <view class="gift-header">
      <view class="gift-name">{{ record.gift_name }}</view>
      <view class="gift-status" :class="getStatusClass(record.gift_status)">
        {{ record.gift_status_text }}
      </view>
    </view>
    
    <view class="gift-info">
      <view class="info-row">
        <text class="info-label">赠品类型：</text>
        <text class="info-value">{{ record.gift_type_text }}</text>
      </view>
      <view class="info-row">
        <text class="info-label">获得时间：</text>
        <text class="info-value">{{ formatTime(record.gift_time_formatted) }}</text>
      </view>
      <view class="info-row" v-if="record.giver_name">
        <text class="info-label">赠送人：</text>
        <text class="info-value">{{ record.giver_name }}</text>
      </view>
      <view class="info-row" v-if="record.use_time_formatted">
        <text class="info-label">使用时间：</text>
        <text class="info-value">{{ formatTime(record.use_time_formatted) }}</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'GiftRecordCard',
  props: {
    record: {
      type: Object,
      required: true
    }
  },
  methods: {
    formatTime(timeStr) {
      if (!timeStr) return '未知时间'
      
      try {
        // 如果有工具方法则使用，否则使用内置格式化
        if (this.$util && this.$util.formatToDateTime) {
          return this.$util.formatToDateTime(timeStr, 'Y-m-d H:i')
        }
        
        // 内置格式化方法
        if (timeStr.includes('-')) {
          const date = new Date(timeStr)
          const year = date.getFullYear()
          const month = String(date.getMonth() + 1).padStart(2, '0')
          const day = String(date.getDate()).padStart(2, '0')
          const hours = String(date.getHours()).padStart(2, '0')
          const minutes = String(date.getMinutes()).padStart(2, '0')
          return `${year}-${month}-${day} ${hours}:${minutes}`
        }
        
        return timeStr
      } catch (error) {
        console.error('时间格式化失败:', error)
        return timeStr
      }
    },
    
    getStatusClass(status) {
      const statusMap = {
        0: 'status-expired',  // 已失效
        1: 'status-available', // 可使用
        2: 'status-used'      // 已使用
      }
      return statusMap[status] || 'status-unknown'
    }
  }
}
</script>

<style lang="less" scoped>
.gift-record-card {
  background: #fff;
  border-radius: 16rpx;
  padding: 32rpx;
  margin-bottom: 24rpx;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.06);
}

.gift-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24rpx;
  
  .gift-name {
    font-size: 32rpx;
    font-weight: 600;
    color: #333;
    flex: 1;
  }
  
  .gift-status {
    padding: 8rpx 16rpx;
    border-radius: 8rpx;
    font-size: 24rpx;
    font-weight: 500;
    
    &.status-available {
      background: #E8F5E8;
      color: #52C41A;
    }
    
    &.status-used {
      background: #F0F0F0;
      color: #8C8C8C;
    }
    
    &.status-expired {
      background: #FFF2F0;
      color: #FF4D4F;
    }
    
    &.status-unknown {
      background: #F6F6F6;
      color: #999;
    }
  }
}

.gift-info {
  .info-row {
    display: flex;
    align-items: center;
    margin-bottom: 16rpx;
    
    &:last-child {
      margin-bottom: 0;
    }
    
    .info-label {
      font-size: 28rpx;
      color: #999;
      width: 160rpx;
      flex-shrink: 0;
    }
    
    .info-value {
      font-size: 28rpx;
      color: #333;
      flex: 1;
    }
  }
}
</style>