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

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { getOnOffCount } from '@/api/sDevice'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    }
  },
  data() {
    return {
      relayon: 0,
      relayoff: 0,
      devon: 0,
      devoff: 0,
      chart: null,
      scale: 1,
      total_datas: 0
    }
  },
  mounted() {
    this.getOnOffCount()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {

    getOnOffCount() {
      getOnOffCount().then(res => {
        if (res.relayon != null) {
          this.relayon = res.relayon
          this.total_datas += res.relayon
        }
        if (res.relayoff != null) {
          this.relayoff = res.relayoff
          this.total_datas += res.relayoff
        }
        if (res.devon != null) {
          this.devon = res.devon
          this.total_datas += res.devon
        }
        if (res.devoff != null) {
          this.devoff = res.devoff
          this.total_datas += res.devoff
        }
        // console.log(this.relayon, this.relayoff, this.devon, this.devoff)
        this.initChart()
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        title: {
          text: '一周温度',
          top: '15px',
          left: 'center',
          textStyle: {
            color: '#00fcfd'
          }
        },
        grid: {
          top: '20%',
          left: '5%',
          right: '5%',
          bottom: '5%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: ['26', '27', '28', '29', '30', '1', '2'],
          axisLabel: {
            margin: 10,
            color: '#00fcfd'
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: '#00fcfd',
              width: 2
            }
          },
          axisTick: {
            show: false,
            length: 25,
            lineStyle: {
              color: '#00fcfd'
            }
          },
          splitLine: {
            show: false,
            lineStyle: {
              color: '#00fcfd'
            }
          }
        },
        yAxis: [{
          type: 'value',
          position: 'right',
          axisTick: {
            show: false,
            length: 15,
            lineStyle: {
              color: '#00fcfd'
            }
          },
          splitLine: {
            show: false,
            lineStyle: {
              color: '#00fcfd'
            }
          },
          axisLine: {
            lineStyle: {
              color: '#00fcfd',
              width: 2
            }
          }
        }],
        series: [{
          name: '注册总量',
          type: 'line',
          smooth: true, // 是否平滑曲线显示
          showAllSymbol: true,
          symbol: 'circle',
          symbolSize: 6,
          lineStyle: {
            normal: {
              color: '#00fcfd' // 线条颜色
            }
          },
          label: {
            show: true,
            position: 'top',
            textStyle: {
              color: '#00fcfd'
            }
          },
          itemStyle: {
            color: 'red',
            borderColor: '#00fcfd',
            borderWidth: 3
          },
          tooltip: {
            show: false
          },
          areaStyle: {
            normal: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                offset: 0,
                color: '#eb64fb'
              },
              {
                offset: 1,
                color: '#3fbbff0d'
              }
              ], false)
            }
          },
          data: [3, 7, 9, 3, 8, 6, 8]
        }]
      })
    }
  }
}
</script>
