import moment from 'moment'

export default {
  data() {
    return {
      timeTicket: null,
      currentIndex: -1
    }
  },
  methods: {
    handleChartLineLoop(option, myChart) {
      if (!myChart) {
        return
      }
      const selectPie = () => {
        const dataLen = option.series[0].data.length
        this.currentIndex = (this.currentIndex + 1) % dataLen
        highlightLine()
      }
      this.timeTicket = setInterval(selectPie, 1500)

      const highlightLine = () => {
        // 遍历饼图数据，取消所有图形的高亮效果
        for (const idx in option.series[0].data) {
          myChart.dispatchAction({
            type: 'downplay',
            seriesIndex: 0,
            dataIndex: idx
          })
        }
        myChart.dispatchAction({
          type: 'showTip',
          seriesIndex: 0,
          dataIndex: this.currentIndex
        })
        myChart.setOption(option)
      }

      this.handleMouseOver(myChart, () => {
        clearInterval(this.timeTicket)
      })

      this.handleMouseOut(myChart, () => {
        if (this.timeTicket) {
          clearInterval(this.timeTicket)
        }
        this.timeTicket = setInterval(selectPie, 1500)
      })
    },

    handleChartPieLoop(option, myChart, pieTimeTicket, currentIndex, showTips = false) {
      if (!myChart) {
        return
      }
      const selectPie = () => {
        const dataLen = option.series[0].data.length
        this[currentIndex] = (this[currentIndex] + 1) % dataLen
        highlightPie()
      }

      this[pieTimeTicket] = setInterval(selectPie, 2000) // 设置自动切换高亮图形的定时器

      // 取消所有高亮并高亮当前图形
      const highlightPie = () => {
        // 遍历饼图数据，取消所有图形的高亮效果
        for (const idx in option.series[0].data) {
          myChart.dispatchAction({
            type: 'downplay',
            seriesIndex: 0,
            dataIndex: idx
          })
        }
        // 高亮当前图形
        myChart.dispatchAction({
          type: 'highlight',
          seriesIndex: 0,
          dataIndex: this[currentIndex]
        })
        showTips && myChart.dispatchAction({
          type: 'showTip',
          seriesIndex: 0,
          dataIndex: this[currentIndex]
        })
      }
      myChart.on('mouseover', (params) => {
        clearInterval(this[pieTimeTicket])
        this[currentIndex] = params.dataIndex
        highlightPie()
      })

      // 用户鼠标移出时，重新开始自动切换
      myChart.on('mouseout', () => {
        if (this[pieTimeTicket]) {
          clearInterval(this[pieTimeTicket])
        }
        this[pieTimeTicket] = setInterval(selectPie, 2000)
      })

      this.handleMouseOver(myChart, () => {
        clearInterval(this[pieTimeTicket])
      })

      this.handleMouseOut(myChart, () => {
        if (this[pieTimeTicket]) {
          clearInterval(this[pieTimeTicket])
        }
        this[pieTimeTicket] = setInterval(selectPie, 2000)
      })
    },
    /**
     * @returns 获取今天的时间
     */
    getDay() {
      return moment().format('YYYY-MM-DD')
    },
    /**
     * 获取从今天开始指定天数
     * @param {Number} num
     * @returns string
     */
    getYesterDay(num) {
      return moment().subtract(num, 'days').format('YYYY-MM-DD')
    },
    getMonths(num) {
      return moment().subtract(num, 'months').format('YYYY-MM-DD')
    },
    getYears(num) {
      return moment().subtract(num, 'years').format('YYYY-MM-DD')
    },
    getWeeks(num) {
      return moment().subtract(num, 'weeks').format('YYYY-MM-DD')
    }
  }
}
