<template>
  <div class="chart-container">
    <div class="chart-header">
      <h3>客户迫切解决的问题</h3>
    </div>
    <div class="chart-content">
      <div class="problem-ranking">
        <div v-for="(item, index) in sortedProblemData" :key="item.name" class="ranking-item" :class="getRankingClass(index)">
          <div class="rank-number">
            <span class="rank-badge" :class="getRankBadgeClass(index)">{{ index + 1 }}</span>
          </div>
          <div class="problem-info">
            <div class="problem-name">{{ item.name }}</div>
            <div class="problem-count">{{ item.value }}次咨询</div>
          </div>
          <div class="problem-percentage">
            <span class="percentage">{{ getPercentage(item.value) }}%</span>
            <div class="progress-bar">
              <div class="progress-fill" :style="{ width: getPercentage(item.value) + '%' }"></div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue';

// 接收父组件传递的数据
const props = defineProps({
  problemRanking: {
    type: Object,
    default: () => ({})
  }
});

// 将API数据转换为组件需要的格式
const problemData = computed(() => {
  const urgentIssues = props.problemRanking.urgent_issue_consultations || {};
  return Object.entries(urgentIssues).map(([name, value]) => ({
    name,
    value
  }));
});

// 计算属性
const sortedProblemData = computed(() => {
  return [...problemData.value].sort((a, b) => b.value - a.value);
});

const totalProblemCount = computed(() => {
  return problemData.value.reduce((sum, item) => sum + item.value, 0);
});

// 排行榜相关方法
const getPercentage = (value) => {
  if (totalProblemCount.value === 0) return '0.0';
  return ((value / totalProblemCount.value) * 100).toFixed(1);
};

const getRankingClass = (index) => ({
  'rank-first': index === 0,
  'rank-second': index === 1,
  'rank-third': index === 2,
  'rank-other': index > 2
});

const getRankBadgeClass = (index) => ({
  'badge-gold': index === 0,
  'badge-silver': index === 1,
  'badge-bronze': index === 2,
  'badge-default': index > 2
});
</script>

<style lang="scss" scoped>
// 颜色变量
$slate-200: #e2e8f0;
$slate-900: #0f172a;
$white: #ffffff;

.chart-container {
  background: $white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  display: flex;
  flex-direction: column;
  height: 21.5rem !important;
  max-height: 26rem;
  // flex: 1;
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 20px 10px;
  border-bottom: 1px solid #ebeef5;
  
  h3 {
    margin: 0;
    color: $slate-900;
    font-size: 18px;
    font-weight: 600;
  }
}

.chart-content {
  padding-left: 20px;
  padding-right: 20px;
  padding-bottom: 20px;
  flex-grow: 1;
  position: relative;
  overflow-y: auto;
}

.ranking-item {
  display: flex;
  align-items: center;
  padding: 12px 0;
  
  &:not(:last-child) {
    border-bottom: 1px solid #f0f2f5;
  }
}

.rank-number .rank-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  font-weight: bold;
  font-size: 14px;
  color: $white;
  
  &.badge-gold {
    background: linear-gradient(135deg, #ffd700, #ffb300);
  }
  
  &.badge-silver {
    background: linear-gradient(135deg, #c0c0c0, #a8a8a8);
  }
  
  &.badge-bronze {
    background: linear-gradient(135deg, #cd7f32, #b8860b);
  }
  
  &.badge-default {
    background: linear-gradient(135deg, #6c757d, #495057);
  }
}

.problem-info {
  flex: 1;
  margin: 0 16px;
}

.problem-name {
  font-size: 15px;
  font-weight: 500;
  color: #212529;
  margin-bottom: 4px;
}

.problem-count {
  font-size: 13px;
  color: #6c757d;
}

.problem-percentage {
  min-width: 80px;
  text-align: right;
}

.percentage {
  font-size: 15px;
  font-weight: bold;
  color: #495057;
  margin-bottom: 6px;
  display: block;
}

.progress-bar {
  width: 100%;
  height: 6px;
  background: rgba(0, 0, 0, 0.1);
  border-radius: 3px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #007bff, #0056b3);
  border-radius: 3px;
}

.rank-first .progress-fill {
  background: linear-gradient(90deg, #ffd700, #ffb300);
}

.rank-second .progress-fill {
  background: linear-gradient(90deg, #c0c0c0, #a8a8a8);
}

.rank-third .progress-fill {
  background: linear-gradient(90deg, #cd7f32, #b8860b);
}
</style>