<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>
        <el-col :span="11">
          <ve-histogram
            :data="chartData"
            :grid="grid"
            :extend="extend"
            :settings="chartSettings"
          />
        </el-col>
        <el-col :span="13">
          <normal-table
            :data="list"
            :height="height"
            :query="listQuery"
            :total="tableOption.total"
            :options="tableOption.options"
            :tools-option="tableOption.toolsOption"
            :list-loading="listLoading"
            size="small"
            @change="changePage"
          >
            <template slot="columns">
              <el-table-column
                v-for="column in columns"
                :key="column.value"
                :label="column.text"
                :width="column.width"
                :align="column.align"
                show-overflow-tooltip
              >
                <template slot-scope="scope">
                  <span :class="column.class">{{ scope.row[column.value] }}</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="60" align="center">
                <template slot-scope="scope">
                  <el-button type="text" size="small" @click="edit(scope.row)">
                    详情
                  </el-button>
                </template>
              </el-table-column>
            </template>
          </normal-table>
        </el-col>
      </el-col>
    </el-col>
    <detail-alarm-statics ref="editDeviceAlarm" @watchChild="fetchDeviceTypeBar" />
  </el-row>
</template>

<script>
import { getDeviceAlarmList } from '@/api/alarm/deviceStatics'
import detailAlarmStatics from './detailAlarmStatics'
export default {
  name: 'AlarmHistoryByType',
  components: { detailAlarmStatics },
  data() {
    return {
      height: 220,
      // 表格属性
      tableOption: {
        options: {
          needIndex: true // 是否需要序号列
        },
        toolsOption: {
          selectColumns: false, // 是否需要筛选列
          refresh: false // 是否需要刷新按钮
        },
        total: 0
      },
      // 是否需要序号列
      options: {
        needIndex: true
      },
      list: [],
      listLoading: false,
      columns: [
        {
          text: '报警类型',
          value: 'alarmType',
          align: 'center'
        },
        {
          text: '报警次数',
          value: 'alarmCount',
          width: '100',
          align: 'center'
        }
      ], // 显示列
      // 柱状图搜索条件
      timeRange: '',
      listQuery: {
        startTime: '',
        endTime: '',
        alarmType: '',
        offset: 1,
        limit: 20
      },
      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: {
          'alarmType': '报警类型',
          'alarmCount': '报警数量'
        },
        dimension: ['alarmType'],
        metrics: ['alarmCount']
      },
      chartData: {
        columns: ['alarmType', 'alarmCount'],
        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
        this.tableOption.total = response.total
        this.list = response.data
      })
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchDeviceTypeBar()
    },
    // 详情
    edit(row) {
      this.$refs.editDeviceAlarm.initDialog('detail', row)
    }
  }
}
</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>
