<script lang="ts" setup>
/**
 * KPI 卡片壳：统一「渐变竖条标题 + 芯片区」的 Premium 外壳
 * 为什么：各角色 KPI 组件共用同一外壳，避免每个组件复制标题/容器样式
 * 同时统一收口加载骨架 / 失败重试 / 手动刷新，KPI 组件只管拉数与渲染芯片
 */
import { IconifyIcon as VbenIcon } from '@vben/icons';

interface Props {
  /** 卡片标题 */
  title: string;
  /** 标题右侧补充说明（如「近7天」） */
  subtitle?: string;
  /** 加载中：渲染骨架占位，不再整卡消失 */
  loading?: boolean;
  /** 请求失败：渲染错误行 + 重试按钮 */
  error?: boolean;
}

withDefaults(defineProps<Props>(), {
  subtitle: '',
  loading: false,
  error: false,
});

const emit = defineEmits<{
  /** 点击刷新/重试时触发，由外部组件重新拉数 */
  refresh: [];
}>();
</script>

<template>
  <div class="kpi-card">
    <div class="kpi-card__head">
      <span class="kpi-card__title-bar"></span>
      <span class="kpi-card__title">{{ title }}</span>
      <span v-if="subtitle" class="kpi-card__subtitle">{{ subtitle }}</span>
      <button
        type="button"
        class="kpi-card__refresh"
        :class="{ 'is-loading': loading }"
        :disabled="loading"
        aria-label="刷新"
        @click="emit('refresh')"
      >
        <VbenIcon icon="lucide:refresh-cw" />
      </button>
    </div>

    <!-- 加载骨架：占位形状与统计芯片一致，避免布局跳动 -->
    <div v-if="loading" class="kpi-card__chips" aria-label="加载中">
      <div v-for="n in 3" :key="n" class="kpi-skeleton">
        <div class="sk sk-icon"></div>
        <div class="sk-lines">
          <div class="sk sk-value"></div>
          <div class="sk sk-label"></div>
        </div>
      </div>
    </div>

    <!-- 失败态：可重试，不再静默消失 -->
    <div v-else-if="error" class="kpi-card__error">
      <VbenIcon icon="lucide:cloud-off" class="kpi-card__error-icon" />
      <span>数据加载失败</span>
      <button type="button" class="kpi-card__retry" @click="emit('refresh')">
        重试
      </button>
    </div>

    <div v-else class="kpi-card__chips">
      <slot></slot>
    </div>
  </div>
</template>

<style scoped>
.kpi-card {
  padding: 18px 20px;
  background: hsl(var(--card));
  border: 1px solid hsl(var(--border) / 0.7);
  border-radius: 12px;
}

.kpi-card__head {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 12px;
}

.kpi-card__title-bar {
  width: 3px;
  height: 14px;
  background: linear-gradient(180deg, hsl(var(--primary)), hsl(var(--primary) / 0.4));
  border-radius: 2px;
}

.kpi-card__title {
  font-size: 15px;
  font-weight: 650;
  color: hsl(var(--foreground));
  letter-spacing: -0.01em;
}

.kpi-card__subtitle {
  font-size: 12px;
  color: hsl(var(--muted-foreground));
}

/* 刷新按钮：靠右，hover 才显主色，避免抢视觉 */
.kpi-card__refresh {
  display: grid;
  place-items: center;
  width: 26px;
  height: 26px;
  margin-left: auto;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: hsl(var(--muted-foreground));
  cursor: pointer;
  transition:
    color 0.15s ease,
    background 0.15s ease;
}

.kpi-card__refresh:hover:not(:disabled) {
  color: hsl(var(--primary));
  background: hsl(var(--primary) / 0.08);
}

.kpi-card__refresh :deep(svg) {
  width: 14px;
  height: 14px;
}

.kpi-card__refresh.is-loading :deep(svg) {
  animation: kpi-spin 0.8s linear infinite;
}

.kpi-card__chips {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

/* ===== 骨架占位 ===== */
.kpi-skeleton {
  display: flex;
  gap: 12px;
  align-items: center;
  min-width: 128px;
  padding: 14px 16px;
  border: 1px solid hsl(var(--border) / 0.5);
  border-radius: 12px;
}

.sk {
  border-radius: 8px;
  background: linear-gradient(
    90deg,
    hsl(var(--muted) / 0.5) 25%,
    hsl(var(--muted) / 0.3) 50%,
    hsl(var(--muted) / 0.5) 75%
  );
  background-size: 200% 100%;
  animation: kpi-shimmer 1.4s ease-in-out infinite;
}

.sk-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
}

.sk-lines {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.sk-value {
  width: 56px;
  height: 18px;
}

.sk-label {
  width: 40px;
  height: 10px;
}

/* ===== 失败态 ===== */
.kpi-card__error {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 14px 4px;
  font-size: 13px;
  color: hsl(var(--muted-foreground));
}

.kpi-card__error-icon {
  width: 16px;
  height: 16px;
}

.kpi-card__retry {
  height: 26px;
  padding: 0 12px;
  border: 1px solid hsl(var(--primary) / 0.35);
  border-radius: 999px;
  background: hsl(var(--primary) / 0.06);
  color: hsl(var(--primary));
  font-size: 12px;
  cursor: pointer;
  transition: background 0.15s ease;
}

.kpi-card__retry:hover {
  background: hsl(var(--primary) / 0.12);
}

@keyframes kpi-spin {
  to {
    transform: rotate(360deg);
  }
}

@keyframes kpi-shimmer {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .sk,
  .kpi-card__refresh.is-loading :deep(svg) {
    animation: none;
  }
}
</style>
