<template>
  <div class="subsidy-integral-charts">
    <el-card v-loading="listLoading">
      <div slot="header" class="clearfix">
        <span>{{ $t('pages.dashboard.dailyPurchase.overviewTitle') }}</span>
      </div>
      <el-tag v-for="(item, index) in platformCounts" :key="index" style="margin-right: 10px;">{{ item.label }}({{ item.type }}): {{ item.val }}</el-tag>
      <div id="subsidyIntegralCharts" ref="subsidyIntegralCharts" :style="'width: 100%;height:' + height + ';'" />
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { formatDate } from '@/utils'
import { getDailyPurchaseAmountThirtyDays } from '@/api/statistics'

export default {
  name: 'LineGraphChartsGeneral',
  props: {
    height: {
      type: String,
      default: '600px'
    },
    areaCode: {
      type: String,
      default: 'DAILY'
    }
  },
  data() {
    return {
      listLoading: true,
      list: [],
      subsidyIntegralCharts: null,
      platformCounts: []
    }
  },
  computed: {
    ...mapGetters(['language'])
  },
  watch: {
    areaCode: {
      handler(newVal) {
        if (newVal) {
          this.renderData()
        }
      },
      immediate: true
    },
    language() {
      this.$nextTick(() => {
        this.renderCharts()
      })
    }
  },
  mounted() {
    this.subsidyIntegralCharts = this.$echarts.init(this.$refs.subsidyIntegralCharts)
    window.addEventListener('resize', () => {
      this.subsidyIntegralCharts.resize()
    })
  },
  methods: {
    renderData() {
      this.listLoading = true
      getDailyPurchaseAmountThirtyDays(this.areaCode).then(res => {
        const { body } = res
        this.list = body || []
        this.renderCharts()
        this.listLoading = false
      })
    },
    handleChartsData() {
      const iosPurchase = []
      const iosRefund = []
      const googlePurchase = []
      const googleRefund = []
      const h5Purchase = []
      const h5Refund = []
      const offline = []
      if (this.list.length > 0) {
        this.list.forEach(item => {
          const date = formatDate(item.statisticsTime, 'yyyyMMdd')
          if (item.platform === 'iOS') {
            iosPurchase.push([date, (item.purchase || 0)])
            iosRefund.push([date, (item.refund || 0)])
            return
          }
          if (item.platform === 'Android') {
            googlePurchase.push([date, (item.purchase || 0)])
            googleRefund.push([date, (item.refund || 0)])
          }
          if (item.platform === 'H5') {
            h5Purchase.push([date, (item.purchase || 0)])
            h5Refund.push([date, (item.refund || 0)])
          }
          if (item.platform === 'Offline' && this.areaCode === 'DAILY') {
            offline.push([date, (item.purchase || 0)])
          }
        })
      }
      return { iosPurchase, iosRefund, googlePurchase, googleRefund, h5Purchase, h5Refund, offline }
    },
    renderCharts() {
      if (!this.subsidyIntegralCharts) {
        return
      }
      const thatDayData = this.handleChartsData()
      const series = [
        {
          name: this.$t('pages.dashboard.dailyPurchase.androidPurchase'),
          type: 'line',
          stack: this.$t('pages.dashboard.dailyPurchase.androidPurchase'),
          smooth: 0.6,
          symbol: 'none',
          symbolSize: 10,
          data: thatDayData.googlePurchase,
          areaStyle: {},
          itemStyle: {
            color: '#3eda86'
          }
        },
        {
          name: this.$t('pages.dashboard.dailyPurchase.androidRefund'),
          type: 'line',
          stack: this.$t('pages.dashboard.dailyPurchase.androidRefund'),
          smooth: 0.6,
          symbol: 'none',
          symbolSize: 10,
          data: thatDayData.googleRefund,
          areaStyle: {},
          itemStyle: {
            color: '#55886d'
          }
        },
        {
          name: this.$t('pages.dashboard.dailyPurchase.iosPurchase'),
          type: 'line',
          stack: this.$t('pages.dashboard.dailyPurchase.iosPurchase'),
          smooth: 0.6,
          symbol: 'none',
          symbolSize: 10,
          data: thatDayData.iosPurchase,
          areaStyle: {},
          itemStyle: {
            color: '#303133'
          }
        },
        {
          name: this.$t('pages.dashboard.dailyPurchase.iosRefund'),
          type: 'line',
          stack: this.$t('pages.dashboard.dailyPurchase.iosRefund'),
          smooth: 0.6,
          symbol: 'none',
          symbolSize: 10,
          data: thatDayData.iosRefund,
          areaStyle: {},
          itemStyle: {
            color: '#828486'
          }
        },
        {
          name: 'Offline',
          type: 'line',
          stack: 'Offline',
          smooth: 0.6,
          symbol: 'none',
          symbolSize: 10,
          data: thatDayData.offline,
          areaStyle: {},
          itemStyle: {
            color: '#6c4c49'
          }
        },
        {
          name: this.$t('pages.dashboard.dailyPurchase.h5Purchase'),
          type: 'line',
          stack: this.$t('pages.dashboard.dailyPurchase.h5Purchase'),
          smooth: 0.6,
          symbol: 'none',
          symbolSize: 10,
          data: thatDayData.h5Purchase,
          areaStyle: {},
          itemStyle: {
            color: '#e9640e'
          }
        },
        {
          name: this.$t('pages.dashboard.dailyPurchase.h5Refund'),
          type: 'line',
          stack: this.$t('pages.dashboard.dailyPurchase.h5Refund'),
          smooth: 0.6,
          symbol: 'none',
          symbolSize: 10,
          data: thatDayData.h5Refund,
          areaStyle: {},
          itemStyle: {
            color: '#ff9c6e'
          }
        }
      ]
      this.subsidyIntegralCharts.setOption({
        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
          },
          splitLine: { lineStyle: { color: ['#eee'] }},
          splitNumber: 24,
          axisLine: { lineStyle: { color: '#b7bdc7' }}
        }],
        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: series.filter(item => item.data && item.data.length > 0)
      }, true)
    }
  }
}
</script>
