<template>
  <div class="path-optimizer-panel">
    <div class="optimizer-header">
      <h3>路径优化</h3>
    </div>
    <div v-if="optimizations.length === 0" class="optimizer-empty">
      <a-empty description="暂无优化记录" :image-style="{ height: '40px' }" />
    </div>
    <div v-else class="optimizer-list">
      <div
        v-for="opt in optimizations"
        :key="opt.id"
        class="optimizer-item"
      >
        <div class="optimizer-item__header" @click="toggleExpand(opt.id)">
          <div class="optimizer-item__info">
            <span class="optimizer-item__type">{{ opt.task_type }}</span>
            <a-tag v-if="opt.improvement > 0" color="green" size="small">
              改善 {{ (opt.improvement * 100).toFixed(1) }}%
            </a-tag>
          </div>
          <span class="optimizer-item__time">
            {{ formatTime(opt.updated_at) }}
          </span>
        </div>
        <div v-if="expandedId === opt.id" class="optimizer-item__detail">
          <div v-if="opt.previous_path.length > 0" class="path-comparison">
            <div class="path-section">
              <div class="path-label">优化前路径</div>
              <div class="path-steps">
                <div v-for="(step, i) in opt.previous_path" :key="'prev-' + i" class="path-step path-step--old">
                  {{ i + 1 }}. {{ step }}
                </div>
              </div>
            </div>
            <div class="path-arrow">→</div>
            <div class="path-section">
              <div class="path-label">优化后路径</div>
              <div class="path-steps">
                <div v-for="(step, i) in opt.current_path" :key="'curr-' + i" class="path-step path-step--new">
                  {{ i + 1 }}. {{ step }}
                </div>
              </div>
            </div>
          </div>
          <div v-else-if="opt.current_path.length > 0" class="path-current">
            <div class="path-label">当前推荐路径</div>
            <div class="path-steps">
              <div v-for="(step, i) in opt.current_path" :key="'rec-' + i" class="path-step path-step--new">
                {{ i + 1 }}. {{ step }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { Tag as ATag, Empty as AEmpty } from 'ant-design-vue'
import type { PathOptimization } from '@/api/evolution'

const props = defineProps<{
  optimizations: PathOptimization[]
}>()

const expandedId = ref<string | null>(null)

function toggleExpand(id: string) {
  expandedId.value = expandedId.value === id ? null : id
}

function formatTime(isoStr: string | null): string {
  if (!isoStr) return ''
  const d = new Date(isoStr)
  return `${d.getMonth() + 1}/${d.getDate()} ${d.getHours().toString().padStart(2, '0')}:${d.getMinutes().toString().padStart(2, '0')}`
}
</script>

<style scoped>
.path-optimizer-panel {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.optimizer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.optimizer-header h3 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
}

.optimizer-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.optimizer-list {
  flex: 1;
  overflow-y: auto;
}

.optimizer-item {
  background: #fafafa;
  border: 1px solid #f0f0f0;
  border-radius: 6px;
  padding: 10px 12px;
  margin-bottom: 8px;
}

.optimizer-item__header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
}

.optimizer-item__info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.optimizer-item__type {
  font-size: 13px;
  font-weight: 500;
  color: #1890ff;
}

.optimizer-item__time {
  font-size: 12px;
  color: #8c8c8c;
}

.optimizer-item__detail {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid #f0f0f0;
}

.path-comparison {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.path-arrow {
  font-size: 16px;
  color: #52c41a;
  padding-top: 20px;
  flex-shrink: 0;
}

.path-section {
  flex: 1;
  min-width: 0;
}

.path-current {
  width: 100%;
}

.path-label {
  font-size: 12px;
  font-weight: 600;
  color: #595959;
  margin-bottom: 4px;
}

.path-steps {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.path-step {
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 3px;
  line-height: 1.5;
}

.path-step--old {
  background: #fff2f0;
  color: #cf1322;
}

.path-step--new {
  background: #f6ffed;
  color: #389e0d;
}
</style>
