<template>
  <div class="spotRate" :id="id" />
</template>

<script setup>
  import { fitChartSize } from '@/utils/dataUtil'
  import { useEchart } from '@/hooks/echart'

  const props = defineProps({
    config: {
      type: Object,
      default: () => {
        return {}
      }
    },
    dataList: {
      type: Array,
      default: () => []
    },
    total: {
      type: Number,
      default: () => 0
    },
    label: {
      type: String,
      default: () => ''
    },
    colors: {
      type: Array,
      default: () => ['#FDC40A', '#FF5232', '#50F0A6', '#5FDFFA']
    }
  })

  const { id, setOption } = useEchart()

  var defaultCofig = {
    color: [],
    series: [
      {
        type: 'pie',
        center: ['50%', '50%'],
        radius: ['70%', '90%'],
        itemStyle: {
          borderWidth: fitChartSize(4),
          borderColor: '#093672'
        },
        label: {
          show: true,
          position: 'center',
          fontWeight: 'bold',
          rich: {
            value: {
              color: '#fff',
              fontSize: fitChartSize(24),
              fontWeight: 'bold',
              padding: [0, 0, 5, 0]
            },
            name: {
              color: '#fff',
              fontSize: fitChartSize(12)
            }
          }
        },
        labelLine: {
          show: false
        },
        data: []
      }
    ]
  }

  watch(
    () => props.dataList,
    (newVal) => {
      if (newVal.length > 0) {
        nextTick(() => {
          defaultCofig.color = props.colors
          defaultCofig.series[0].data = props.dataList
          defaultCofig.series[0].label.formatter = () => {
            return `{value|${props.total}}` + '\n' + `{name|${props.label}}`
          }
          setOption({
            ...defaultCofig,
            ...props.config
          })
        })
      }
    },
    { immediate: true }
  )
</script>

<style scoped lang="scss">
  .spotRate {
    width: vw(240);
    height: vh(320);
  }
</style>
