<template>
  <div style="height: calc(100% - 200px); display: flex; align-items: center; justify-content: flex-start;">
    <div id="todayType" style="width:400px; height: 230px" />
  </div>
</template>

<script>
export default {
  props: {
    todayAllNum: {
      type: Object,
      require: true,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
    }
  },
  watch: {
    'todayAllNum': {
      handler(val) {
        setTimeout(() => {
          this.initTodayCircle()
        }, 100)
      },
      immediate: true,
      deep: true
    }
  },
  created() {
  },
  beforeDestroy() {
  },
  mounted() {
    this.initTodayCircle()
  },
  methods: {
    initTodayCircle() {
      const optionData = [
        { value: this.todayAllNum.headerLib || 0, name: '大湾' },
        { value: this.todayAllNum.branchLib || 0, name: '张家湾' }
      ]
      const myChart = this.$echarts.init(document.getElementById('todayType'))

      function arrCount(arr) {
        let count = 0
        arr.forEach(item => {
          count = count + item.value
        })
        return count
      }
      var centerImg = require('@/assets/images/circle-bg.png')
      const option = {
        baseOption: {
          tooltip: {
            show: false,
            trigger: 'item',
            position: 'bottom',
            textStyle: {
              color: '#EEF6FF',
              fontSize: '18'
            }
          // backgroundColor: 'rgba(74, 144, 226, 0.84)',
          // formatter: (params) => {
          //   return `<div>${params.seriesName} <br> ${params.data.name}：${this.$parent.formatter(params.data.value)} (${params.percent}%)</div>`
          // }
          },
          legend: {
            orient: 'vertical',
            right: 40,
            top: 55,
            // textStyle: {
            //   color: '#EEF6FF',
            //   padding: [20, 0, 18, 4],
            //   fontSize: '14'
            // },
            itemWidth: 10,
            itemHeight: 10,
            icon: 'circle',
            selectedMode: false,
            data: ['大湾', '张家湾'],
            formatter: (name) => {
            // `${name}  ${((flag.value / count).toFixed(2)) * 100 + '%'}`
              const count = arrCount(optionData)
              if (optionData) {
                const flag = optionData?.find(item => name === item.name)
                if (flag) {
                  const percentage = count > 0 ? ((flag.value / count) * 100).toFixed(0) + '%' : '0%'
                  return [`{name|${name}}`, `{num|${percentage}}`]
                }
              }
              return name
            },
            textStyle: {
              rich: {
                name: {
                  fontSize: 18,
                  color: '#EEF6FF',
                  padding: [20, 0, 20, 4],
                  fontFamily: 'DingTalk_JinBuTi_Regular'
                },
                num: {
                  fontSize: 20,
                  fontWeight: 600,
                  padding: [20, 0, 20, 15],
                  color: '#4C90FF',
                  fontFamily: 'DingTalk_JinBuTi_Regular'
                }
              }
            }
          },
          // 中心图片配置(关键代码)
          graphic: [
            {
              type: 'image',
              id: 'logo',
              left: '8.5%', // 调整图片位置
              top: '22%', // 调整图片位置
              z: -10,
              bounding: 'raw',
              rotation: 0, // 旋转
              origin: [64.5, 32.5], // 中心点
              scale: [1.0, 1.0], // 缩放
              // 设置图片样式
              style: {
                image: centerImg,
                width: 132,
                height: 131,
                opacity: 1
              }
            }
          ],
          series: [
            {
              name: '今日累计借阅',
              type: 'pie',
              left: '-50%',
              radius: ['60%', '70%'],
              avoidLabelOverlap: false,
              label: {
                show: false,
                position: 'center'
              },
              labelLine: {
                show: true
              },
              itemStyle: {
                borderWidth: 2,
                borderColor: 'rgba(16,16,21,0.4)'
              },
              emphasis: {
                label: {
                  show: true,
                  // 自定义文字显示，函数默认params接受当前指向所有属性
                  formatter: function(params) {
                    const { value, name } = params
                    return [
                      `{c| ${value}}`,
                      `{b| ${name}}`
                    ].join('\n') // 换行
                  },
                  rich: {
                    c: {
                      fontSize: 24,
                      fontWeight: 600,
                      color: '#317FFF',
                      fontFamily: 'DingTalk_JinBuTi_Regular',
                      lineHeight: 34
                    },
                    b: {
                      fontSize: 18,
                      color: '#fff',
                      fontFamily: 'DingTalk_JinBuTi_Regular'
                    }
                  }
                }
              },
              color: ['#317FFF', '#31DFFF'],
              data: optionData
            }
          ]
        },
        media: [
          {
            query: {
              maxWidth: 317
            },
            option: {
              legend: {
                right: 30,
                top: 'center',
                textStyle: {
                  rich: {
                    name: {
                      fontSize: 14
                    },
                    num: {
                      fontSize: 16
                    }
                  }
                }
              },
              // 中心图片配置(关键代码)
              graphic: [
                {
                  left: '8%', // 调整图片位置
                  top: '26%', // 调整图片位置
                  z: -10,
                  bounding: 'raw',
                  rotation: 0, // 旋转
                  origin: [64.5, 32.5], // 中心点
                  scale: [1.0, 1.0], // 缩放
                  // 设置图片样式
                  style: {
                    image: centerImg,
                    width: 110,
                    height: 110,
                    opacity: 1
                  }
                }
              ],
              series: [
                {
                  type: 'pie',
                  radius: ['50%', '55%'],
                  emphasis: {
                    label: {
                      rich: {
                        c: {
                          fontSize: 22,
                          lineHeight: 28
                        },
                        b: {
                          fontSize: 16
                        }
                      }
                    }
                  }
                }
              ]
            }
          }
        ]
      }
      myChart.setOption(option)
      // // 然后可以通过定时器来旋转背景图
      // var step = 0
      // setInterval(function() {
      //   // 每秒旋转1度
      //   myChart.setOption({
      //     graphic: {
      //       id: 'logo',
      //       style: {
      //         transform: 'rotate(' + (step++ % 360) + 'deg)' // 持续旋转
      //       }
      //     }
      //   })
      // }, 1000)

      this.$LoopShowTooltip(myChart, option.baseOption, { loopSeries: true, interval: 4000 })
    }
  }
}

</script>

<style lang="scss">
@import "~@/assets/styles/index.scss";
@import "~@/assets/styles/font-some.css";

</style>
