<template>
  <div>
    <div id="acceptLine" ref="acceptLine" :style="{ height: height + 'px'}"></div>
  </div>
</template>

<script>
import { getAcceptList } from '@/api/eventlist'
import echarts from 'echarts'
import { on, off } from '@/libs/tools'
// import tdTheme from '../theme.json'
// echarts.registerTheme('tdTheme', tdTheme)

export default {
  name: 'AcceptLineEchart',
  data () {
    return {
      chartData: []
    }
  },
  props: {
    width: {
      type: Number,
      default: () => {
        return 500
      }
    },
    height: {
      type: Number,
      default: () => {
        return 500
      }
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.chart()
      on(window, 'resize', this.$refs.acceptLine.resize())
    })
  },
  methods: {
    resize () {
      this.$refs.acceptLine.resize()
    },
    chart () {
      getAcceptList().then(ret => {
        if (ret.data.errcode === 0) {
          this.chartData = ret.data.data
          // 数据处理
          this.chartData.forEach(item => {
          })
          let option = {
            title: {
              text: '受理折线图',
              x: 'center',
              textStyle: {
                color: '#516b91'
              }
            },
            textStyle: {
              color: '#516b91'
            },
            legend: {
              // align: 'right'
              right: '0'
            },
            tooltip: {
              trigger: 'axis',
              backgroundColor: 'rgba(50,50,50,0.6)',
              axisPointer: { // 坐标轴指示器，坐标轴触发有效
                type: 'line' // 默认为直线，可选为：'line' | 'shadow'
              }
            },
            dataset: {
              dimensions: ['datename', 'num'],
              source: []
            },
            yAxis: {
              // name: '单位：',
              type: 'value',
              axisLine: {
                lineStyle: {
                  color: '#ccc' // 轴及其文字颜色
                }
              }
            },
            xAxis: {
              type: 'category',
              axisLabel: {
                // interval: 0,
                // rotate: 40
              },
              axisLine: {
                lineStyle: {
                  color: '#ccc' // 轴及其文字颜色
                }
              }
            },
            series: [
              {
                name: '受理量',
                type: 'line',
                smooth: true,
                symbolSize: 8,
                label: {
                  normal: {
                    show: true,
                    position: 'inside',
                    color: ['#fff'],
                    formatter: function (params) {
                      if (params.value.outstate < 0) {
                        return params.value.outstate
                      } else {
                        return ''
                      }
                    }
                  }
                },
                itemStyle: {
                  // 通常情况下：
                  normal: {
                    color: ['#57B1EE']
                  },
                  // 鼠标悬停时：
                  emphasis: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                  }
                }
              }
            ]
          }
          this.chartData.forEach(item => {
            // area为空的是之前导入的数据（暂不显示）
            if (item.num) {
              option.dataset.source.push(item)
            }
          })
          let myChart = echarts.init(document.getElementById('acceptLine'))
          myChart.setOption(option)
        } else {
          this.$Message.error({
            content: '数据获取失败,\n' + ret.data.errmsg
          })
        }
      })
    }
  },
  beforeDestroy () {
    off(window, 'resize', this.$refs.acceptLine.resize)
  }
}
</script>

<style scoped>
  .header {
    height: 30px;
  }

  .icon {
    font-size: 28px;
    font-weight: 600;
    margin-right: 5px;
  }

  .title {
    font-size: 16px;
    font-weight: 600;
    line-height: 30px;
  }
</style>
