<script setup lang="ts">
import { computed } from 'vue'

const props = defineProps<{
  title: string
  value: string | number
  unit?: string
  changeRate?: number
  status?: 'success' | 'warning' | 'danger' | 'info'
  subtext?: string
  trend?: 'up' | 'down' | 'neutral'
}>()

const statusClass = computed(() => {
  if (props.status) return `ks-metric-card--${props.status}`
  if (props.trend === 'up') return 'ks-metric-card--up'
  if (props.trend === 'down') return 'ks-metric-card--down'
  return ''
})

const formattedChangeRate = computed(() => {
  if (props.changeRate === undefined) return null
  const prefix = props.changeRate > 0 ? '+' : ''
  return `${prefix}${props.changeRate.toFixed(2)}%`
})
</script>

<template>
  <div class="ks-metric-card" :class="statusClass">
    <div class="ks-metric-card__header">
      <span class="ks-metric-card__title">{{ title }}</span>
      <span v-if="trend || status" class="ks-metric-card__badge" :class="`ks-badge--${trend || status}`">
        {{ trend ? (trend === 'up' ? '▲' : trend === 'down' ? '▼' : '-') : status }}
      </span>
    </div>
    
    <div class="ks-metric-card__body">
      <div class="ks-metric-card__value-group">
        <span class="ks-metric-card__value ks-financial-number">{{ value }}</span>
        <span v-if="unit" class="ks-metric-card__unit">{{ unit }}</span>
      </div>

      <div v-if="formattedChangeRate || subtext" class="ks-metric-card__footer">
        <span v-if="formattedChangeRate" class="ks-metric-card__change" :class="changeRate! >= 0 ? 'ks-text-up' : 'ks-text-down'">
          {{ formattedChangeRate }}
        </span>
        <span v-if="subtext" class="ks-metric-card__subtext">{{ subtext }}</span>
      </div>
    </div>
  </div>
</template>

<style scoped>
.ks-metric-card {
  padding: 8px 10px;
  background: var(--ks-color-surface);
  border: 1px solid var(--ks-color-border-strong);
  border-radius: var(--ks-radius-md);
  box-shadow: var(--ks-shadow-sm);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  height: 72px;
  box-sizing: border-box;
  transition: transform 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease;
}

.ks-metric-card:hover {
  border-color: var(--ks-color-action);
  box-shadow: var(--ks-shadow-md);
}

.ks-metric-card__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  line-height: 1;
}

.ks-metric-card__title {
  font-size: var(--ks-font-caption, 11px);
  font-weight: 600;
  color: var(--ks-color-text-muted, #64748b);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ks-metric-card__badge {
  font-size: 10px;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: 2px;
  line-height: 1.2;
}

.ks-badge--up, .ks-badge--success { background: rgba(16, 185, 129, 0.12); color: var(--ks-color-success, #059669); }
.ks-badge--down, .ks-badge--danger { background: rgba(239, 68, 68, 0.12); color: var(--ks-color-danger, #dc2626); }
.ks-badge--warning { background: rgba(245, 158, 11, 0.12); color: var(--ks-color-warning, #d97706); }
.ks-badge--info { background: rgba(59, 130, 246, 0.12); color: var(--ks-color-info, #2563eb); }

.ks-metric-card__body {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 4px;
}

.ks-metric-card__value-group {
  display: flex;
  align-items: baseline;
  gap: 2px;
}

.ks-metric-card__value {
  font-size: var(--ks-font-page, 15px);
  font-weight: 700;
  color: var(--ks-color-text, #0f172a);
  line-height: 1.2;
}

.ks-metric-card__unit {
  font-size: 11px;
  color: var(--ks-color-text-muted);
}

.ks-metric-card__footer {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  line-height: 1;
}

.ks-text-up { color: var(--ks-color-success, #059669); font-weight: 600; }
.ks-text-down { color: var(--ks-color-danger, #dc2626); font-weight: 600; }
.ks-metric-card__subtext { color: var(--ks-color-text-muted, #64748b); font-size: 11px; white-space: nowrap; }
</style>

