<template>
  <div class="center-overview">
    <div class="overview-header">
      <h2>中心整体概览</h2>
      <div class="stats-toggle">
        <button 
          :class="['toggle-btn', { active: statsMode === 'monthly' }]"
          @click="switchStatsMode('monthly')"
        >
          按月统计
        </button>
        <button 
          :class="['toggle-btn', { active: statsMode === 'period' }]"
          @click="switchStatsMode('period')"
        >
          按期统计
        </button>
      </div>
    </div>
    <div class="overview-grid">
      <div class="overview-card primary">
        <div class="card-header">
          <span class="card-title">
            中心总业绩
            <i @mouseenter="showTooltip($event, 'centerPerformance')" @mouseleave="hideTooltip">ⓘ</i>
          </span>
          <span class="card-trend positive">{{ props.overallData.CenterPerformance?.center_monthly_vs_previous_deals }} vs 上期</span>
        </div>
        <div class="card-value">{{ props.overallData.CenterPerformance.center_monthly_deal_count || '0' }} 单</div>
        <div class="card-subtitle">月目标完成率: {{ props.overallData.CenterPerformance?.center_monthly_target_completion_rate || '56%' }}</div>
      </div>
      
      <div class="overview-card">
        <div class="card-header">
          <span class="card-title">
            活跃组数
            <i @mouseenter="showTooltip($event, 'activeGroups')"
                  @mouseleave="hideTooltip">ⓘ</i>
          </span>
          <span class="card-trend stable">{{ props.overallData.TotalGroupCount?.center_total_team_count}}/{{ props.overallData.TotalGroupCount?.center_total_team_count }} 组</span>
        </div>
        <div class="card-value">{{ props.overallData.TotalGroupCount?.center_total_team_count || '5' }} 组</div>
        <div class="card-subtitle">总人数: {{ props.overallData.TotalGroupCount?.center_total_user_count || '40' }}人</div>
      </div>
      
      <div class="overview-card">
        <div class="card-header">
          <span class="card-title">
            中心转化率
            <i
                  @mouseenter="showTooltip($event, 'conversionRate')"
                  @mouseleave="hideTooltip">ⓘ</i>
          </span>
          <span class="card-trend positive">{{ props.overallData.CenterConversionRate?.center_monthly_vs_previous_deals }}vs 上期</span>
        </div>
        <div class="card-value">{{ props.overallData.CenterConversionRate?.center_conversion_rate || '5.2' }}</div>
        <div class="card-subtitle">行业平均: {{ props.overallData.CenterConversionRate?.current_user_center_conversion_rate || '4.8' }}</div>
      </div>
      
      <div class="overview-card">
        <div class="card-header">
          <span class="card-title">
            总通话次数
            <i 
                  @mouseenter="showTooltip($event, 'totalCalls')"
                  @mouseleave="hideTooltip">ⓘ</i>
          </span>
          <span class="card-trend positive">{{ props.overallData.TotalCallCount?.total_call_count_vs_yesterday}} vs 上期</span>
        </div>
        <div class="card-value">{{ props.overallData.TotalCallCount?.total_call_count || '0' }} 次</div>
        <div class="card-subtitle">有效通话: {{ props.overallData.TotalCallCount?.center_effective_call_count || '0' }}次</div>
      </div>
      
      <div class="overview-card">
        <div class="card-header">
          <span class="card-title">
            新增客户
            <i 
                  @mouseenter="showTooltip($event, 'newCustomers')"
                  @mouseleave="hideTooltip">ⓘ</i>
          </span>
          <span class="card-trend positive">{{ props.overallData.NewCustomer?.center_new_leads_vs_previous_period }} vs 上期</span>
        </div>
        <div class="card-value">{{ props.overallData.NewCustomer?.center_new_leads_count || '117' }} 人</div>
        <div class="card-subtitle">意向客户: {{ props.overallData.NewCustomer?.center_new_v_customer_count || '89' }}人</div>
      </div>
      
      <div class="overview-card">
        <div class="card-header">
          <span class="card-title">
            定金转化
            <i 
                  @mouseenter="showTooltip($event, 'depositConversion')"
                  @mouseleave="hideTooltip">ⓘ</i>
          </span>
          <span class="card-trend positive">{{ props.overallData.DepositConversionRate?.center_deposit_conversion_vs_previous }} vs 上期</span>
        </div>
        <div class="card-value">{{ props.overallData.DepositConversionRate?.center_current_deposit_conversion_rate || '0' }} </div>
        <div class="card-subtitle">平均定金转化率: {{ props.overallData.DepositConversionRate?.center_monthly_deposit_conversion_rate || '0' }}</div>
      </div>
    </div>
    
    <!-- Tooltip组件 -->
    <Tooltip 
      :visible="tooltip.visible"
      :x="tooltip.x"
      :y="tooltip.y"
      :title="tooltip.title"
      :description="tooltip.description"
    />
  
  </div>
</template>

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

// 统计模式状态
const statsMode = ref('monthly') // 默认按月统计

// 定义emit事件
const emit = defineEmits(['update-check-type'])

// 切换统计模式
const switchStatsMode = (mode) => {
  statsMode.value = mode
  // 向父组件发送事件，修改CheckType的值
  const checkTypeValue = mode === 'monthly' ? 'month' : 'period'
  emit('update-check-type', checkTypeValue)
  console.log('切换统计模式:', mode, '发送CheckType值:', checkTypeValue)
}

// 中心整体概览组件
const props = defineProps({
  overallData: {
    type: Object,
    default: () => ({
      CenterPerformance: {},
      TotalGroupCount: {},
      CenterConversionRate: {},
      TotalCallCount: {},
      NewCustomer: {},
      DepositConversionRate: {}
    })
  }
})

// Tooltip状态管理
const tooltip = reactive({
  visible: false,
  x: 0,
  y: 0,
  title: '',
  description: ''
})

// 指标描述信息
const metricDescriptions = {
  centerPerformance: {
    title: '中心总业绩计算方式',
    description: '月目标完成率 = 当月成交单数 / 月度目标单数 × 100%'
  },
  activeGroups: {
    title: '活跃组数计算方式',
    description: '本月有通话记录或成交记录的团队。总人数为所有活跃团队的人员总和'
  },
  conversionRate: {
    title: '中心转化率计算方式',
    description: '中心转化率 = 总成交客户数 / 总接触客户数 × 100%'
  },
  totalCalls: {
    title: '总通话次数计算方式',
    description: '所有销售人员的通话总次数，包括接听和拨出。有效通话指通话时长超过30秒的通话记录'
  },
  newCustomers: {
    title: '新增客户计算方式',
    description: '本期新录入系统的客户数量。意向客户指经过初步沟通，有明确购买意向的客户数量'
  },
  depositConversion: {
    title: '定金转化计算方式',
    description: '定金转化率 = 缴纳定金客户数 / 意向客户总数 × 100%'
  }
}

// 显示tooltip
const showTooltip = (event, metricType) => {
  const rect = event.target.getBoundingClientRect()
  tooltip.visible = true
  tooltip.x = rect.left + rect.width / 2
  tooltip.y = rect.top - 10
  tooltip.title = metricDescriptions[metricType].title
  tooltip.description = metricDescriptions[metricType].description
}

// 隐藏tooltip
const hideTooltip = () => {
  tooltip.visible = false
}
</script>

<style lang="scss" scoped>
.center-overview {
  background: white;
  border-radius: 12px;
  padding: 0.5rem;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  
  h2 {
    font-size: 1.3rem;
    font-weight: 600;
    color: #1e293b;
    margin: 0 0 1.5rem 0;
  }
  
  .overview-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1rem;
    margin-bottom: 0;
  }
  
  .overview-card {
    padding: 1.2rem;
    border: 1px solid #e2e8f0;
    border-radius: 10px;
    transition: all 0.3s ease;
    
    &:hover {
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
      transform: translateY(-2px);
    }
    
    &.primary {
      background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
      color: white;
      border: none;
      
      .card-title, .card-subtitle {
        color: rgba(255, 255, 255, 0.9);
      }
      
      .card-trend {
        color: rgba(255, 255, 255, 0.8);
      }
      
      .card-value {
        color: white;
      }
    }
    
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      // padding: 1rem;
      margin-bottom: 0.5rem;
    }
    
    .card-title {
      color: #64748b;
      font-size: 0.9rem;
      font-weight: 500;
    }
    
    .card-trend {
      font-size: 0.75rem;
      font-weight: 600;
      
      &.positive {
        color: #059669;
      }
      
      &.negative {
        color: #dc2626;
      }
      
      &.stable {
        color: #7c3aed;
      }
    }
    
    .card-value {
      font-size: 1.8rem;
      font-weight: bold;
      color: #1e293b;
      margin-bottom: 0.25rem;
    }
    
    .card-subtitle {
      color: #94a3b8;
      font-size: 0.8rem;
    }
    
    .info-icon {
      display: inline-block;
      width: 16px;
      height: 16px;
      background: #409eff;
      color: white;
      border-radius: 50%;
      text-align: center;
      line-height: 16px;
      font-size: 12px;
      font-weight: bold;
      margin-left: 6px;
      cursor: pointer;
      opacity: 0.8;
      transition: all 0.3s ease;
      
      &:hover {
        opacity: 1;
        transform: scale(1.1);
        background: #66b3ff;
      }
    }
    
    // 主要卡片中的图标样式
    &.primary .info-icon {
      background: rgba(255, 255, 255, 0.3);
      color: white;
      
      &:hover {
        background: rgba(255, 255, 255, 0.5);
      }
    }
  }
  
  .trend-section {
    h3 {
      font-size: 1.1rem;
      font-weight: 600;
      color: #1e293b;
      margin: 0 0 1rem 0;
    }
    
    .trend-charts {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      gap: 1rem;
    }
    
    .trend-item {
      display: flex;
      align-items: center;
      gap: 1rem;
      padding: 0.75rem;
      background: #f8fafc;
      border-radius: 8px;
      
      .trend-label {
        font-size: 0.85rem;
        color: #64748b;
        min-width: 80px;
      }
      
      .trend-bar {
        flex: 1;
        height: 6px;
        background: #e2e8f0;
        border-radius: 3px;
        overflow: hidden;
        
        .trend-fill {
          height: 100%;
          background: linear-gradient(90deg, #10b981, #059669);
          border-radius: 3px;
          transition: width 0.3s ease;
        }
      }
      
      .trend-value {
        font-size: 0.8rem;
        font-weight: 600;
        color: #059669;
        min-width: 40px;
        text-align: right;
      }
    }
  }
}

// 切换按钮样式
.overview-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
  
  h2 {
    margin: 0;
    color: #1e293b;
    font-size: 1.5rem;
    font-weight: 600;
  }
  
  .stats-toggle {
    display: flex;
    background: #f1f5f9;
    border-radius: 8px;
    padding: 4px;
    gap: 2px;
    
    .toggle-btn {
      padding: 8px 16px;
      border: none;
      background: transparent;
      color: #64748b;
      font-size: 0.875rem;
      font-weight: 500;
      border-radius: 6px;
      cursor: pointer;
      transition: all 0.2s ease;
      
      &:hover {
        background: #e2e8f0;
        color: #475569;
      }
      
      &.active {
        background: #ffffff;
        color: #0f172a;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
      }
    }
  }
}

// 移动端适配
@media (max-width: 768px) {
  .overview-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 1rem;
    
    .stats-toggle {
      align-self: stretch;
      
      .toggle-btn {
        flex: 1;
        text-align: center;
      }
    }
  }
  
  .center-overview {
    padding: 1rem;
    
    .overview-grid {
      grid-template-columns: repeat(2, 1fr);
      gap: 0.75rem;
    }
    
    .overview-card {
      padding: 1rem;
      
      .card-value {
        font-size: 1.5rem;
      }
    }
    
    .trend-charts {
      grid-template-columns: 1fr;
    }
  }
}

@media (max-width: 480px) {
  .center-overview {
    .overview-grid {
      grid-template-columns: 1fr;
    }
  }
}
</style>