<template>
  <div ref="chartRef"
    :class="className"
    :style="{ height: height, width: width }" />
</template>

<script setup>
import * as echarts from 'echarts'
import { onBeforeUnmount } from 'vue'
import { useWindowResize } from '@/hooks'

defineProps({
  className: {
    type: String,
    default: 'chart'
  },
  width: {
    type: String,
    default: '100%'
  },
  height: {
    type: String,
    default: '300px'
  }
})

let chart = null
const chartRef = ref(null)

const initChart = () => {
  chart = echarts.init(chartRef.value)

  chart.setOption({
    tooltip: {
      trigger: 'axis',
      axisPointer: { // 坐标轴指示器，坐标轴触发有效
        type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
      }
    },
    radar: {
      radius: '66%',
      center: ['50%', '42%'],
      splitNumber: 8,
      splitArea: {
        areaStyle: {
          color: 'rgba(127,95,132,.3)',
          opacity: 1,
          shadowBlur: 45,
          shadowColor: 'rgba(0,0,0,.5)',
          shadowOffsetX: 0,
          shadowOffsetY: 15
        }
      },
      indicator: [
        { name: '北京', },
        { name: '上海', },
        { name: '广州', },
        { name: '深圳', },
        { name: '杭州', },
        { name: '厦门', }
      ]
    },
    legend: {
      left: 'center',
      bottom: '10',
      data: ['营销中心', '人力资源中心', '财务中心'],
      textStyle: {
        color: '#888'
      }
    },
    series: [{
      type: 'radar',
      symbolSize: 0,
      areaStyle: {
        // normal: {
        shadowBlur: 13,
        shadowColor: 'rgba(0,0,0,.2)',
        shadowOffsetX: 0,
        shadowOffsetY: 10,
        opacity: 1
        // }
      },
      data: [
        {
          value: [5000, 7000, 12000, 11000, 15000, 14000],
          name: '营销中心'
        },
        {
          value: [4000, 9000, 15000, 15000, 13000, 11000],
          name: '人力资源中心'
        },
        {
          value: [5500, 11000, 12000, 15000, 12000, 12000],
          name: '财务中心'
        }
      ]
    }]
  })
}

const resize = () => {
  chart.resize()
}
useWindowResize(resize)

onMounted(() => {
  nextTick(() => {
    initChart()
  })
})

onBeforeUnmount(() => {
  if (!chart) {
    return
  }
  chart.dispose()
  chart = null
})
</script>
