<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
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    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() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({expectedData, actualData,expedData} = {}) {
      const week = () =>{
        let today = new Date();
        let firstDayOfYear = new Date(today.getFullYear(), 0, 1);
        let dayOfWeek = firstDayOfYear.getDay();
        let spendDay = 1;
        if (dayOfWeek != 0) {
          spendDay = 7 - dayOfWeek + 1;
        }
        firstDayOfYear = new Date(today.getFullYear(), 0, spendDay);
        let d = Math.ceil((today.valueOf() - firstDayOfYear.valueOf()) / 86400000);
        return Math.ceil((d / 7) + 1)
      }
      this.chart.setOption({
        title: {
          text: `第${week()}周产量`,
          textStyle: {
            align: 'center',
            color: '#000',
            fontSize: 20,
          },
          top: '0%',
          left: '0%',
        },
        xAxis: {
          data: ['2023-12-11', '2023-12-12', '2023-12-13',  '2023-12-14', '2023-12-15', '2023-12-16', '2023-12-17'],
          // boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 50,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: [
          {
            axisTick: {
              show: false
            }
          },
          {
            axisTick: {
              show: false
            }
          }
        ],
        legend: {
          data: ['expected', 'actual']
        },
        series: [
          {
            name: '订单违约率',
            type: 'bar',
            yAxisIndex:1,
            itemStyle: {
              normal: {
                color: '#3888fa',
                lineStyle: {
                  color: '#3888fa',
                  width: 2
                },
                areaStyle: {
                  color: '#f3f8ff'
                }
              }
            },
            data: expectedData,
            animationDuration: 1000,
            animationEasing: 'quadraticOut'
          },
          {
            name: '产量',
            smooth: false,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#3888fa',
                lineStyle: {
                  color: '#3888fa',
                  width: 2
                },
                areaStyle: {
                  color: '#f3f8ff'
                }
              }
            },
            data: expedData,
            animationDuration: 1000,
            animationEasing: 'quadraticOut'
          }
        ]
      })
    }
  }
}
</script>
