<template>
  <div class="team-report">
    <h2>今日团队实时战报</h2>
    <div class="report-grid">
      <div class="report-card">
        <div class="card-header">
          <span class="card-title">团队总通话 <i class="info-icon" @mouseenter="showTooltip('totalCalls', $event)" @mouseleave="hideTooltip">ⓘ</i></span>
          <span class="card-trend positive">{{ weekTotalData.week_total_call?.team_data?.current_rate_last_current || '0%' }} vs 上期</span>
        </div>
        <div class="card-value">{{ weekTotalData.week_total_call?.team_data?.total_current_week_call || 0 }} 次</div>
      </div>
      <div class="report-card">
        <div class="card-header">
          <span class="card-title">有效通话时长 <i class="info-icon" @mouseenter="showTooltip('callDuration', $event)" @mouseleave="hideTooltip">ⓘ</i></span>
          <span class="card-trend negative">{{ weekTotalData.group_call_duration?.group_data?.current_rate_last_current || '0%' }} vs 上期</span>
        </div>
        <div class="card-value">{{ formatDuration(weekTotalData.group_call_duration.group_data?.current_total_call_time_hour)||0 }} 小时</div>
      </div>
      <div class="report-card">
        <div class="card-header">
          <span class="card-title">新增意向客户 <i class="info-icon" @mouseenter="showTooltip('newCustomers', $event)" @mouseleave="hideTooltip">ⓘ</i></span>
          <span class="card-trend positive">{{ weekTotalData.week_add_customer_total?.team_data?.week_rate_last_week || '0%' }} vs 上期</span>
        </div>
        <div class="card-value">{{ weekTotalData.week_add_customer_total?.team_data?.total_week_add_customer || 0 }} 人</div>
      </div>
      <div class="report-card">
        <div class="card-header">
          <span class="card-title">新增成交 <i class="info-icon" @mouseenter="showTooltip('newDeals', $event)" @mouseleave="hideTooltip">ⓘ</i></span>
          <span class="card-trend positive">{{ weekTotalData.week_add_deal_total?.team_data?.week_rate_last_week || '0%' }} vs 上期</span>
        </div>
        <div class="card-value">{{ weekTotalData.week_add_deal_total?.team_data?.total_week_add_deal || 0 }} 单</div>
      </div>
      <div class="report-card">
        <div class="card-header">
          <span class="card-title">月度总业绩 <i class="info-icon" @mouseenter="showTooltip('monthlyRevenue', $event)" @mouseleave="hideTooltip">ⓘ</i></span>
          <span class="card-trend positive">+8% vs 上月</span>
        </div>
        <div class="card-value">{{ formatCurrency(weekTotalData.week_add_fee_total?.total_add_fee || 0) }} 元</div>
      </div>
      <div class="report-card">
        <div class="card-header">
          <span class="card-title">定金转化率 <i class="info-icon" @mouseenter="showTooltip('conversionRate', $event)" @mouseleave="hideTooltip">ⓘ</i></span>
          <span class="card-trend positive">{{ weekTotalData.pay_deposit_to_money_rate?.team_data?.week_vs_last_week || '0%' }} vs 上期</span>
        </div>
        <div class="card-value">{{ weekTotalData.pay_deposit_to_money_rate?.team_data?.week_pay_deposit_to_money_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 { watch, reactive } from 'vue'
import Tooltip from '@/components/Tooltip.vue'

// 定义props
const props = defineProps({
  weekTotalData: {
    type: Object,
    default: () => ({
      week_total_call: {},
      week_add_customer_total: {},
      week_add_deal_total: {},
      week_add_fee_total: {},
      pay_deposit_to_money_rate: {},
      group_call_duration: {}
    })
  }
})

// 监听数据变化，用于调试
watch(() => props.weekTotalData, (newData) => {
  console.log('TeamReport 收到的数据:', newData)
}, { deep: true, immediate: true })

// 格式化时长（分钟转小时）
const formatDuration = (minutes) => {
  if (!minutes) return '0'
  const hours = (minutes / 60).toFixed(1)
  return hours
}

// 格式化货币
const formatCurrency = (amount) => {
  if (!amount) return '0'
  return amount.toLocaleString()
}

// Tooltip 相关数据
const tooltip = reactive({
  visible: false,
  x: 0,
  y: 0,
  title: '',
  description: ''
})

// 指标说明配置
const metricDescriptions = {
  totalCalls: {
    title: '团队总通话',
    description: '团队所有成员在本期内的通话总次数，包括拨出和接听的所有电话。'
  },
  callDuration: {
    title: '有效通话时长',
    description: '团队所有成员有效通话的总时长，不包括未接通和短时间通话。'
  },
  newCustomers: {
    title: '新增意向客户',
    description: '本期新增的有明确购买意向的客户数量，通过沟通确认有需求的客户。'
  },
  newDeals: {
    title: '新增成交',
    description: '本期新增的成交订单数量，已确认付款或签约的客户订单。'
  },
  monthlyRevenue: {
    title: '月度总业绩',
    description: '本月团队累计完成的销售业绩总额，包括所有已确认的订单金额。'
  },
  conversionRate: {
    title: '定金转化率',
    description: '支付定金的客户数 ÷ 意向客户总数'
  }
}

// Tooltip 相关方法
const showTooltip = (metricType, event) => {
  const description = metricDescriptions[metricType]
  if (description) {
    tooltip.title = description.title
    tooltip.description = description.description
    tooltip.x = event.clientX + 10
    tooltip.y = event.clientY - 10
    tooltip.visible = true
  }
}

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

<style lang="scss" scoped>
// Team Report
.team-report {
  background: white;
  border-radius: 12px;
  padding: 1.5rem;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  
  h2 {
    font-size: 1.2rem;
    font-weight: 600;
    color: #1e293b;
    margin: 0 0 1.5rem 0;
  }
  
  .report-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1rem;
  }
  
  .report-card {
    padding: 1rem;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 0.5rem;
    }
    
    .card-title {
      color: #64748b;
      font-size: 0.85rem;
    }
    
    .card-trend {
      font-size: 0.75rem;
      font-weight: 500;
      
      &.positive {
        color: #059669;
      }
      
      &.negative {
        color: #dc2626;
      }
    }
    
    .card-value {
      font-size: 1.5rem;
      font-weight: bold;
      color: #1e293b;
    }
  }
}

// 移动端适配
@media (max-width: 768px) {
  .team-report {
    padding: 1rem;
    
    h2 {
      font-size: 1.1rem;
      margin-bottom: 1rem;
    }
    
    .report-grid {
      grid-template-columns: repeat(2, 1fr);
      gap: 0.75rem;
    }
    
    .report-card {
      padding: 0.75rem;
      
      .card-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.25rem;
      }
      
      .card-title {
        font-size: 0.8rem;
      }
      
      .card-trend {
        font-size: 0.7rem;
      }
      
      .card-value {
        font-size: 1.25rem;
      }
    }
  }
}

@media (max-width: 480px) {
  .team-report {
    padding: 0.75rem;
    border-radius: 8px;
  }
}

// 感叹号图标样式
.info-icon {
  font-style: normal;
  color: #409eff;
  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);
  }
}

.card-title:hover .info-icon {
  opacity: 1;
}

.report-card {
  position: relative;
  transition: all 0.2s ease;
}
</style>