<template>
  <div class="daily-currency-charts" :data-language="language">
    <el-select v-model="typeValue" :placeholder="$t('pages.dashboard.shared.select')" size="mini" @change="typeValueChange">
      <el-option
        v-for="item in types"
        :key="item.value"
        :label="item.name"
        :value="item.value"
      />
    </el-select>
    <div id="videoCharts" ref="videoCharts" :style="'width: 100%;height:' + height + ';'" />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'DailyCurrencyCharts',
  props: {
    height: {
      type: String,
      default: '600px'
    },
    chartsData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      typeValue: 0,
      videoRealTimeCharts: null,
      groupChartsData: {
        income: [],
        expenditure: []
      }
    }
  },
  computed: {
    ...mapGetters(['language']),
    types() {
      const language = this.language
      return [
        { value: 0, name: this.$t('pages.dashboard.shared.income') },
        { value: 1, name: this.$t('pages.dashboard.shared.expenditure') }
      ]
    }
  },
  watch: {
    chartsData: {
      handler() {
        this.groupChartsData = this.groupChartsDataByType()
        this.$nextTick(() => {
          this.renderCharts()
        })
      },
      immediate: true
    },
    language() {
      this.$nextTick(() => {
        this.renderCharts()
      })
    }
  },
  mounted() {
    this.videoRealTimeCharts = this.$echarts.init(this.$refs.videoCharts)
    this.renderCharts()
    window.addEventListener('resize', () => {
      this.videoRealTimeCharts.resize()
    })
  },
  methods: {
    groupChartsDataByType() {
      const obj = {
        income: [],
        expenditure: []
      }
      if (this.chartsData.length > 0) {
        this.chartsData.forEach(item => {
          if (item.type === 0) {
            obj.income.push(item)
          }
          if (item.type === 1) {
            obj.expenditure.push(item)
          }
        })
      }
      return obj
    },
    createSeries(list) {
      if (!list || list.length === 0) {
        return []
      }
      const groupOrigin = {}
      const size = list.length - 1
      for (let index = size; index >= 0; index--) {
        const item = list[index]
        const origin = groupOrigin[item.originName]
        const element = [item.dateNumber, item.quantity || 0]
        if (!origin) {
          groupOrigin[item.originName] = {
            origin: item.origin,
            originName: item.originName,
            data: [element]
          }
          continue
        }
        origin.data.push(element)
      }

      const resultSeries = []
      for (const key in groupOrigin) {
        const origin = groupOrigin[key]
        resultSeries.push({
          name: origin.originName,
          type: 'line',
          stack: origin.originName,
          smooth: 0.6,
          symbol: 'none',
          symbolSize: 10,
          data: origin.data || [],
          areaStyle: {}
        })
      }
      return resultSeries
    },
    renderCharts() {
      if (!this.videoRealTimeCharts) {
        return
      }
      this.videoRealTimeCharts.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)' }
            }
          }
        },
        grid: {
          x: 20,
          y: 50,
          x2: 20,
          y2: 0,
          containLabel: true,
          borderColor: '#eee'
        },
        toolbox: { color: ['#1e90ff', '#1e90ff', '#1e90ff', '#1e90ff'], effectiveColor: '#ff4500' },
        xAxis: [{
          show: false,
          type: 'category',
          boundaryGap: false,
          splitArea: {
            show: true,
            areaStyle: { color: ['rgba(250,250,250,0.1)', 'rgba(200,200,200,0.1)'] }
          },
          axisLabel: {
            rotate: 38
          },
          axisLine: { lineStyle: { color: '#b7bdc7' }},
          splitLine: { lineStyle: { color: ['#eee'] }},
          splitNumber: 24
        }],
        yAxis: [{
          type: 'value',
          axisTick: { show: true, length: 0 },
          splitNumber: 5,
          splitLine: { lineStyle: { color: ['#eee'] }},
          axisLine: { lineStyle: { color: '#b7bdc7' }}
        }],
        legend: {
          y: 10,
          textStyle: { color: '#8e929b' }
        },
        series: this.createSeries(this.typeValue === 0 ? this.groupChartsData.income : this.groupChartsData.expenditure)
      }, true)
    },
    typeValueChange() {
      this.$nextTick(() => {
        this.renderCharts()
      })
    }
  }
}
</script>
