<template>
  <div class="step-container">
    <div v-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">《大而美法案》的通过概率非常高。</p>
          <p class="highlight-text text-regular">该法案由众议院共和党领导层在5月正式提出，作为特朗普第二任期核心经济议程，旨在通过一揽子税收、贸易和预算改革提振经济。到6月初，法案已快速通过关键的筹款委员会和预算委员会审议，进入众议院全院辩论阶段。共和党当时控制众议院，且党内团结支持；白宫已明确表示强烈支持。虽然民主党普遍反对，但共和党凭借席位优势足以在众议院通过。主要不确定性在于参议院，但预计部分温和民主党议员可能支持，或通过预算和解程序（只需简单多数）规避阻挠议事。因此，该法案在6月初已势在必行，最终成法几无悬念。</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, onMounted, computed } from 'vue'
import { fetchPredictionAnalysis, type PredictionAnalysis } from '../api'
import PredictionPhaseCard from './PredictionPhaseCard.vue'

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

// 预测分析数据
const predictionData = ref<PredictionAnalysis | null>(null)

// 获取预测分析数据
onMounted(async () => { 
  const data = await fetchPredictionAnalysis() 
  predictionData.value = data 
})

// 根据索引和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>
/* .text-title-2-bold{
  color: #3b414b;
} */
.step-container {
  display: flex;
  flex-direction: column;
  height: 100%;
}

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

.header-section {
  justify-content: space-between;
  align-items: flex-start;
  /* margin-bottom: 24px; */
}

.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>
