<template>
  <div>
    <div>
      <div class="statistic-container">
        <span id="s8T" class="chart-tool-button" @click="fetchData('today')">今日</span>
        <span id="s8Y" class="chart-tool-button" @click="fetchData('yesterday')">昨日</span>
        <span id="s8L" class="chart-tool-button" @click="fetchData('lastSevenDays')">近7日</span>
      </div>
      <ve-pie :data="chartData" :title="title" :legend="legends" :tooltip="tooltip" :settings="chartSettings"/>
    </div>
  </div>
</template>

<script>
import { printCountByDept } from '@/api/statistics'
import { getDayTime } from '@/utils/dateutils'
export default {
  name: 'PrintAnalysis',
  data() {
    this.title = {
      text: '各部门打印情况'
    }
    this.chartSettings = {
      limitShowNum: 5,
      label: {
        normal: {
          show: true
        }
      }
    }
    this.legends = {
      show: true,
      orient: 'horizontal',
      x: 'right', // 可设定图例在左、右、居中
      padding: [0, 0, 0, 100]
    }
    this.tooltip = {
      show: true
    }
    return {
      chartData: {
        columns: ['deptName', 'count'],
        rows: []
      }
    }
  },
  mounted() {
    this.fetchData()
  },
  activated() {
    this.fetchData()
  },
  methods: {
    fetchData(type) {
      var startTime = getDayTime(new Date().getTime()).Format('yyyy-MM-dd') + ' 00:00:00'
      var endTime = getDayTime(new Date().getTime()).Format('yyyy-MM-dd') + ' 23:59:59'
      document.getElementById('s8T').classList.add('selected')
      document.getElementById('s8Y').classList.remove('selected')
      document.getElementById('s8L').classList.remove('selected')
      if (type === 'yesterday') {
        document.getElementById('s8T').classList.remove('selected')
        document.getElementById('s8Y').classList.add('selected')
        document.getElementById('s8L').classList.remove('selected')
        startTime = getDayTime(new Date().getTime() - 24 * 60 * 60 * 1000).Format('yyyy-MM-dd') + ' 00:00:00'
        endTime = getDayTime(new Date().getTime() - 24 * 60 * 60 * 1000).Format('yyyy-MM-dd') + ' 23:59:59'
      } else if (type === 'lastSevenDays') {
        document.getElementById('s8T').classList.remove('selected')
        document.getElementById('s8Y').classList.remove('selected')
        document.getElementById('s8L').classList.add('selected')
        startTime = getDayTime(new Date().getTime() - 24 * 6 * 60 * 60 * 1000).Format('yyyy-MM-dd') + ' 00:00:00'
        endTime = getDayTime(new Date().getTime()).Format('yyyy-MM-dd') + ' 23:59:59'
      }
      // const startTime = '2020-06-01 00:00:00'
      // const endTime = '2020-06-20 00:00:00'
      const listQuery = {
        startTime: startTime,
        endTime: endTime
      }
      printCountByDept(listQuery).then(response => {
        const data = response.data
        this.legends.show = true
        this.tooltip.show = true
        this.chartSettings.label.normal.show = true
        if (data.length !== 0) {
          if (data.length === 1 && data[0].count === 0) {
            this.legends.show = false
            this.tooltip.show = false
            this.chartSettings.label.normal.show = false
          }
          for (var i = 0; i < data.length; i++) {
            data[i].count = parseInt(data[i].count)
          }
          this.chartData.rows = data
        }
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .statistic-container {
    /*padding:0px;*/
    margin-bottom: 10px;
    /*border: 1px solid #d3dce6;*/
    text-align: center;
    .chart-tool-button {
      padding: 5px;
      line-height: 16px;
      margin-top: 5px;
      font-size: 14px;
    }
    .chart-tool-button:hover {
      background-color: #8cc5ff;
      cursor: pointer;
      color: white;
    }
    .chart-tool-button:active {
      background-color: #8cc5ff;
      color: white;
    }
  }
  .selected {
    background-color: #8cc5ff;
    color: white;
  }
</style>
