<template>
  <div class="system-accuracy-compare-bar">
    <div ref="chartElement" class="chart-body" role="img" :aria-label="ariaLabel"></div>
  </div>
</template>

<script setup>
import { computed, shallowRef } from 'vue'
import { BarChart as EChartsBarChart } from 'echarts/charts'
import { GridComponent, LegendComponent, TooltipComponent } from 'echarts/components'
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'

import { useEcharts } from '../../composables/useEcharts.js'
import { useThemeColors } from '../../composables/useThemeColors.js'

use([GridComponent, LegendComponent, TooltipComponent, EChartsBarChart, CanvasRenderer])

const props = defineProps({
  categories: { type: Array, required: true },
  correct: { type: Array, required: true },
  wrong: { type: Array, required: true }
})

const chartElement = shallowRef(null)
const themeColors = useThemeColors()
const ariaLabel = computed(() =>
  props.categories.map((name, index) => (
    `${name}正确${props.correct[index] || 0}次，错误${props.wrong[index] || 0}次`
  )).join('；')
)

const chartOptions = computed(() => ({
  backgroundColor: 'transparent',
  animation: true,
  animationDuration: 900,
  animationEasing: 'cubicOut',
  legend: {
    top: 0,
    left: 0,
    itemWidth: 9,
    itemHeight: 9,
    itemGap: 18,
    textStyle: {
      color: '#475569',
      fontSize: 12,
      fontWeight: 700
    }
  },
  grid: {
    top: 38,
    right: 42,
    bottom: 18,
    left: 88,
    containLabel: false
  },
  tooltip: {
    trigger: 'axis',
    axisPointer: { type: 'shadow' },
    confine: true,
    appendToBody: true,
    backgroundColor: 'rgba(255, 255, 255, 0.98)',
    borderColor: 'rgba(148, 163, 184, 0.24)',
    borderWidth: 1,
    padding: [9, 10],
    textStyle: {
      color: '#334155',
      fontSize: 12,
      fontWeight: 700
    },
    extraCssText: 'border-radius:4px;box-shadow:0 12px 28px rgba(15,23,42,.12);',
    valueFormatter: (value) => `${value} 次`
  },
  xAxis: {
    type: 'value',
    min: 0,
    axisLabel: {
      color: '#64748b',
      fontSize: 11,
      fontWeight: 700
    },
    splitLine: { lineStyle: { color: 'rgba(226, 232, 240, 0.74)' } }
  },
  yAxis: {
    type: 'category',
    data: props.categories,
    inverse: true,
    axisTick: { show: false },
    axisLine: { show: false },
    axisLabel: {
      color: '#475569',
      fontSize: 12,
      fontWeight: 750
    }
  },
  series: [
    {
      name: '正确',
      type: 'bar',
      data: props.correct,
      barWidth: 18,
      itemStyle: {
        color: themeColors.value.success,
        borderRadius: [0, 4, 4, 0]
      },
      label: {
        show: true,
        position: 'right',
        color: '#475569',
        fontSize: 11,
        fontWeight: 800
      }
    },
    {
      name: '错误',
      type: 'bar',
      data: props.wrong,
      barWidth: 18,
      itemStyle: {
        color: themeColors.value.danger,
        borderRadius: [0, 4, 4, 0]
      },
      label: {
        show: true,
        position: 'right',
        color: '#ef4444',
        fontSize: 11,
        fontWeight: 800
      }
    }
  ]
}))

useEcharts(chartElement, chartOptions)
</script>

<style scoped>
.system-accuracy-compare-bar {
  height: 292px;
}

.chart-body {
  width: 100%;
  height: 100%;
}
</style>
