<template>
  <TechCard>
    <div class="card-title">监护对象类型</div>
    <BaseChart :option="chartOption" :height="180" />
  </TechCard>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import TechCard from '../TechCard/index.vue'

defineOptions({ name: 'GuardObjectTypeCard' })

interface GuardItem {
  guardType: string
  count: string | number
  name?: string
}

interface Props {
  guardList: GuardItem[]
}

const props = defineProps<Props>()

// 提取分类名称和数量
const categories = computed(() => props.guardList.map((item) => item.name))
const counts = computed(() => props.guardList.map((item) => Number(item.count)))

// 配色方案
const colorPalette = ['#f39c12', '#2ecc71', '#3498db', '#e74c3c', '#9b59b6', '#1abc9c']

const chartOption = computed(() => ({
  tooltip: {
    trigger: 'item',
    axisPointer: { type: 'shadow' },
    formatter: '{b}: {c} 个',
  },
  grid: {
    left: 40,
    right: 20,
    top: 20,
    bottom: 60,
  },
  xAxis: {
    type: 'category',
    data: categories.value,
    axisLabel: {
      color: '#9cc5e0',
      interval: 0,
      fontSize: 10,
      margin: 10,
      formatter: (value: string) => {
        const lines = value.length > 4 ? value.match(/.{1,2}/g) : [value]
        return lines?.map((line) => `{label|${line}}`).join('\n')
      },
      rich: {
        label: {
          lineHeight: 14,
        },
      },
    },
    axisTick: { show: false },
    axisLine: { show: false },
  },
  yAxis: {
    type: 'value',
    axisLabel: { color: '#9cc5e0' },
    splitLine: { show: false },
  },
  series: [
    {
      type: 'bar',
      data: counts.value,
      barWidth: 30,
      itemStyle: {
        color: (params: { dataIndex: number }) => {
          return colorPalette[params.dataIndex % colorPalette.length]
        },
      },
      label: {
        show: true,
        position: 'top',
        color: '#00f0ff',
        fontSize: 12,
      },
    },
  ],
}))
</script>

<style scoped lang="less">
.card-title {
  font-size: 16px;
  font-weight: bold;
  color: #00f0ff;
  margin-bottom: 12px;
  text-align: center;
}
</style>
