<template>
  <div :class="className" :style="'height: ' + height" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '300px'
    },
    totalName: {
      type: String,
      default() {
        return this.$t('ec.dashboard.activeTrend')
      }
    },
    /**
    * 图表展示最多的条数限制
    * */
    showMaxNum: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      chart: null,
      option: null
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  // watch: {
  //   chartData() {
  //     debugger
  //     if (!this.chart) {
  //       return
  //     }
  //     this.setData()
  //   }
  // },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.option = {
        // backgroundColor: '#344b58',
        title: {
          // text: '活跃用户分析',
          x: '20',
          top: '10',
          textStyle: {
            color: '#90979c',
            fontSize: '22'
          },
          subtextStyle: {
            color: '#90979c',
            fontSize: '16'
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            textStyle: {
              color: '#fff'
            }
          }
        },
        grid: {
          left: '5%',
          right: '5%',
          borderWidth: 0,
          top: 50,
          bottom: 50,
          textStyle: {
            color: '#fff'
          }
        },
        legend: {
          x: '5%',
          top: '0',
          textStyle: {
            color: '#90979c'
          },
          data: [this.$t('ec.dashboard.app'), this.$t('ec.dashboard.pc'), this.$t('ec.dashboard.wechat'), this.totalName]
        },
        calculable: true,
        xAxis: [{
          type: 'category',
          axisLine: {
            lineStyle: {
              color: '#90979c'
            }
          },
          splitLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitArea: {
            show: false
          },
          axisLabel: {
            interval: 0
          },
          axisPointer: {
            type: 'shadow'
          },
          data: []
        }],
        yAxis: [{
          type: 'value',
          splitLine: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '#90979c'
            }
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            interval: 0
          },
          splitArea: {
            show: false
          }
        }],
        dataZoom: [{
          show: true,
          height: 20,
          xAxisIndex: [
            0
          ],
          bottom: 0,
          start: 0,
          end: 100,
          handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
          handleSize: '110%',
          handleStyle: {
            color: '#d3dee5',
            borderWidth: 1000
          },
          textStyle: {
            color: '#fff' },
          borderColor: '#90979c'
        }, {
          type: 'inside',
          show: true,
          height: 15,
          start: 0,
          end: 35
        }],
        series: [{
          name: this.$t('ec.dashboard.app'),
          type: 'bar',
          // stack: 'total',
          itemStyle: {
            normal: {
              color: 'rgb(0,191,183)',
              barBorderRadius: 0,
              label: {
                show: true,
                position: 'top',
                formatter(p) {
                  return p.value > 0 ? p.value : ''
                }
              }
            }
          },
          data: []
        }, {
          name: this.$t('ec.dashboard.pc'),
          type: 'bar',
          stack: 'total',
          barMaxWidth: 35,
          barGap: '10%',
          itemStyle: {
            normal: {
              color: 'rgba(255,144,128,1)',
              label: {
                show: true,
                // textStyle: {
                //   color: '#fff'
                // },
                // position: 'insideTop',
                position: 'top',
                formatter(p) {
                  return p.value > 0 ? p.value : ''
                }
              }
            }
          },
          data: []
        }, {
          name: this.$t('ec.dashboard.wechat'),
          type: 'bar',
          // stack: 'total',
          itemStyle: {
            normal: {
              color: '#feb42f',
              barBorderRadius: 0,
              label: {
                show: true,
                position: 'top',
                formatter(p) {
                  return p.value > 0 ? p.value : ''
                }
              }
            }
          },
          data: []
        }, {
          name: this.totalName,
          type: 'line',
          stack: 'total',
          symbolSize: 10,
          symbol: 'circle',
          itemStyle: {
            normal: {
              color: '#3888fa',
              barBorderRadius: 0,
              label: {
                show: true,
                position: 'top',
                formatter(p) {
                  return p.value > 0 ? p.value : ''
                }
              }
            }
          },
          data: []
        }
        ]
      }
      this.chart.setOption(this.option)
    },
    setData(chartData) {
      // if (!chartData) {
      //   chartData = this.chartData
      // }
      // this.chart.clear();
      this.showMaxNum = parseInt(this.showMaxNum)
      this.option.xAxis[0].data = chartData.date
      this.option.series[0].data = chartData.app
      this.option.series[1].data = chartData.pc
      this.option.series[2].data = chartData.weapp
      this.option.series[3].data = chartData.total

      if (this.showMaxNum && this.showMaxNum < chartData.date.length) {
        this.option.dataZoom[0].start = (1 - this.showMaxNum / chartData.date.length) * 100
      }
      this.chart.setOption(this.option)
    }
  }
}
</script>

<style scoped>
  .chart{
    position: relative;
    width: 100%;
    margin-top: 10px;
    margin-bottom: 20px;
  }
</style>
