<template>
  <el-row>
    <el-col>
      <el-col>
        <el-col :span="8">
          <div class="flagBoxStyle">
            <div class="flagStyle" />
            <div>各单位报警统计</div>
          </div>
        </el-col>
        <el-col :span="16">
          <el-date-picker
            v-model="timeRange"
            type="datetimerange"
            range-separator="至"
            size="small"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd HH:mm:ss"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            @change="fetchDeviceTypeBar"
          />
        </el-col>
      </el-col>
      <el-col>
        <ve-histogram
          :data="chartData"
          :grid="grid"
          :extend="extend"
          :settings="chartSettings"
        />
      </el-col>
    </el-col>
  </el-row>
</template>

<script>
import { getDeviceAlarmList } from '@/api/alarm/deviceStatics'
export default {
  name: 'AlarmStaticsByDept',
  data() {
    return {
      // 柱状图搜索条件
      timeRange: '',
      listQuery: {
        startTime: '',
        endTime: '',
        alarmType: ''
      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      // 柱状图
      extend: {
        xAxis: {
          axisLabel: {
            rotate: 30,
            margin: 30,
            textStyle: {
              align: 'center'
            }
          }
        },
        series: {
          label: { show: true, position: 'top' },
          barMaxWidth: 35 // 最大柱宽度
        }
      },
      grid: {
        right: 60
      },
      chartSettings: {
        itemStyle: {
          'barCategoryGap': 5
        },
        barWidth: 15,
        labelMap: {
          'deptName': '报警内容',
          'alarmCount': '报警数',
          'count': '报警设备数'
        },
        dimension: ['deptName'],
        metrics: ['alarmCount', 'count']
      },
      chartData: {
        columns: ['deptName', 'alarmCount', 'count'],
        rows: []
      }
    }
  },
  created() {
    this.fetchDeviceTypeBar()
  },
  methods: {
    fetchDeviceTypeBar(val) {
      if (val !== undefined) {
        this.listQuery.startTime = val[0]
        this.listQuery.endTime = val[1]
      }
      getDeviceAlarmList(this.listQuery).then(response => {
        this.chartData.rows = response.data
      })
    }
  }
}
</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>
