<template>
  <div class="team-plan-card">
    <div class="team-plan-card__bar" />
    <div class="team-plan-card__header">
      <div class="team-plan-card__lead">
        <span
          class="team-plan-card__lead-avatar"
          :style="leadColor ? { background: leadColor } : undefined"
        >
          {{ leadAvatar }}
        </span>
        <span class="team-plan-card__label">
          {{ leadName }}<span class="team-plan-card__label-suffix">· 专家团计划</span>
        </span>
      </div>
      <span class="team-plan-card__count">{{ completedCount }}/{{ phases.length }} 阶段完成</span>
    </div>

    <div v-if="phases.length > 0" class="team-plan-card__body">
      <div
        v-for="phase in phases"
        :key="phase.id"
        class="team-plan-card__phase"
        :class="`team-plan-card__phase--${phase.status}`"
      >
        <span class="team-plan-card__phase-dot" aria-hidden="true">
          {{ statusIcon(phase.status) }}
        </span>
        <div class="team-plan-card__phase-info">
          <span class="team-plan-card__phase-name" :title="phase.task_description || phase.name">
            {{ phase.name }}
          </span>
          <span class="team-plan-card__phase-expert">{{ phase.assigned_expert }}</span>
        </div>
        <span class="team-plan-card__phase-status">{{ statusLabel(phase.status) }}</span>
      </div>
    </div>

    <div class="team-plan-card__footer">
      <div class="team-plan-card__progress">
        <div
          class="team-plan-card__progress-fill"
          :style="{ width: `${progressPercent}%` }"
        />
      </div>
      <span class="team-plan-card__progress-text">
        当前: {{ currentPhaseName || '—' }} / {{ phases.length }} 阶段
      </span>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import type { ITeamPlanPhase } from '@/api/types'

interface Props {
  phases: ITeamPlanPhase[]
  leadName?: string
  leadAvatar?: string
  leadColor?: string
}

const props = withDefaults(defineProps<Props>(), {
  leadName: 'Lead',
  leadAvatar: '🧑‍💼',
})

const completedCount = computed(() => props.phases.filter((p) => p.status === 'completed').length)

const progressPercent = computed(() => {
  if (props.phases.length === 0) return 0
  return Math.min((completedCount.value / props.phases.length) * 100, 100)
})

const currentPhaseName = computed(() => {
  const running = props.phases.find((p) => p.status === 'in_progress')
  if (running) return running.name
  const pending = props.phases.find((p) => p.status === 'pending')
  if (pending) return pending.name
  return props.phases[props.phases.length - 1]?.name
})

function statusLabel(status: string): string {
  const labels: Record<string, string> = {
    pending: '待执行',
    in_progress: '执行中',
    completed: '已完成',
    failed: '失败',
  }
  return labels[status] || status
}

function statusIcon(status: string): string {
  const icons: Record<string, string> = {
    pending: '○',
    in_progress: '●',
    completed: '✓',
    failed: '✕',
  }
  return icons[status] || '○'
}
</script>

<style scoped>
.team-plan-card {
  width: 100%;
  background: var(--bg-primary);
  border: 1px solid var(--accent-team-soft);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-card);
  overflow: hidden;
}

.team-plan-card__bar {
  height: 4px;
  background: var(--accent-team);
}

.team-plan-card__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border-color);
}

.team-plan-card__lead {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.team-plan-card__lead-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: var(--radius-full);
  font-size: 12px;
  background: var(--accent-team-soft);
}

.team-plan-card__label {
  font-size: var(--font-sm);
  font-weight: var(--font-weight-medium);
  color: var(--text-primary);
}

.team-plan-card__label-suffix {
  margin-left: var(--space-1);
  color: var(--text-tertiary);
  font-weight: var(--font-weight-normal);
}

.team-plan-card__count {
  font-size: var(--font-xs);
  color: var(--text-tertiary);
}

.team-plan-card__body {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
}

.team-plan-card__phase {
  display: grid;
  grid-template-columns: 20px 1fr 60px;
  gap: var(--space-2);
  align-items: center;
  font-size: var(--font-sm);
  color: var(--text-secondary);
}

.team-plan-card__phase-dot {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  font-size: 10px;
  border-radius: var(--radius-full);
  color: var(--text-tertiary);
}

.team-plan-card__phase--in_progress .team-plan-card__phase-dot {
  color: var(--accent-team);
}

.team-plan-card__phase--completed .team-plan-card__phase-dot {
  color: var(--color-success);
}

.team-plan-card__phase--failed .team-plan-card__phase-dot {
  color: var(--color-error);
}

.team-plan-card__phase-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.team-plan-card__phase-name {
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.team-plan-card__phase-expert {
  font-family: var(--font-mono);
  font-size: var(--font-xs);
  color: var(--text-tertiary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.team-plan-card__phase-status {
  font-size: var(--font-xs);
  color: var(--text-tertiary);
  text-align: right;
}

.team-plan-card__phase--in_progress .team-plan-card__phase-status {
  color: var(--accent-team);
}

.team-plan-card__phase--completed .team-plan-card__phase-status {
  color: var(--color-success);
}

.team-plan-card__phase--failed .team-plan-card__phase-status {
  color: var(--color-error);
}

.team-plan-card__footer {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-4) var(--space-3);
  border-top: 1px solid var(--border-color);
}

.team-plan-card__progress {
  flex: 1;
  height: 6px;
  background: var(--bg-tertiary);
  border-radius: var(--radius-full);
  overflow: hidden;
}

.team-plan-card__progress-fill {
  height: 100%;
  background: var(--accent-team);
  border-radius: var(--radius-full);
  transition: width 0.3s ease;
}

.team-plan-card__progress-text {
  font-size: var(--font-xs);
  color: var(--text-tertiary);
  white-space: nowrap;
}
</style>
