<template>
  <panel-card title="各单位报警统计">
    <template slot="func">
      <div>
        <time-buttons ref="timeButtons" @change="changeTime" />
        <el-date-picker
          v-model="timeRange"
          type="datetimerange"
          range-separator="至"
          size="small"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          @change="fetchData"
        />
      </div>
    </template>
    <div style="margin-top: 10px;">
      <ve-histogram
        :loading="loading"
        :data="chartData"
        :settings="chartSettings"
        :extend="extend"
        :data-empty="dataEmpty"
      />
    </div>
  </panel-card>
</template>

<script>
import TimeButtons from '@/components/BigData/TimeButtons'
import { alarmStaticByDept } from '@/api/data/dataStatics'
import PanelCard from '@/components/BigData/Card/panelCard'
export default {
  name: 'AlarmStaticsByDept',
  components: { PanelCard, TimeButtons },
  data() {
    return {
      // 柱状图搜索条件
      timeRange: [],
      listQuery: {
        beginTime: '',
        endTime: '',
        deviceType: ''
      },
      // 柱状图
      extend: {
        xAxis: {
          axisLabel: {
            rotate: 30,
            margin: 30,
            textStyle: {
              align: 'center'
            }
          }
        },
        series: {
          label: { show: true, position: 'top' },
          itemStyle: {
            normal: {
              barBorderRadius: [3, 3, 0, 0]
            }
          },
          barMaxWidth: 35
        }
      },
      grid: {
        right: 60
      },
      chartSettings: {
        itemStyle: {
          'barCategoryGap': 5
        },
        barWidth: 15,
        labelMap: {
          'deptName': '权属单位',
          'alarmDevices': '报警设备数',
          'alarmTimes': '报警次数'
        }
      },
      chartData: {
        columns: ['deptName', 'alarmDevices', 'alarmTimes'],
        rows: []
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 时间切换
    changeTime(timeRange) {
      this.timeRange = timeRange
      this.fetchData()
    },
    fetchData() {
      this.handleDateTime()
      alarmStaticByDept(this.listQuery).then(response => {
        this.chartData.rows = response.data
        const maxValue = Math.max.apply(Math, response.data.map(function(item) { return item.alarmTimes }))
        if (maxValue < 10) {
          this.extend.yAxis = { max: 10 }
        } else {
          this.extend.yAxis = {}
        }
        this.listLoading = false
      })
    },
    // 处理时间
    handleDateTime() {
      if (this.timeRange && this.timeRange.length > 0) {
        this.listQuery.beginTime = this.timeRange[0]
        this.listQuery.endTime = this.timeRange[1]
      } else {
        this.listQuery.beginTime = ''
        this.listQuery.endTime = ''
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.flagBoxStyle {
  display: flex;
  margin-bottom: 20px;
}
.flagBoxStyle div:nth-child(2){
  line-height: 30px;
  font-weight: 600;
}
.flagStyle {
  width: 4px;
  height: 30px;
  margin-right: 6px;
  background: rgb(64 121 242);
}
.el-date-editor--datetimerange.el-input,
.el-date-editor--datetimerange.el-input__inner{
  width: 340px;
}
</style>
