<script setup lang="ts">
import { ref } from 'vue'
import type { PlanStepInfo } from '../types'

defineProps<{
  steps: PlanStepInfo[]
}>()

const emit = defineEmits<{
  quote: [text: string]
}>()

const expandedSteps = ref<Set<number>>(new Set())

function toggleStep(order: number) {
  if (expandedSteps.value.has(order)) {
    expandedSteps.value.delete(order)
  } else {
    expandedSteps.value.add(order)
  }
}

function statusIcon(status?: string) {
  switch (status) {
    case 'done': return '✓'
    case 'running': return '⟳'
    case 'failed': return '✗'
    default: return '○'
  }
}

function quoteStep(e: Event, step: PlanStepInfo) {
  e.stopPropagation()
  emit('quote', `[步骤${step.order}] ${step.description}`)
}
</script>

<template>
  <div class="plan-section">
    <div class="section-header">
      <h2>计划</h2>
    </div>
    <div class="steps-list">
      <div
        v-for="step in steps"
        :key="step.order"
        class="step-item"
        :class="step.status || 'pending'"
      >
        <div class="step-header" @click="step.command ? toggleStep(step.order) : undefined">
          <span class="step-icon">{{ statusIcon(step.status) }}</span>
          <span class="step-order">{{ step.order }}.</span>
          <span class="step-title">{{ step.description }}</span>
          <button class="quote-btn" @click="quoteStep($event, step)" title="引用到反馈">
            <svg width="12" height="12" viewBox="0 0 16 16" fill="currentColor"><path d="M6.32 3.2A5.6 5.6 0 0 0 1.6 8.8v3.6h3.6a2.4 2.4 0 0 0 2.4-2.4 2.4 2.4 0 0 0-2.4-2.4H3.84A3.36 3.36 0 0 1 6.32 3.2ZM14.4 3.2A5.6 5.6 0 0 0 9.68 8.8v3.6h3.6a2.4 2.4 0 0 0 2.4-2.4 2.4 2.4 0 0 0-2.4-2.4h-1.36A3.36 3.36 0 0 1 14.4 3.2Z"/></svg>
          </button>
          <span v-if="step.command" class="step-toggle">{{ expandedSteps.has(step.order) ? '▾' : '▸' }}</span>
        </div>
        <div v-if="step.command && expandedSteps.has(step.order)" class="step-detail">
          {{ step.command }}
        </div>
      </div>
      <div v-if="!steps.length" class="empty-state">
        AI 将在这里展示执行计划
      </div>
    </div>
  </div>
</template>

<style scoped>
.plan-section {
  flex: 1;
  background: var(--bg-card);
  border-radius: 8px;
  padding: 12px 16px;
  border: 1px solid var(--border);
  overflow-y: auto;
  min-width: 0;
}

.section-header {
  margin-bottom: 12px;
}

.section-header h2 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

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

.step-item {
  border-radius: 6px;
  background: var(--bg-secondary);
  overflow: hidden;
}

.step-item.done { border-left: 3px solid var(--success); }
.step-item.running { border-left: 3px solid var(--accent); background: rgba(79, 195, 247, 0.08); }
.step-item.failed { border-left: 3px solid var(--error); }
.step-item.pending { border-left: 3px solid var(--pending); opacity: 0.7; }

.step-header {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 8px 10px;
  font-size: 13px;
  line-height: 1.5;
  cursor: default;
}

.step-header:has(.step-toggle) {
  cursor: pointer;
}

.step-header:has(.step-toggle):hover {
  background: var(--bg-tertiary);
}

.step-icon {
  font-size: 14px;
  flex-shrink: 0;
  width: 18px;
  text-align: center;
}

.step-item.done .step-icon { color: var(--success); }
.step-item.running .step-icon { color: var(--accent); }
.step-item.failed .step-icon { color: var(--error); }

.step-order {
  color: var(--text-secondary);
  flex-shrink: 0;
}

.step-title {
  color: var(--text-primary);
  font-weight: 500;
  flex: 1;
}

.quote-btn {
  flex-shrink: 0;
  width: 22px;
  height: 22px;
  padding: 0;
  border: none;
  background: none;
  color: var(--text-secondary);
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.15s;
}

.step-header:hover .quote-btn {
  opacity: 0.5;
}

.quote-btn:hover {
  opacity: 1 !important;
  background: rgba(79, 195, 247, 0.15);
  color: var(--accent);
}

.step-toggle {
  color: var(--text-secondary);
  font-size: 11px;
  flex-shrink: 0;
}

.step-detail {
  padding: 6px 10px 10px 44px;
  font-size: 12px;
  line-height: 1.6;
  color: var(--text-secondary);
  border-top: 1px solid var(--border);
}

.empty-state {
  color: var(--text-secondary);
  font-size: 13px;
  text-align: center;
  padding: 24px;
}
</style>
