<template>
  <div shadow="never" class="homeBoxCard" v-loading="loading">
    <div class="height260" ref="lineChartBanlance" id="lineChartBanlance" />
  </div>
</template>
<script>
import { computed, defineComponent, onMounted, ref, watch } from 'vue'
import { useStore } from 'vuex'
import * as echarts from 'echarts'

export default defineComponent({
  name: 'LoopRadarChart',
  props: {
    getData: Object,
  },
  setup(props) {
    let lineChartBanlance = ref(null)

    const store = useStore()
    const max=ref(0)

    // 总数
    const total = computed(() => store.state.Home.worksChartData.total)
    // num
    const num = computed(() => store.state.Home.worksChartData.num)

    // 读取数据 func
    const loading = ref(true)
    const getData = async () => {
      // loading.value = true
      // await store.dispatch('Home/queryWorksChartData')
      loading.value = false
      var aa = [props.getData.ua, props.getData.ub, props.getData.uc]

      aa.sort(function (a, b) {
        return a - b
      })
      max.value=  aa.pop()
       if (!props.getData.ua && !props.getData.ub && !props.getData.uc) {
        max.value = 1
      }
    
    }
    function echarts2() {
      let myChart = echarts.init(document.getElementById('lineChartBanlance'))
      // 绘制图表
      myChart.setOption({
        backgroundColor: '#FFF',
        tooltip: {},
        grid: {
          left: '20',
          right: '40',
          top: '-20',
          bottom: '0',
          containLabel: true,
        },
        radar: {
          shape: 'circle',
          radius: '70%',
          center: ['50%', '55%'],
          splitNumber: 5,
          nameGap: '15',
          name: {
            textStyle: {
              color: '#a8a8a8',
            },
          },
          axisLine: {
            lineStyle: {
              color: '#ebebeb',
            },
          },
          splitLine: {
            lineStyle: {
              width: 1,
              color: '#ebebeb',
            },
          },
          splitArea: {
            areaStyle: {
              color: ['#f8f8f8', '#fff'].reverse(),
            },
          },
          indicator: [
            {
              name: 'Ua',
              max: max.value?max.value:0,
            },
            {
              name: 'Ub',
              max: max.value?max.value:0,
            },
            {
              name: 'Uc',
              max: max.value?max.value:0,
            },
          ],
        },
        series: [
          {
            name: 'Title✍',
            type: 'radar',
            symbolSize: 12,
            itemStyle: {
              borderColor: '#ebebeb',
              borderWidth: 2,
            },
            areaStyle: {
              opacity: 0.3,
            },
            data: [
              {
                itemStyle: {
                  color: '#5eb6db',
                },
                value: [props.getData.ua, props.getData.ub, props.getData.uc],
              },
            ],
          },
        ],
      })
      window.addEventListener('resize', () => {
        myChart.resize()
      })
    }

    const writeValue = (val) => {
      val
      getData()
      echarts2()
    }

    //监听变化
    watch(
      () => props.getData,
      (newVal, oldVal, clear) => {
        // 执行异步任务，并得到关闭异步任务的 id
       newVal
        let id = writeValue(newVal, oldVal)
        // 如果 watch 监听被重复执行了，则会先清除上次未完成的异步任务
        clear(() => clearTimeout(id))
      },
      { lazy: true }
    )

    onMounted(() => {
      getData()
      echarts2()
    })

    return {
      lineChartBanlance,
      loading,
      total,
      num,
      echarts2,
    }
  },
})
</script>
<style lang="scss" scoped>
.homeBoxCard {
  margin-bottom: 24px;
  ::v-deep(.el-card__header) {
    padding-left: 12px;
    padding-right: 12px;
  }
  ::v-deep(.el-card__body) {
    padding: 12px;
    font-size: 14px;
    line-height: 1.5715;
  }
  ::v-deep(.el-divider) {
    margin: 8px 0;
  }
  .num {
    font-size: 30px;
    color: #515a6e;
  }
  .height260 {
    height: 200px;
  }
}
</style>