<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'
import { listOrders } from '@/api/canteen/order'
import { getHomeData } from '@/api/yiqing/home'

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
    },
    queryParams: {
      type: Object
    }
  },

  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.$nextTick(() => {
          this.draw()(val)
          //  this.initChart();
        })
      }
    }
  },

  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },

  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },

  methods: {
    initChart() {
      this.draw()
      //this.loading = true;
      this.chart = echarts.init(this.$el, 'macarons')
    },

    /*   setOptions({optiondate,optionOrdernum} = {}) {
         console.log("33333333"+optiondate),
         this.chart.setOption({
           xAxis: {
             data: optiondate,
             boundaryGap: false,
             name:'日期',
             axisTick: {
               show: false
             }
           },
           grid: {
             left: 10,
             right: 40,
             bottom: 20,
             top: 30,
             containLabel: true
           },
           tooltip: {
             trigger: 'axis',
             axisPointer: {
               type: 'cross'
             },
             padding: [5, 10]
           },
           yAxis: {
             name:'人数',
             axisTick: {
               show: false
             }
           },
           legend: {
             data: ['expected']
           },
           series: [{
             name: 'expected', itemStyle: {
               normal: {
                 color: '#FF005A',
                 lineStyle: {
                   color: '#FF005A',
                   width: 2
                 }
               }
             },
             smooth: true,
             type: 'line',
             data: optionOrdernum,
             animationDuration: 2800,
             animationEasing: 'cubicInOut'
           }]
         })
       },
   */

    draw() {
      getHomeData(this.queryParams).then(response => {
        var dateList = response.data.dateList
        var count = response.data.count
        this.chartData.optiondate = dateList
        //rotate倾斜度 -90 至 90 默认为0
        this.chart.setOption({
          xAxis: {
            data: dateList,
            rotate: 45,
            boundaryGap: false,
            name: '日期',
            axisTick: {
              show: false
            },
            axisLabel: {
              interval: 0,
              rotate: 65,
              margin: 9,
              textStyle: {
                color: '#000000'
              }
            }
          },
          grid: {
            left: 17,
            right: 40,
            bottom: 20,
            top: 30,
            containLabel: true
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross'
            },
            padding: [5, 10]
          },
          yAxis: {
            name: '人数',
            axisTick: {
              show: false
            }
          },
          legend: {
            data: ['total']
          },
          series: [
            {
              name: '总管控人数',
              itemStyle: {
                normal: {
                  color: '#36a3f7',
                  lineStyle: {
                    color: '#36a3f7',
                    width: 2
                  }
                }
              },
              smooth: true,
              type: 'line',
              data: count.sum,
              animationDuration: 2800,
              animationEasing: 'cubicInOut'
            },
            {
              name: '阳性（含复阳）',
              itemStyle: {
                normal: {
                  color: '#f4516c',
                  lineStyle: {
                    color: '#f4516c',
                    width: 2
                  }
                }
              },
              smooth: true,
              type: 'line',
              data: count.yang,
              animationDuration: 2800,
              animationEasing: 'cubicInOut'
            },
            {
              name: '正常（含阳性治愈）',
              itemStyle: {
                normal: {
                  color: '#34bfa3',
                  lineStyle: {
                    color: '#34bfa3',
                    width: 2
                  }
                }
              },
              smooth: true,
              type: 'line',
              data: count.yin,
              animationDuration: 2800,
              animationEasing: 'cubicInOut'
            }
          ]
        })

      })
    }

  }
}
</script>
