<template>
  <div>
    <div style="margin-bottom: 28px"/>
    <ve-line :data="chartData" :title="title" :settings="chartSettings" :extend="chartExtend"/>
  </div>
</template>

<script>
import { countbyDay } from '@/api/statistics'
import { getDayTime } from '@/utils/dateutils'

export default {
  name: 'PassSevenDay',
  data() {
    this.title = {
      text: '七日通行趋势'
    }
    this.chartSettings = {
      labelMap: {
        'total': '通行人数'
      },
      metrics: ['total'],
      dimension: ['date']
    }
    this.chartExtend = {
      yAxis: { minInterval: 1 }
    }
    return {
      chartData: {
        columns: ['date', 'total'],
        rows: []
      }
    }
  },
  mounted() {
    this.fetchData()
  },
  activated() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      const startDate = getDayTime(new Date().getTime() - 24 * 6 * 60 * 60 * 1000).Format('yyyy-MM-dd')
      const endDate = getDayTime(new Date().getTime()).Format('yyyy-MM-dd')
      // const startDate = '2020-05-10'
      // const endDate = '2020-05-16'
      const listQuery = {
        startDate: startDate,
        endDate: endDate,
        personType: ''
      }
      countbyDay(listQuery).then(response => {
        const data = response.data
        var hasData = false
        if (data.length !== 0) {
          for (var i = 0; i < data.length; i++) {
            if (parseInt(data[i].total) !== 0) {
              hasData = true
            }
          }
        }
        if (!hasData) {
          this.chartExtend.yAxis.max = 10
        }
        this.chartData.rows = data
      })
    }
  }
}
</script>
