<template>
  <div class="statistics-card" :style="cardStyle">
    <div class="card-content">
      <div class="info">
        <div class="label" :style="labelStyle">{{ label | dv}}</div>
        <div class="value" :style="valueStyle">
          <count-to :start-val="0" :end-val="value" :duration="3000" :decimals="precision"/>
        </div>
        <div v-if="subLabel" class="subtitle" :style="subtitleStyle">
          <span class="sub-label">{{ subLabel | dv}}</span>
          <span class="sub-value" :class="{ 'up': subValue >= 0, 'down': subValue < 0 }">
            <count-to :start-val="0" :end-val="subValue" :duration="3000" :decimals="precision"/>
            <svg-icon v-if="showValueChange && subValue >= 0" icon-class="up" />
            <svg-icon v-else-if="showValueChange && subValue < 0" icon-class="down" />
          </span>
        </div>
      </div>
      <div class="icon-wrapper">
        <i :class="icon" :style="iconStyle"></i>
      </div>
    </div>
  </div>
</template>

<script>
import CountTo from 'vue-count-to'
export default {
  name: 'StatisticsCard',
  components: {
    CountTo
  },
  props: {
    value: {
      type: Number,
      default: null,
    },
    label: {
      type: String,
      required: true
    },
    subLabel: {
      type: String,
      default: null
    },
    subValue: {
      type: Number,
      default: null
    },
    icon: {
      type: String,
      required: true
    },
    startColor: {
      type: String,
      required: true
    },
    endColor: {
      type: String,
      required: true
    },
    width: {
      type: [Number, String],
      default: '100%'
    },
    height: {
      type: [Number, String],
      default: 120
    },
    // 是否显示值变化
    showValueChange: {
      type: Boolean,
      default: true
    },
    // 小数位数
    precision: {
      type: Number,
      default: 0
    }
  },
  computed: {
    // 转换宽高为像素值
    sizeInPx() {
      const width = typeof this.width === 'number' ? `${this.width}px` : this.width;
      const height = typeof this.height === 'number' ? `${this.height}px` : this.height;
      return { width, height };
    },
    // 根据高度动态计算字体大小
    fontSizes() {
      const height = typeof this.height === 'number' ? this.height : parseInt(this.height);
      const baseHeight = 120; // 基准高度
      const scale = height / baseHeight;
      const hasSubtitle = this.subLabel !== null;
      
      return {
        value: Math.max(16, Math.round(hasSubtitle ? 28 * scale : 40 * scale)),
        label: Math.max(12, Math.round(hasSubtitle ? 16 * scale : 22 * scale)),
        subtitle: Math.max(10, Math.round(14 * scale)),
        icon: Math.max(32, Math.round(64 * scale))
      };
    },
    cardStyle() {
      return {
        background: `linear-gradient(135deg, ${this.startColor}15, ${this.endColor}15)`,
        width: this.sizeInPx.width,
        height: this.sizeInPx.height
      }
    },
    iconStyle() {
      return {
        background: `linear-gradient(135deg, ${this.startColor}, ${this.endColor})`,
        '-webkit-background-clip': 'text',
        '-webkit-text-fill-color': 'transparent',
        'background-clip': 'text',
        'font-size': `${this.fontSizes.icon}px`
      }
    },
    valueStyle() {
      return {
        'font-size': `${this.fontSizes.value}px`
      }
    },
    labelStyle() {
      return {
        'font-size': `${this.fontSizes.label}px`
      }
    },
    subtitleStyle() {
      return {
        'font-size': `${this.fontSizes.subtitle}px`
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.statistics-card {
  border-radius: 8px;
  padding: 20px;
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
  backdrop-filter: blur(5px);
  box-sizing: border-box;
  cursor: pointer;
  
  &:hover {
    transform: translateY(-4px);
  }
  
  .card-content {
    position: relative;
    height: 100%;
    z-index: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    
    .info {
      position: relative;
      z-index: 2;
      
      .value {
        font-weight: bold;
        color: #303133;
        line-height: 1.2;
        margin-bottom: 8px;
      }
      
      .label {
        color: #606266;
        margin-bottom: 4px;
      }

      .subtitle {
        display: flex;
        align-items: flex-end;
        .sub-label {
          color: #333;
          opacity: 0.8;
          margin-right: 4px;
        }
        .sub-value {
          color: #333;
          opacity: 0.8;
          &.up {
            color: #dc3545;
          }
          &.down {
            color: #198754;
          }
        }
        
      }
    }
    
    .icon-wrapper {
      position: absolute;
      right: -15px;
      bottom: -20px;
      z-index: 1;
      
      i {
        opacity: 0.15;
        transition: all 0.3s ease;
      }
    }
  }

  &:hover {
    .icon-wrapper i {
      opacity: 0.25;
      transform: scale(1.2) rotate(10deg);
    }
  }
}
</style> 