<template>
  <div class="period-stage-container">
    <div class="period-stage-header">
      <h3>各中心营期阶段</h3>
    </div>
    
    <div class="period-stage-content">
      <div class="center-list">
        <div 
          v-for="(center, index) in periodStageData" 
          :key="index"
          class="center-item"
        >
          <div class="center-info">
            <div class="center-name">{{ center.center_name }}</div>
            <div class="center-leader">负责人：{{ center.center_leader_name }}</div>
          </div>
          <div class="stage-badge" :class="getStageClass(center.period_stage)">
            {{ center.period_stage }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getPeriodStage } from '@/api/top'

// 营期阶段数据
const periodStageData = ref([])

// 获取营期阶段数据
const fetchPeriodStageData = async () => {
  try {
    console.log('开始获取营期阶段数据...')
    const res = await getPeriodStage()
    console.log('API响应:', res)
    if (res && res.data) {
      periodStageData.value = res.data.period_stage
      console.log('数据设置成功:', periodStageData.value)
    } else {
      console.log('API响应无数据，使用默认数据')
      setDefaultData()
    }
  } catch (error) {
    console.error('获取营期阶段数据失败:', error)
    setDefaultData()
  }
}

// 设置默认数据
const setDefaultData = () => {
  periodStageData.value = [
    {
      center_name: '一中心',
      center_leader_name: '张三丰',
      period_stage: '接数据'
    },
    {
      center_name: '二中心',
      center_leader_name: '朱一航',
      period_stage: '未知阶段'
    },
    {
      center_name: '三中心',
      center_leader_name: '程琦',
      period_stage: '课1'
    }
  ]
  console.log('已设置默认数据:', periodStageData.value)
}

// 获取阶段状态样式类
const getStageClass = (stage) => {
  const stageMap = {
    '接数据': 'stage-data',
    '课1': 'stage-course1',
    '课2': 'stage-course2', 
    '课3': 'stage-course3',
    '课4': 'stage-course4',
    '休息': 'stage-rest',
    '未知阶段': 'stage-unknown'
  }
  return stageMap[stage] || 'stage-default'
}

// 组件挂载时获取数据
onMounted(() => {
  fetchPeriodStageData()
})
</script>

<style scoped>
.period-stage-container {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.period-stage-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 16px;
  border-bottom: 1px solid #f0f0f0;
}

.period-stage-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.stage-legend {
  display: flex;
  gap: 8px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #666;
}

.legend-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.legend-dot.active {
  background-color: #1890ff;
}

.legend-dot.completed {
  background-color: #52c41a;
}

.legend-dot.pending {
  background-color: #d9d9d9;
}

.period-stage-content {
  margin-top: 16px;
  max-height: 250px;
  overflow-y: auto;
}

.center-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.center-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px;
  background: #f8fafc;
  border-radius: 8px;
  border: 1px solid #e2e8f0;
  transition: all 0.2s ease;
}

.center-item:hover {
  background: #f1f5f9;
  border-color: #cbd5e1;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.center-info {
  flex: 1;
}

.center-name {
  font-size: 16px;
  font-weight: 600;
  color: #1e293b;
  margin-bottom: 4px;
}

.center-leader {
  font-size: 14px;
  color: #64748b;
}

.stage-badge {
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  text-align: center;
  min-width: 80px;
}

.stage-data {
  background: #dbeafe;
  color: #1e40af;
}

.stage-course1 {
  background: #dcfce7;
  color: #166534;
}

.stage-course2 {
  background: #fef3c7;
  color: #92400e;
}

.stage-course3 {
  background: #fed7d7;
  color: #c53030;
}

.stage-course4 {
  background: #e9d5ff;
  color: #7c3aed;
}

.stage-rest {
  background: #f3f4f6;
  color: #6b7280;
}

.stage-unknown {
  background: #fecaca;
  color: #dc2626;
}

.stage-default {
  background: #e5e7eb;
  color: #374151;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .period-stage-container {
    padding: 16px;
  }
  
  .period-stage-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
  
  .stage-legend {
    gap: 12px;
  }
  
  .stage-item {
    padding-left: 32px;
    margin-bottom: 24px;
  }
  
  .stage-timeline::before {
    left: 10px;
  }
  
  .stage-dot {
    left: 6px;
  }
}
</style>