<template>
  <el-row>
    <el-col>
      <el-col>
        <div class="flagBoxStyle">
          <div class="flagStyle" />
          <div>报警总体情况</div>
        </div>
      </el-col>
      <el-col class="cardStyle">
        <el-col :span="10">
          <el-card>
            <div>
              <span class="ttileStyle">正在报警</span>
              <span class="countStyle">123</span>
              <span>个</span>
            </div>
          </el-card>
        </el-col>
        <el-col :span="10">
          <el-card>
            <div>
              <span class="ttileStyle">今日报警</span>
              <span class="countStyle">123</span>
              <span>个</span>
            </div>
          </el-card>
        </el-col>
      </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="timeRange1"
            type="datetimerange"
            range-separator="至"
            size="small"
            :picker-options="pickerOptions1"
            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-col :span="8">
          <div class="flagBoxStyle">
            <div class="flagStyle" />
            <div>报警趋势统计</div>
          </div>
        </el-col>
        <el-col :span="16">
          <el-date-picker
            v-model="timeRange2"
            type="datetimerange"
            range-separator="至"
            size="small"
            :picker-options="pickerOptions2"
            value-format="yyyy-MM-dd HH:mm:ss"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            @change="fetchDeviceTypeLine"
          />
        </el-col>
      </el-col>
      <el-col>
        <ve-line
          :data="chartDataLine"
          :settings="chartSettingsLine"
        />
      </el-col>
    </el-col>
  </el-row>
</template>

<script>
import { deviceStaticsByDept } from '@/api/alarm/deviceStatics'
import { alarmStaticsByContent } from '@/api/alarm/deviceStatics'
export default {
  name: 'AlarmStaticsByType',
  data() {
    return {
      // 柱状图搜索条件
      timeRange1: '',
      timeRange2: '',
      listQuery: {
        startTime: '',
        endTime: '',
        alarmType: ''
      },
      pickerOptions1: {
        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])
          }
        }]
      },
      // 折线图搜索条件
      listStatue: {
        startTime: '',
        endTime: '',
        alarmType: ''
      },
      pickerOptions2: {
        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])
          }
        }]
      },
      // 折线图
      chartSettingsLine: {
        labelMap: {
          'alarmWellData': '井盖开盖报警',
          'alarmLiquidData': '液位超限报警',
          'alarmHarmfulData': '有害气体超标报警'
        },
        metrics: ['alarmWellData', 'alarmLiquidData', 'alarmHarmfulData'],
        dimension: ['date']
      },
      chartDataLine: {
        columns: ['date', 'alarmWellData', 'alarmLiquidData', 'alarmHarmfulData'],
        rows: []
      },
      // 柱状图
      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': '报警内容',
          'totalAlarm': '报警数量',
          'cancelAlarm': '消警数量'
        },
        dimension: ['alarmContent'],
        metrics: ['totalAlarm', 'cancelAlarm']
      },
      chartData: {
        columns: ['alarmContent', 'totalAlarm', 'cancelAlarm'],
        rows: []
      }
    }
  },
  created() {
    this.fetchDeviceTypeBar()
    this.fetchDeviceTypeLine()
  },
  methods: {
    fetchDeviceTypeBar(val) {
      if (val !== undefined) {
        this.listQuery.startTime = val[0]
        this.listQuery.endTime = val[1]
      }
      deviceStaticsByDept(this.listQuery).then(response => {
        this.chartData.rows = response.data
      })
    },
    fetchDeviceTypeLine(val) {
      if (val !== undefined) {
        this.listQuery.startTime = val[0]
        this.listQuery.endTime = val[1]
      }
      alarmStaticsByContent(this.listStatue).then(response => {
        console.log(response.data, '===123')
        this.chartDataLine.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);
}
.cardStyle {
  display:flex;
  margin-bottom: 60px;
  justify-content: space-around;
}
.ttileStyle {
  font-size: 12px;
}
.countStyle {
  font-size: 20px;
  margin: 0 10px;
}
.el-date-editor--datetimerange.el-input,
.el-date-editor--datetimerange.el-input__inner{
  width: 340px;
}
</style>
