<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <!-- 页面标题 -->
      <div style="margin-bottom: 20px;">
        <h2 style="margin: 0; font-size: 20px; font-weight: 600; color: #303133;">学历统计</h2>
      </div>

      <!-- 图表统计 -->
      <div style="width: 100%">
        <el-row :gutter="24">
          <el-col :span="12">
            <v-chart ref="chartRef" style="width: 100%; height: 400px;" :option="chartOption" />
          </el-col>
          <el-col :span="12">
            <el-table :data="chartData" border show-summary style="width: 100%">
              <el-table-column prop="xl" label="学历" width="180" align="center" />
              <el-table-column prop="total" label="人数" width="180" align="center" />
              <el-table-column prop="rate" label="占比" width="180" align="center" />
            </el-table>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import VChart from 'vue-echarts'
import { use } from 'echarts/core'
import { PieChart } from 'echarts/charts'
import { TooltipComponent, LegendComponent } from 'echarts/components'
import { CanvasRenderer } from 'echarts/renderers'
import { getChartOption } from '/@/api/professional/professionaluser/professionalteacheracademicrelation'

// 注册 ECharts 组件
use([TooltipComponent, LegendComponent, PieChart, CanvasRenderer])

// 图表引用
const chartRef = ref()

// 图表数据
const chartOption = ref<any>({})
const chartData = ref<any[]>([])

// 初始化图表
const initChartOption = async () => {
  try {
    const response = await getChartOption()
    chartOption.value = response.data.data || {}
    
    // 处理图表数据
    if (chartOption.value.series && chartOption.value.series[0] && chartOption.value.series[0].data) {
      let total = 0
      chartOption.value.series[0].data.forEach((item: any) => {
        total += item.value || 0
      })
      
      chartData.value = []
      chartOption.value.series[0].data.forEach((item: any) => {
        const rate = total > 0 ? Number((item.value / total * 100).toFixed(1)) : 0
        chartData.value.push({
          xl: item.name,
          total: item.value,
          rate: `${rate}%`
        })
      })
    }
  } catch (error) {
    // Failed to load chart data
  }
}

// 页面加载时初始化图表
onMounted(() => {
  initChartOption()
})
</script>

<style lang="scss" scoped>
</style>

