<template>
  <div class="stat-card kpi-card">
    <h3 class="card-title">统计指标</h3>
    <div class="kpi-grid stats-grid-inner">
      <!-- 所有 .kpi-item 保持不变 -->
      <div class="kpi-item stat-item" >
        <div class="stat-icon customer-rate"><i class="el-icon-chat-dot-round"></i></div>
        <div class="kpi-value">{{ customerCommunicationRate }}</div>
        <p>活跃客户沟通率 <i class="info-icon" @mouseenter="showTooltip('customerCommunicationRate', $event)" @mouseleave="hideTooltip">ⓘ</i></p>
      </div>
      <div class="kpi-item stat-item" >
        <div class="stat-icon response-time"><i class="el-icon-timer"></i></div>
        <div class="kpi-value">{{ averageResponseTime }}<span class="kpi-unit">分钟</span></div>
        <p>平均应答时间 <i class="info-icon" @mouseenter="showTooltip('averageResponseTime', $event)" @mouseleave="hideTooltip">ⓘ</i></p>
      </div>
      <div class="kpi-item stat-item" >
        <div class="stat-icon timeout-rate"><i class="el-icon-warning"></i></div>
        <div class="kpi-value">{{ timeoutResponseRate }}</div>
        <p>超时应答率 <i class="info-icon" @mouseenter="showTooltip('timeoutResponseRate', $event)" @mouseleave="hideTooltip">ⓘ</i></p>
      </div>
      <div class="kpi-item stat-item">
        <div class="stat-icon severe-timeout-rate"><i class="el-icon-warning-outline"></i></div>
        <div class="kpi-value">{{ severeTimeoutRate }}</div>
        <p>严重超时应答率 <i class="info-icon" @mouseenter="showTooltip('severeTimeoutRate', $event)" @mouseleave="hideTooltip">ⓘ</i></p>
      </div>
      <div class="kpi-item stat-item">
        <div class="stat-icon form-rate"><i class="el-icon-document"></i></div>
        <div class="kpi-value">{{ formCompletionRate }}</div>
        <p>表格填写率 <i class="info-icon" @mouseenter="showTooltip('formCompletionRate', $event)" @mouseleave="hideTooltip">ⓘ</i></p>
      </div>
    </div>
    
    <!-- 指标说明 Tooltip -->
    <Tooltip
      :visible="tooltip.visible"
      :x="tooltip.x"
      :y="tooltip.y"
      :title="tooltip.title"
      :description="tooltip.description"
    />
  </div>
</template>

<script setup>
import { defineProps, reactive } from 'vue';
import Tooltip from '@/components/Tooltip.vue';

defineProps({
  customerCommunicationRate: { type: Number, default: 0 },
  averageResponseTime: { type: Number, default: 0 },
  timeoutResponseRate: { type: Number, default: 0 },
  severeTimeoutRate: { type: Number, default: 0 },
  formCompletionRate: { type: Number, default: 0 }
});

const tooltip = reactive({
  visible: false, // 确保初始值为 false
  x: 0,
  y: 0,
  title: '',
  description: ''
});

const statDescriptions = {
  customerCommunicationRate: { title: '活跃客户沟通率', description: '活跃沟通客户数 ÷ 总客户数' },
  averageResponseTime: { title: '平均应答时间', description: '总应答时间 ÷ 应答次数' },
  timeoutResponseRate: { title: '超时应答率', description: '超时应答次数 ÷ 总应答次数' },
  severeTimeoutRate: { title: '严重超时应答率', description: '严重超时应答次数 ÷ 总应答次数' },
  formCompletionRate: { title: '表格填写率', description: '已填写表格数 ÷ 总表格数' }
};

const showTooltip = (statKey, event) => {
  console.log('Mouse entered! Firing showTooltip for:', statKey);
  const description = statDescriptions[statKey];
  if (description) {
    tooltip.visible = true;
    tooltip.x = event.clientX + 10;
    tooltip.y = event.clientY + 15;
    tooltip.title = description.title;
    tooltip.description = description.description;
    console.log('Tooltip state updated:', JSON.parse(JSON.stringify(tooltip))); // 使用 JSON 序列化来查看快照
  }
};

const hideTooltip = () => {
  console.log('Mouse left! Hiding tooltip.');
  tooltip.visible = false;
};
</script>

<style lang="scss" scoped>
</style>

<style lang="scss" scoped>
// --- 颜色和变量定义 ---
$slate-50: #f8fafc;
$slate-100: #f1f5f9;
$slate-200: #e2e8f0;
$slate-700: #334155;
$slate-800: #1e293b;
$slate-900: #303133;
$gray-400: #909399;
$gray-600: #606266;
$white: #ffffff;

.stat-card {
  background: $white;
  padding: 24px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  display: flex;
  flex-direction: column;
  align-items: stretch;
}

.card-title {
  font-size: 18px;
  font-weight: 600;
  color: $slate-900;
  margin: -10px 0 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #ebeef5;
}

// 统计指标卡片特定样式
.stats-grid-inner {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  gap: 0.75rem;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 120px;
  padding: 1rem 0.5rem;
  text-align: center;
  background-color: $slate-50;
  border-radius: 0.5rem;
  border: 1px solid $slate-200;
  
  .stat-icon {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    color: $white;
    margin-bottom: 0.75rem;
    
    &.customer-rate { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); }
    &.response-time { background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); }
    &.timeout-rate { background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%); }
    &.form-rate { background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%); }
    &.severe-timeout-rate { background: linear-gradient(135deg, #ff6b6b 0%, #ee5a52 100%); }
  }
  
  .kpi-value {
    font-size: 1.5rem;
    font-weight: bold;
    color: $slate-800;
    margin-bottom: 0.25rem;
  }
  
  .kpi-unit {
    font-size: 0.875rem;
    font-weight: normal;
    color: $gray-400;
    margin-left: 2px;
  }
  
  p {
    font-size: 0.875rem;
    color: $gray-600;
    margin: 0.25rem 0 0;
  }
}

// Info 图标样式
.info-icon {
  font-size: 12px;
  margin-left: 4px;
  opacity: 0.7;
  cursor: pointer;
  transition: all 0.3s ease;
  
  &:hover {
    opacity: 1;
    color: #007bff;
    transform: scale(1.2);
  }
}

// Tooltip 样式
.stat-tooltip {
  position: fixed;
  z-index: 9999;
  background: rgba(0, 0, 0, 0.9);
  color: white;
  padding: 12px 16px;
  border-radius: 8px;
  font-size: 14px;
  max-width: 300px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  pointer-events: none;
  
  .tooltip-title {
    font-weight: 600;
    margin-bottom: 4px;
    color: #fff;
  }
  
  .tooltip-description {
    font-size: 13px;
    color: #e0e0e0;
    line-height: 1.4;
  }
}

// 统计项悬停效果
.stat-item {
  cursor: pointer;
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-2px);

    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }
}

// --- 响应式设计 ---
@media (max-width: 768px) {
  .stats-grid-inner {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.5rem;
  }
  
  .stat-item {
    min-height: 100px;
    padding: 0.75rem 0.25rem;
    
    .stat-icon {
      width: 32px;
      height: 32px;
      font-size: 16px;
      margin-bottom: 0.5rem;
    }
    
    .kpi-value {
      font-size: 1.125rem;
    }
    
    p {
      font-size: 0.75rem;
    }
  }
}

/* 小屏幕优化 */
@media (max-width: 480px) {
  .stats-grid-inner {
    grid-template-columns: 1fr;
    gap: 0.75rem;
  }
  
  .stat-item {
    min-height: 80px;
    padding: 1rem;
    flex-direction: row;
    text-align: left;
    
    .stat-icon {
      margin-bottom: 0;
      margin-right: 0.75rem;
    }
  }
}
</style>