<template>
  <div class="register-logout-count-charts">
    <div id="charts" ref="charts" :style="'width: 100%;height:' + height + ';'" />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { copyText } from '@/utils'

export default {
  props: {
    height: {
      type: String,
      default: '600px'
    },
    chartsData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      charts: null
    }
  },
  computed: {
    ...mapGetters(['language'])
  },
  watch: {
    chartsData: {
      immediate: true,
      handler() {
        if (this.charts) {
          this.charts.clear()
          this.renderCharts()
        }
      }
    },
    language() {
      if (this.charts) {
        this.charts.clear()
        this.renderCharts()
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.charts = this.$echarts.init(this.$refs.charts)
      this.renderCharts()
      window.addEventListener('resize', () => {
        this.charts.resize()
      })
      this.charts.on('click', param => {
        copyText(param.name).then(() => {
          this.$opsMessage.success()
        }).catch(() => {
          this.$opsMessage.fail()
        })
      })
    })
  },
  methods: {
    newRich(imageUrl) {
      return {
        height: 40,
        align: 'center',
        backgroundColor: {
          image: imageUrl
        }
      }
    },
    proccessData() {
      const charts = {
        xAxisData: [],
        seriesData: [],
        richData: {
          value: {
            lineHeight: 30,
            align: 'center'
          }
        }
      }
      if (this.chartsData.length > 0) {
        this.chartsData.forEach(item => {
          const propsSource = item.propsSource
          charts.xAxisData.push(propsSource.id)
          charts.seriesData.push([propsSource.id, item.saleQuantity || 0])
          charts.richData[propsSource.id] = this.newRich(propsSource.cover)
        })
      }
      return charts
    },
    renderCharts() {
      if (!this.charts) {
        return
      }
      const proccessChartsData = this.proccessData()
      this.charts.setOption({
        color: ['#66b3ff', '#ce90e8', '#ff9c6e', '#5cdbd3'],
        tooltip: {
          trigger: 'axis',
          backgroundColor: 'rgba(50,50,50,0.5)',
          axisPointer: {
            type: 'cross',
            label: {
              lineStyle: { color: '#009688' },
              crossStyle: { color: '#008acd' },
              shadowStyle: { color: 'rgba(200,200,200,0.2)' }
            }
          },
          formatter: val => {
            return this.$t('pages.dashboard.propsSalesOverview.quantity', { value: val[0].data[1] })
          }
        },
        legend: {
          show: false,
          textStyle: { color: '#8e929b' }
        },
        grid: {
          x: 20,
          y: 50,
          x2: 20,
          y2: 30,
          containLabel: true,
          borderColor: '#eee'
        },
        toolbox: { color: ['#1e90ff', '#1e90ff', '#1e90ff', '#1e90ff'], effectiveColor: '#ff4500' },
        xAxis: [{
          show: true,
          type: 'category',
          data: proccessChartsData.xAxisData || [],
          splitArea: {
            show: true,
            areaStyle: { color: ['rgba(250,250,250,0.1)', 'rgba(200,200,200,0.1)'] }
          },
          axisLine: { lineStyle: { color: '#b7bdc7' }},
          splitLine: { lineStyle: { color: ['#eee'] }},
          axisLabel: {
            interval: 0,
            formatter(value) {
              return '{' + value + '| }'
            },
            rich: proccessChartsData.richData || {}
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: { show: true, length: 0 },
          splitNumber: 5,
          splitLine: { lineStyle: { color: ['#eee'] }},
          axisLine: { lineStyle: { color: '#b7bdc7' }}
        }],
        series: {
          type: 'bar',
          smooth: 0.6,
          symbol: 'none',
          symbolSize: 10,
          data: proccessChartsData.seriesData || [],
          areaStyle: {},
          barWidth: 40
        }
      }, true)
    }
  }
}
</script>
