<template>
  <div>
    <div>
      <div class="statistic-container">
        <span id="s6T" class="chart-tool-button" @click="fetchData('today')">今日</span>
        <span id="s6Y" class="chart-tool-button" @click="fetchData('yesterday')">昨日</span>
        <span id="s6L" 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 { getDayTime } from '@/utils/dateutils'
import { peopleCountByDept } from '@/api/statistics'

export default {
  name: 'PassAnalysis',
  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 {
      isLegendVisible: true,
      isTipVisible: true,
      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('s6T').classList.add('selected')
      document.getElementById('s6Y').classList.remove('selected')
      document.getElementById('s6L').classList.remove('selected')
      if (type === 'yesterday') {
        document.getElementById('s6T').classList.remove('selected')
        document.getElementById('s6Y').classList.add('selected')
        document.getElementById('s6L').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('s6T').classList.remove('selected')
        document.getElementById('s6Y').classList.remove('selected')
        document.getElementById('s6L').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
      }
      peopleCountByDept(listQuery).then(response => {
        var data = response.data
        // data = [
        //   { 'deptId': 'ww9', 'deptName': 'ww', 'count': '27' },
        //   { 'deptId': 'ww9', 'deptName': 'ww2', 'count': '27' },
        //   { 'deptId': 'ww9', 'deptName': 'ww3', 'count': '27' },
        //   { 'deptId': 'ww9', 'deptName': 'ww11', 'count': '27' },
        //   { 'deptId': 'ww9', 'deptName': 'ww12', 'count': '27' },
        //   { 'deptId': 'ww9', 'deptName': 'ww13', 'count': '27' },
        //   { 'deptId': 'ww9', 'deptName': 'ww21', 'count': '27' },
        //   { 'deptId': 'ww9', 'deptName': 'ww33', 'count': '27' },
        //   { 'deptId': 'ww9', 'deptName': 'ww33', 'count': '27' },
        //   { 'deptId': 'ww9', 'deptName': 'ww4', 'count': '28' }
        // ]
        if (data.length !== 0) {
          this.legends.show = true
          this.tooltip.show = true
          this.chartSettings.label.normal.show = true
          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>
