<template>
  <a-spin :spinning="formLoading.visible" class="fill">
    <a-row class="fill">
      <a-col span="16" style="height: 100%">
        <div :id="chartMain.id" class="fill"></div>
      </a-col>
      <a-col span="8" style="height: 100%">
        <div :id="chartSub.id" class="fill" style="height: 50%"></div>
        <div :id="chartThird.id" class="fill" style="height: 50%"></div>
      </a-col>
    </a-row>
  </a-spin>
</template>

<script>
import * as echarts from 'echarts'
import { aggregate } from 'echarts-simple-transform'
import config from './options/lg'
import moment from 'moment'
import warnApi from '@/api/warnApi'
import _ from 'lodash'

export default {
  name: 'WarnHistoryLg',
  props: {
    sourceList: {
      type: Object,
      default: () => { }
    },
    jh: {
      type: String,
      default: () => ''
    }
  },
  data () {
    return {
      formLoading: {
        visible: false,
        tip: '正在加载...'
      },

      minSize: { width: 330, height: 260 },
      chartMain: { id: 'warnHistoryXsChart' + moment().unix(), chart: {} },
      chartSub: {
        id: 'warnHistoryXsChartSub' + moment().unix(), chart: {}
      },
      chartThird: {
        id: 'warnHistoryXsChartThird' + moment().unix(), chart: {}
      },
      sizeInfo: {}
    }
  },
  methods: {
    chartInit () {
      this.chartMain.chart.showLoading()
      warnApi.getWarnHistoryList_Hour({ jh: this.jh })
        .then(res => {
          this.chartMain.chart.hideLoading()

          this.chartMain.chart.setOption(config.mainOption(res.result.data))
          const firstDate = res.result.data.length > 0 ? res.result.data[0].date : ''
          this.chartSub.chart.setOption(config.subOption(firstDate, res.result.data))
          this.chartThird.chart.setOption(config.thirdOption(firstDate, []))
        }).catch(e => {
          this.chartMain.chart.hideLoading()
          this.$message.error('获取报警历史信息时出现错误,请重试')
        })
    },
    subChartFllow (recodrOnTime, pieData) {
      if (!recodrOnTime) {
        return
      }
      this.chartSub.chart.setOption({
        title: {
          text: `分时合计(${recodrOnTime.date})`,
          left: 'center',
          top: 8
        },
        dataset: [{
          id: 'hours',
          transform: [{
            type: 'filter',
            config: { dimension: 'date', '=': recodrOnTime.date }
          }]
        }]
      })
      this.chartThird.chart.setOption({
        title: {
          show: true,
          text: `当天来源分类（${recodrOnTime.date}）`,
          left: 'center',
          top: 8
        },
        dataset: [{
          id: 'raw',
          source: pieData
        }]
      })
    }
  },
  created () {
    echarts.registerTransform(aggregate)

    this.debounceResize = _.debounce((rec) => {
      const { chartMain, chartSub, chartThird } = this
      chartMain.chart.resize()
      chartSub.chart.resize()
      chartThird.chart.resize()
    }, 200)

    this.debounceSubChartFllow = _.debounce((recodrOnTime, pieData) => {
      this.subChartFllow(recodrOnTime, pieData)
    }, 200)
  },
  mounted () {
    this.chartMain.chart = echarts.init(document.getElementById(this.chartMain.id), 'shine')
    this.chartSub.chart = echarts.init(document.getElementById(this.chartSub.id), 'shine')
    this.chartThird.chart = echarts.init(document.getElementById(this.chartThird.id), 'shine')
    const that = this
    this.chartMain.chart.on('timelinechanged', function (event) {
      const name = event.currentIndex
      that.chartSub.sourceKey = this.getOption().dataset[0].source[event.currentIndex * 24]
      const pieData = this.getOption().pieList[that.chartSub.sourceKey.date]
      that.debounceSubChartFllow(that.chartSub.sourceKey, pieData)
    })

    this.$nextTick(function () {
      this.chartInit()
      this.chartMain.chart.showLoading()
    })
  },
  computed: {},
  watch: {},
  destroyed () {
    this.chartMain.chart.dispose()
    this.chartSub.chart.dispose()
    this.chartThird.chart.dispose()
  }
}
</script>

<style lang="less" scoped>
.fill {
  width: 100%;
  height: 100%;
}
</style>
