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

<script>
import * as echarts from 'echarts'
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: Number,
      default: 0
    },
    showId: {
      type: Number,
      default: 0
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    },
    title: {
      type: String,
      default: 'Mix-Chart'
    },
    titleFooter: {
      type: String,
      default: ''
    },
    titleHeader: {
      type: String,
      default: ''
    },
    dataList: {
      type: Array,
      default: () => {
        return []
      }
    },
    lastDataList: {
      type: Array,
      default: () => {
        return []
      }
    },
    chartType: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    return {
      chart: null,
      series: []
    }
  },
  beforeDestroy () {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  watch: {
    dataList: {
      handler (val) {
        if (val) {
          this.series = []
          if (this.chartType.indexOf('柱状') >= 0) {
            this.series.push({
              name: '本' + this.title,
              type: 'bar',
              barMaxWidth: 35,
              barGap: '10%',
              color: '#5AD8A6',
              label: {
                show: true,
                position: 'top',
                formatter (p) {
                  return p.value > 0 ? p.value : ''
                }
              },
              data: this.dataList.map(item => {
                return item.value
              })
            })
            this.series.push({
              name: '上' + this.title,
              type: 'bar',
              barMaxWidth: 35,
              barGap: '10%',
              color: '#5B8FF9',
              label: {
                show: true,
                position: 'top',
                formatter (p) {
                  return p.value > 0 ? p.value : ''
                }
              },
              data: this.lastDataList.map(item => {
                return item.value
              })
            })
          }
          if (this.chartType.indexOf('折线') >= 0) {
            this.series.push({
              name: '本' + this.title,
              type: 'line',
              symbolSize: 10,
              symbol: 'circle',
              color: '#5B8FF9',
              label: {
                show: true,
                position: 'top',
                formatter (p) {
                  return p.value > 0 ? p.value : ''
                }
              },
              data: this.dataList.map(item => {
                return item.value
              })
            })
            this.series.push({
              name: '上' + this.title,
              type: 'line',
              symbolSize: 10,
              symbol: 'circle',
              color: '#5AD8A6',
              label: {
                show: true,
                position: 'top',
                formatter (p) {
                  return p.value > 0 ? p.value : ''
                }
              },
              data: this.lastDataList.map(item => {
                return item.value
              })
            })
          }
          this.initChart()
        }
      },
      deep: true
    },
    showId: {
      handler () {
        this.initChart()
      }
    },
    chartType: {
      handler (val) {
        console.log(val)
        if (val.length) {
          this.series = []
          if (val.indexOf('柱状') >= 0) {
            this.series.push({
              name: '本' + this.title,
              type: 'bar',
              barMaxWidth: 35,
              barGap: '10%',
              color: '#5B8FF9',
              label: {
                show: true,
                position: 'top',
                formatter (p) {
                  return p.value > 0 ? p.value : ''
                }
              },
              data: this.dataList.map(item => {
                return item.value
              })
            })
            this.series.push({
              name: '上' + this.title,
              type: 'bar',
              barMaxWidth: 35,
              barGap: '10%',
              color: '#5AD8A6',
              label: {
                show: true,
                position: 'top',
                formatter (p) {
                  return p.value > 0 ? p.value : ''
                }
              },
              data: this.lastDataList.map(item => {
                return item.value
              })
            })
          }
          if (val.indexOf('折线') >= 0) {
            this.series.push({
              name: '本' + this.title,
              type: 'line',
              symbolSize: 10,
              symbol: 'circle',
              color: '#5B8FF9',
              barBorderRadius: 0,
              label: {
                show: true,
                position: 'top',
                formatter (p) {
                  return p.value > 0 ? p.value : ''
                }
              },
              data: this.dataList.map(item => {
                return item.value
              })
            })
            this.series.push({
              name: '上' + this.title,
              type: 'line',
              symbolSize: 10,
              symbol: 'circle',
              color: '#5AD8A6',
              barBorderRadius: 0,
              label: {
                show: true,
                position: 'top',
                formatter (p) {
                  return p.value > 0 ? p.value : ''
                }
              },
              data: this.lastDataList.map(item => {
                return item.value
              })
            })
          }
          this.initChart()
        }
      },
      deep: true
    }
  },
  methods: {
    initChart () {
      if (this.chart) {
        this.chart.dispose()
      }
      this.chart = echarts.init(document.getElementById(this.id))
      const xData = this.dataList.map(item => {
        return item.time
      })
      this.chart.setOption({
        title: {
          text: this.title === 'Mix-Chart' ? 'Mix-Chart' : this.titleHeader + this.title + this.titleFooter,
          x: '10',
          top: '10',
          fontSize: '18',
          subtextStyle: {
            color: '#90979c',
            fontSize: '16'
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            textStyle: {
              color: '#fff'
            }
          }
        },
        grid: {
          left: '5%',
          right: '5%',
          borderWidth: 0,
          top: 100,
          bottom: 70,
          textStyle: {
            color: '#fff'
          }
        },
        legend: {
          x: 20,
          top: 55,
          textStyle: {
            color: '#90979c'
          },
          data: this.series.map(item => {
            return item.name
          })
        },
        calculable: true,
        xAxis: [
          {
            type: 'category',
            axisLine: {
              lineStyle: {
                color: '#90979c'
              }
            },
            splitLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            splitArea: {
              show: false
            },
            axisLabel: {
              interval: 0
            },
            data: xData
          }
        ],
        yAxis: [
          {
            type: 'value',
            // splitLine: {
            //   show: false
            // },
            axisLine: {
              lineStyle: {
                color: '#90979c'
              }
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              interval: 0
            },
            splitArea: {
              show: false
            }
          }
        ],
        dataZoom: [
          {
            show: true,
            height: 30,
            xAxisIndex: [0],
            bottom: 10,
            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'
            },
            textStyle: {
              color: '#fff'
            },
            borderColor: '#90979c'
          },
          {
            type: 'inside',
            show: true,
            height: 15,
            start: 1,
            end: 35
          }
        ],
        series: this.series
      })
    }
  }
}
</script>
