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

import { Card, Empty, Progress, Tag } from 'ant-design-vue';

import type { SalespersonDashboardStoreAmountItem } from '../api';

const props = defineProps<{
  title: string;
  subtitle?: string;
  items: SalespersonDashboardStoreAmountItem[];
  showPromoter?: boolean;
  emptyText?: string;
}>();

const maxValue = computed(() => {
  if (!props.items.length) return 0;
  return Math.max(...props.items.map((item) => Number(item.amount || 0)));
});

function formatValue(item: SalespersonDashboardStoreAmountItem) {
  return `¥${Number(item.amount || 0).toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
}

function progressPercent(item: SalespersonDashboardStoreAmountItem) {
  if (maxValue.value <= 0) return 0;
  return Math.round((Number(item.amount || 0) / maxValue.value) * 100);
}

function rankClass(index: number) {
  if (index === 0) return 'ranking-item__badge--gold';
  if (index === 1) return 'ranking-item__badge--silver';
  if (index === 2) return 'ranking-item__badge--bronze';
  return 'ranking-item__badge--normal';
}
</script>

<template>
  <Card :title="title" class="ranking-card">
    <template v-if="subtitle" #extra>
      <span class="ranking-card__subtitle">{{ subtitle }}</span>
    </template>

    <Empty v-if="!items.length" :description="emptyText || '暂无数据'" />

    <div v-else class="ranking-card__list">
      <div
        v-for="(item, index) in items"
        :key="item.store_id"
        class="ranking-item"
      >
        <div class="ranking-item__badge" :class="rankClass(index)">
          {{ index + 1 }}
        </div>

        <div class="ranking-item__main">
          <div class="ranking-item__header">
            <div class="ranking-item__store">
              <div class="ranking-item__name">{{ item.name }}</div>
              <div class="ranking-item__meta">
                <Tag size="small">{{ item.type_label }}</Tag>
                <span v-if="showPromoter && item.promoter_nick_name" class="ranking-item__promoter">
                  {{ item.promoter_nick_name }}
                </span>
              </div>
            </div>
            <div class="ranking-item__value">{{ formatValue(item) }}</div>
          </div>
          <Progress
            :percent="progressPercent(item)"
            :show-info="false"
            stroke-color="#1677ff"
            :stroke-width="6"
          />
        </div>
      </div>
    </div>
  </Card>
</template>

<style scoped>
.ranking-card {
  height: 100%;
}

.ranking-card__subtitle {
  max-width: 280px;
  font-size: 12px;
  font-weight: normal;
  color: #86909c;
  text-align: right;
}

.ranking-card__list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.ranking-item {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  padding: 12px;
  background: #fafafa;
  border-radius: 10px;
  transition: background-color 0.2s ease;
}

.dark .ranking-item {
  background: #1f1f1f;
}

.ranking-item:hover {
  background: #f0f5ff;
}

.dark .ranking-item:hover {
  background: #111a2c;
}

.ranking-item__badge {
  display: flex;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  font-size: 13px;
  font-weight: 700;
  color: #fff;
  border-radius: 50%;
}

.ranking-item__badge--gold {
  background: linear-gradient(135deg, #ffd666, #fa8c16);
}

.ranking-item__badge--silver {
  background: linear-gradient(135deg, #f0f0f0, #bfbfbf);
  color: #595959;
}

.ranking-item__badge--bronze {
  background: linear-gradient(135deg, #ffbb96, #d4380d);
}

.ranking-item__badge--normal {
  background: #d9d9d9;
  color: #595959;
}

.ranking-item__main {
  flex: 1;
  min-width: 0;
}

.ranking-item__header {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 8px;
}

.ranking-item__store {
  min-width: 0;
}

.ranking-item__name {
  margin-bottom: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 14px;
  font-weight: 600;
  color: #1d2129;
  white-space: nowrap;
}

.dark .ranking-item__name {
  color: #f3f4f6;
}

.ranking-item__meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}

.ranking-item__promoter {
  font-size: 12px;
  color: #86909c;
}

.ranking-item__value {
  font-size: 15px;
  font-weight: 700;
  color: #1677ff;
  white-space: nowrap;
}
</style>
