<template>
  <div class="step-container">
    <div v-if="props.loading" class="loading-wrapper flex-display-center">
      <span class="text-tip-2 text-primary-50-clor">预测分析中...</span>
    </div>
    <div v-else-if="predictionData" class="content-wrapper">
      <div class="header-section flex-display-start">
        <div class="header-left flex-display-start">
        
          <div>
            <div class="text-title-2-bold">
              <img src="../assets/forecast.svg" />
              {{ predictionData?.title }}</div>
            <div class="text-tip-2 text-primary-50-clor" style="margin-left: 21px;">{{ predictionData?.subtitle }}</div>
          </div>
        </div>
        <div class="header-right">
          <span class="risk-label" :class="overallRiskColorClass">{{ overallRiskLabel }}</span>
          <div class="progress-bar-container">
            <div 
              v-for="i in 3" 
              :key="i" 
              class="progress-segment"
              :class="getOverallSegmentClass(i)"
            ></div>
          </div>
          <p class="estimated-days text-tip-2 text-primary-50-clor">{{ predictionData?.overallEstimatedDays }}</p>
        </div>
      </div>
      <div class="highlight-box text-regular">
        <div class="highlight-wave text-regular"></div>
        <div class="highlight-content text-regular">
          <p class="highlight-title text-regular">该法案的通过概率为 {{ predictionData?.overallProbability || '—' }}。</p>
          <p class="highlight-text text-regular">{{ predictionData?.highlightText }}</p>
        </div>
      </div>
      <div v-if="predictionData?.phases?.length" class="phases-list">
        <PredictionPhaseCard 
          v-for="phase in predictionData.phases" 
          :key="phase.id" 
          :phase="phase" 
        />
      </div>
    </div>
    <div class="action-footer flex-display-start">
      <button class="btn-text flex-display-center" @click="handleViewHistory">
        <svg class="icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
          <line x1="19" y1="12" x2="5" y2="12" />
          <polyline points="12 19 5 12 12 5" />
        </svg>
        <span>查看预测基于的历史法案</span>
      </button>
      <button class="btn-primary flex-display-center" @click="handleRepredict">
        <svg class="icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
          <polyline points="23 4 23 10 17 10" />
          <path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10" />
        </svg>
        <span>重新预测</span>
      </button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import type { PredictionAnalysis } from '../api'
import PredictionPhaseCard from './PredictionPhaseCard.vue'

const props = defineProps<{
  data?: PredictionAnalysis | null
  loading?: boolean
}>()

const emit = defineEmits<{
  (e: 'prev'): void
  (e: 'repredict'): void
}>()

// 预测分析数据 - 从 props 获取，暂无真实接口
const predictionData = computed(() => props.data || null)

// 根据索引和progressLevel返回进度条格子的类名
function getOverallSegmentClass(index: number) {
  const level = predictionData.value?.overallProgressLevel || 0
  const isActive = index <= level
  
  if (!isActive) {
    return 'segment-inactive'
  }
  
  switch (predictionData.value?.overallRisk) {
    case 'high':
      return 'segment-high'
    case 'medium':
      return 'segment-medium'
    case 'low':
      return 'segment-low'
    default:
      return 'segment-inactive'
  }
}

// 根据风险等级返回颜色类
const overallRiskColorClass = computed(() => {
  if (!predictionData.value) return ''
  switch (predictionData.value.overallRisk) {
    case 'high':
      return 'color-red-100'
    case 'medium':
      return 'color-orange-100'
    case 'low':
      return 'color-green-100'
    default:
      return ''
  }
})

// 根据风险等级返回标签文字
const overallRiskLabel = computed(() => {
  if (!predictionData.value) return ''
  switch (predictionData.value.overallRisk) {
    case 'high':
      return '高'
    case 'medium':
      return '中'
    case 'low':
      return '低'
    default:
      return ''
  }
})

// 查看历史法案
function handleViewHistory() {
  emit('prev')
}

// 重新预测
function handleRepredict() {
  emit('repredict')
}
</script>

<style scoped>
.step-container {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.loading-wrapper {
  flex: 1;
}

.content-wrapper {
  flex: 1;
  overflow: auto;
}

.header-section {
  justify-content: space-between;
  align-items: flex-start;
}

.header-left {
  gap: 12px;
  align-items: flex-start;
}

.header-left > div {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.header-right {
  text-align: right;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
}

.progress-bar-container {
  display: flex;
  gap: 4px;
}

.progress-segment {
  width: 50px;
  height: 4px;
  border-radius: 2px;
}

.segment-inactive {
  background-color: var(--bg-black-5);
}

.segment-high {
  background-color: var(--color-red-100);
}

.segment-medium {
  background-color: var(--color-orange-100);
}

.segment-low {
  background-color: var(--color-green-100);
}

.risk-label {
  font-size: 20px;
  font-weight: bold;
}

.highlight-box {
  position: relative;
  background-color: var(--bg-white-100);
  border: 1px solid var(--color-primary-10);
  border-radius: 4px;
  margin-bottom: 32px;
  overflow: hidden;
}

.highlight-wave {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 61px;
  background: linear-gradient(180deg, rgba(231, 243, 255, 1) 0%, rgba(246, 250, 255, 0) 100%);
  pointer-events: none;
}

.highlight-content {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px 24px;
}

.highlight-title {
  font-size: 16px;
  font-weight: 400;
  line-height: 30px;
  color: var(--color-primary-100);
}

.highlight-text {
  font-size: 16px;
  font-weight: 400;
  line-height: 30px;
  color: var(--color-primary-100);
  text-align: justify;
}

.phases-list {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.action-footer {
  justify-content: space-between;
  padding-top: 24px;
  border-top: 1px solid var(--border-black-5);
  margin-top: 24px;
}

.btn-text {
  gap: 8px;
  padding: 10px 24px;
  border: none;
  background: none;
  font-size: 14px;
  color: var(--text-primary-65-color);
  transition: color 0.2s;
}

.btn-text:hover {
  color: var(--text-primary-80-color);
}

.btn-primary {
  gap: 8px;
  padding: 10px 32px;
  border: none;
  border-radius: var(--radius-10);
  background-color: var(--color-primary-100);
  font-size: 14px;
  color: var(--bg-white-100);
  transition: opacity 0.2s;
}

.btn-primary:hover {
  opacity: 0.9;
}

.icon {
  width: 16px;
  height: 16px;
}

.icon-primary {
  width: 20px;
  height: 20px;
  color: var(--color-primary-100);
}
</style>
