<template>
  <div class="container">
    <div id="echartLine" ref="echartLine" />
  </div>
</template>

<script>
export default {
  // eslint-disable-next-line vue/require-prop-types
  props: ['lineData'],
  data() {
    return {
      myChart: null
    }
  },
  computed: {
    mapData() {
      if (this.lineData.length > 0) {
        return this.lineData
      } else {
        const nowDate = new Date()
        const y = nowDate.getFullYear()
        const m = nowDate.getMonth() + 1
        const mm = m < 10 ? '0' + m : m
        const d = nowDate.getDate()
        const dd = d < 10 ? '0' + d : d
        const date = `${y}-${mm}-${dd}`
        const data = [
          { date: date, successCount: 0, failCount: 0 }
        ]
        return data
      }
    }
  },
  watch: {
    mapData: {
      handler() {
        this.getLine()
      },
      deep: true
    }
  },
  mounted() {
    this.myChart = this.$echarts.init(this.$refs.echartLine)
    this.getLine()
  },
  methods: {
    getLine() {
      const dataName = this.mapData.map(val => {
        return val.date
      })
      const successData = this.mapData.map(val => {
        return val.successCount
      })
      const failData = this.mapData.map(val => {
        return val.failCount
      })
      const option = {
        title: {
          text: '实时调用情况'
        },
        legend: {
          data: ['成功数量', '失败数量']
        },
        color: ['#20bc64', '#ffa78f'],
        tooltip: {
          trigger: 'axis'
        },
        grid: {
          left: '5%',
          right: '5%',
          bottom: '0%',
          containLabel: true
        },
        xAxis: {
          boundaryGap: false,
          type: 'category',
          data: dataName
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '成功占比',
            data: successData,
            type: 'line',
            smooth: true,
            areaStyle: {}
          },
          {
            name: '失败占比',
            type: 'line',
            smooth: true,
            data: failData,
            areaStyle: {}
          }
        ]
      }
      this.myChart.setOption(option)
    }
  }
}
</script>

<style scoped>
.container {
  width: 100%;
  height: 225px;
  /* margin-top: 40px; */
}

#echartLine {
  width: 100%;
  height: 100%;
}
</style>
