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

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

const animationDuration = 6000;

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    type: {
      type: String,
      default: 'bar'
    },
    legend: {},
    chartData: {
      type: Array,
      required: true
    },
    height: {
      type: String,
      default: '215px'
    }
  },
  data() {
    return {
      chart: null,
      flag: false
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        if (this.flag) {
          this.setOptions(val);
        }
      }
    },
    type: {
      deep: true,
      handler(val) {
        this.setOptions(this.chartData);
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      setTimeout(() => {
        this.chart = echarts.init(this.$el, 'macarons');
        this.setOptions(this.chartData);
      }, 300);
    },
    setOptions(chartData) {
      this.flag = true;
      let xAxisData = [];
      let t0 = [];
      let t1 = [];
      if (chartData) {
        chartData.forEach((item, index) => {
          xAxisData.push(item.hour + '时');
        });
        t0 = chartData.map((item) => item.t0);
        t1 = chartData.map((item) => item.t1);
      }
      this.chart.setOption({
        color: ['rgba(37, 97, 239, 1)'],
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          },
          formatter: '{b} : {c}'
        },
        legend: {
          data: ['散货', '集装箱']
        },
        grid: {
          top: '35%',
          left: '0%',
          right: '0%',
          bottom: '2%',
          containLabel: true,
          show: false
        },
        xAxis: [
          {
            type: 'category',
            data: xAxisData,
            axisTick: {
              show: false
            },
            axisLine: {
              lineStyle: {
                color: '#e6e6e6'
              }
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: '#545555',
                fontSize: '14'
              }
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            minInterval: 1,
            nameTextStyle: {
              color: '#545555'
            },
            axisTick: {
              show: false
            },
            splitArea: {
              show: false
            },
            splitLine: {
              lineStyle: {
                color: '#e6e6e6',
                width: 0.7
              }
            },
            axisLine: {
              lineStyle: {
                color: '#e6e6e6'
              }
            },
            axisLabel: {
              textStyle: {
                color: '#545555'
              }
            }
          }
        ],
        series: [
          {
            name: '散货',
            type: this.type,
            barWidth: '35%',
            smooth: false,
            data: t0,
            animationDuration,
            itemStyle: {
              normal: {
                color: '#2561ef',
                lineStyle: {
                  color: '#2561ef',
                  width: 2
                },
                areaStyle: {
                  color: '#e9effd'
                },
                label: {
                  show: true,
                  position: 'top',
                  textStyle: {
                    color: '#545555',
                    fontSize: 14
                  },
                  formatter: '{c}次'
                }
              }
            }
          },
          {
            name: '集装箱',
            type: this.type,
            barWidth: '35%',
            smooth: false,
            data: t1,
            animationDuration,
            itemStyle: {
              normal: {
                color: '#ff5722',
                lineStyle: {
                  color: '#ff5722',
                  width: 2
                },
                areaStyle: {
                  color: '#e9effd'
                },
                label: {
                  show: true,
                  position: 'top',
                  textStyle: {
                    color: '#545555',
                    fontSize: 14
                  },
                  formatter: '{c}次'
                }
              }
            }
          }
        ]
      });
    }
  }
};
</script>
