<template>
  <panel-card title="报警历史分析">
    <template slot="func">
      <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="fetchData"
      />
    </template>
    <div class="flex-body">
      <div style="flex:1">
        <ve-histogram
          :data="chartData"
          :grid="grid"
          :extend="extend"
          :settings="chartSettings"
        />
      </div>
      <div class="table">
        <el-table :data="list">
          <el-table-column prop="alarmContent" label="报警类型" width="180" />
          <el-table-column prop="alarmCount" label="报警数" width="100" />
          <el-table-column label="" width="60" align="center">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="detail(scope.row)">
                详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </panel-card>
</template>

<script>
import { alarmStaticsByContent } from '@/api/data/dataStatics'
import detailAlarmStatics from './detailAlarmStatics'
import PanelCard from '@/components/BigData/Card/panelCard'
import { getSearchLastWeekTime } from '@/utils/dateutils'
export default {
  name: 'AlarmHistoryByType',
  components: { PanelCard, detailAlarmStatics },
  data() {
    return {
      height: 220,
      list: [],
      listLoading: false,
      // 柱状图搜索条件
      timeRange: [],
      listQuery: {
        beginTime: '',
        endTime: '',
        alarmType: '1'
      },
      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: {
          'alarmContent': '报警类型',
          'alarmCount': '报警数量'
        },
        dimension: ['alarmContent'],
        metrics: ['alarmCount']
      },
      chartData: {
        columns: ['alarmContent', 'alarmCount'],
        rows: []
      }
    }
  },
  created() {
    this.timeRange = getSearchLastWeekTime()
    this.fetchData()
  },
  methods: {
    // 获取列表数据
    fetchData() {
      this.handleDateTime()
      alarmStaticsByContent(this.listQuery).then(response => {
        this.chartData.rows = response.data
        this.list = response.data
      })
    },
    // 处理时间
    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 = ''
      }
    },
    // 详情
    detail(row) {
      this.$refs.editDeviceAlarm.initDialog('detail', row)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.flex-body{
  padding: 15px 10px;
  display: flex;
  justify-content: space-between;
  .table{
    width: 350px;
  }
}
</style>
