<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

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

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    },
    theme: {
      type: String,
      default: 'dark'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    theme() {
      this.setOptions(this.chartData)
    },
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      if (this.chart) {
        this.chart.dispose()
      }
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ expectedData, actualData, xAxis } = {}) {
      const isDark = this.theme === 'dark';
      const textColor = isDark ? '#fff' : '#333';
      const splitLineColor = isDark ? 'rgba(255, 255, 255, 0.1)' : 'rgba(0, 0, 0, 0.1)';

      this.chart.setOption({
        xAxis: {
          data: xAxis && xAxis.length ? xAxis : ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
          boundaryGap: false,
          axisTick: {
            show: false
          },
          axisLabel: {
            color: textColor
          },
          axisLine: {
            lineStyle: {
              color: textColor
            }
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          },
          axisLabel: {
            color: textColor
          },
          axisLine: {
            lineStyle: {
              color: textColor
            }
          },
          splitLine: {
            lineStyle: {
              color: splitLineColor
            }
          }
        },
        legend: {
          data: ['上周', '本周'],
          textStyle: {
            color: textColor
          }
        },
        series: [{
          name: '上周', itemStyle: {
            normal: {
              color: '#FF005A',
              lineStyle: {
                color: '#FF005A',
                width: 3,
                shadowColor: 'rgba(255, 0, 90, 0.3)',
                shadowBlur: 10
              }
            }
          },
          smooth: true,
          type: 'line',
          data: expectedData,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        },
        {
          name: '本周',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#00f2fe',
              lineStyle: {
                color: '#00f2fe',
                width: 3,
                shadowColor: 'rgba(0, 242, 254, 0.3)',
                shadowBlur: 10
              },
              areaStyle: {
                color: {
                    type: 'linear',
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [{
                        offset: 0, color: 'rgba(0, 242, 254, 0.3)' // 0% 处的颜色
                    }, {
                        offset: 1, color: 'rgba(0, 242, 254, 0)' // 100% 处的颜色
                    }],
                    global: false // 缺省为 false
                }
              }
            }
          },
          data: actualData,
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        }]
      })
    }
  }
}
</script>
