<!--
  统计卡片组件
  展示关键指标、趋势、图标等
-->

<template>
  <el-card
    class="stat-card"
    :class="{
      'stat-card--clickable': clickable,
      'stat-card--loading': loading,
    }"
    :body-style="{ padding: '20px' }"
    shadow="hover"
    @click="handleClick"
  >
    <!-- 骨架屏加载 -->
    <el-skeleton :loading="loading" animated>
      <template #template>
        <el-skeleton-item variant="text" style="width: 40%; height: 16px; margin-bottom: 12px" />
        <el-skeleton-item variant="text" style="width: 70%; height: 28px" />
      </template>

      <!-- 实际内容 -->
      <template #default>
        <!-- 头部：标题和图标 -->
        <div class="stat-card__header">
          <span class="stat-card__title">{{ title }}</span>
          <el-icon v-if="icon" class="stat-card__icon" :style="{ color: color }">
            <component :is="icon" />
          </el-icon>
        </div>

        <!-- 数值 -->
        <div class="stat-card__value" :style="{ color: color }">
          <span class="stat-card__value-number">{{ formattedValue }}</span>
          <span v-if="unit" class="stat-card__value-unit">{{ unit }}</span>
        </div>

        <!-- 趋势 -->
        <div v-if="trend && trendValue !== undefined" class="stat-card__trend">
          <el-tag
            :type="trendType"
            size="small"
            effect="plain"
            :icon="trendIcon"
          >
            {{ trendText }}
          </el-tag>
          <span class="stat-card__trend-label">较上期</span>
        </div>
      </template>
    </el-skeleton>
  </el-card>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { TrendCharts, ArrowUp, ArrowDown, Minus } from '@element-plus/icons-vue'
import type { StatCardConfig } from '@/types/charts'

/** Props */
interface Props {
  title: string
  value: number | string
  unit?: string
  icon?: any
  trend?: 'up' | 'down' | 'flat'
  trendValue?: number
  color?: string
  loading?: boolean
  clickable?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  title: '',
  value: 0,
  unit: '',
  icon: undefined,
  trend: undefined,
  trendValue: undefined,
  color: '#475569',
  loading: false,
  clickable: false,
})

/** Emits */
interface Emits {
  (e: 'click'): void
}

const emit = defineEmits<Emits>()

/** 格式化数值 */
const formattedValue = computed(() => {
  if (typeof props.value === 'string') {
    return props.value
  }

  // 大数值格式化
  if (props.value >= 100000000) {
    return (props.value / 100000000).toFixed(2) + '亿'
  }
  if (props.value >= 10000) {
    return (props.value / 10000).toFixed(2) + '万'
  }
  if (props.value >= 1000) {
    return (props.value / 1000).toFixed(1) + 'K'
  }

  return props.value.toLocaleString()
})

/** 趋势类型 */
const trendType = computed(() => {
  if (props.trend === 'up') return 'success'
  if (props.trend === 'down') return 'danger'
  return 'info'
})

/** 趋势图标 */
const trendIcon = computed(() => {
  if (props.trend === 'up') return ArrowUp
  if (props.trend === 'down') return ArrowDown
  return Minus
})

/** 趋势文本 */
const trendText = computed(() => {
  if (props.trendValue === undefined) return ''

  const sign = props.trend === 'up' ? '+' : props.trend === 'down' ? '-' : ''
  return `${sign}${Math.abs(props.trendValue).toFixed(1)}%`
})

/** 处理点击 */
const handleClick = () => {
  if (props.clickable) {
    emit('click')
  }
}
</script>

<style scoped lang="scss">
.stat-card {
  height: 100%;
  transition: all 0.3s ease;

  &--clickable {
    cursor: pointer;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }
  }

  &--loading {
    pointer-events: none;
  }

  &__header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
  }

  &__title {
    font-size: 14px;
    color: #64748b;
    font-weight: 500;
  }

  &__icon {
    font-size: 24px;
    opacity: 0.8;
  }

  &__value {
    display: flex;
    align-items: baseline;
    margin-bottom: 12px;
    gap: 4px;
  }

  &__value-number {
    font-size: 28px;
    font-weight: 600;
    line-height: 1;
  }

  &__value-unit {
    font-size: 14px;
    color: #64748b;
    margin-left: 4px;
  }

  &__trend {
    display: flex;
    align-items: center;
    gap: 8px;
  }

  &__trend-label {
    font-size: 12px;
    color: #94a3b8;
  }
}
</style>
