<script setup lang="ts">
import type * as API from '@/service/types'
import UniEcharts from 'uni-echarts'
import { computed, ref } from 'vue'

const props = defineProps<{
  data: API.AdvantageAnalysis | undefined
}>()

// 分析模式：同分数段、班级、年级
type AnalysisMode = 'score_range' | 'class' | 'grade'
const analysisMode = ref<AnalysisMode>('class')

// 图表标题
const chartTitle = computed(() => {
  if (analysisMode.value === 'score_range') {
    return '同分数段优劣势分析'
  }
  else if (analysisMode.value === 'class') {
    return '班级优劣势分析'
  }
  else {
    return '年级优劣势分析'
  }
})

// 获取当前模式的数据
const currentData = computed(() => {
  if (!props.data)
    return null

  if (analysisMode.value === 'score_range') {
    return props.data.score_range_analysis
  }
  else if (analysisMode.value === 'class') {
    return props.data.class_analysis
  }
  else {
    return props.data.grade_analysis
  }
})

// 图表配置
const chartOption = computed(() => {
  const data = currentData.value
  if (!data || !('subjects' in data) || !data.subjects || data.subjects.length === 0) {
    return {}
  }

  const categories = data.subjects.map((item: any) => item.subject_name || '')
  const myScores = data.subjects.map((item: any) => item.my_score || 0)
  const avgScores = data.subjects.map((item: any) => item.average_score || 0)

  return {
    tooltip: {
      trigger: 'axis',
      confine: true,
      axisPointer: {
        type: 'shadow',
      },
      formatter: (params: any) => {
        if (!Array.isArray(params))
          return ''
        let result = `${params[0].axisValue}`
        params.forEach((param: any) => {
          result += `\n${param.marker} ${param.seriesName}: ${param.value.toFixed(2)}分`
        })
        return result
      },
    },
    legend: {
      data: ['我的成绩', '平均分'],
      top: 5,
      textStyle: {
        fontSize: 12,
      },
    },
    grid: {
      left: 45,
      right: 15,
      bottom: 40,
      top: 40,
      containLabel: false,
    },
    xAxis: {
      type: 'category',
      data: categories,
      axisLabel: {
        rotate: 30,
        interval: 0,
        fontSize: 10,
      },
      axisLine: {
        lineStyle: {
          color: '#cccccc',
        },
      },
    },
    yAxis: {
      type: 'value',
      name: '分数',
      nameTextStyle: {
        fontSize: 11,
      },
      axisLabel: {
        fontSize: 10,
      },
      splitLine: {
        lineStyle: {
          type: 'dashed',
          color: '#eeeeee',
        },
      },
    },
    series: [
      {
        name: '我的成绩',
        type: 'bar',
        data: myScores,
        itemStyle: {
          color: '#3b82f6',
        },
        barMaxWidth: 35,
      },
      {
        name: '平均分',
        type: 'bar',
        data: avgScores,
        itemStyle: {
          color: '#94a3b8',
        },
        barMaxWidth: 35,
      },
    ],
  }
})

const showChart = computed(() => {
  const data = currentData.value
  return data && 'subjects' in data && data.subjects && data.subjects.length > 0
})

// 切换分析模式
function setAnalysisMode(mode: AnalysisMode) {
  analysisMode.value = mode
}
</script>

<template>
  <view class="rounded-xl bg-white p-4 shadow-sm">
    <!-- 标题 -->
    <view class="mb-4">
      <text class="text-lg text-slate-800 font-semibold">{{ chartTitle }}</text>
    </view>

    <!-- 模式切换按钮 -->
    <view class="mb-4 flex justify-center gap-2">
      <wd-button
        size="small"
        :type="analysisMode === 'score_range' ? 'primary' : 'default'"
        @click="setAnalysisMode('score_range')"
      >
        同分数段
      </wd-button>
      <wd-button
        size="small"
        :type="analysisMode === 'class' ? 'primary' : 'default'"
        @click="setAnalysisMode('class')"
      >
        班级
      </wd-button>
      <wd-button
        size="small"
        :type="analysisMode === 'grade' ? 'primary' : 'default'"
        @click="setAnalysisMode('grade')"
      >
        年级
      </wd-button>
    </view>

    <!-- 图表 -->
    <view v-if="showChart" class="chart-container">
      <uni-echarts :option="chartOption" custom-class="chart" />
    </view>

    <!-- 暂无数据 -->
    <view v-else class="h-60 flex items-center justify-center rounded-lg bg-gray-50">
      <text class="text-sm text-gray-400">暂无数据</text>
    </view>
  </view>
</template>

<style scoped>
.chart {
  width: 100%;
  height: 280px;
}
</style>
