<template>
  <box
    title="设备告警"
    @fullscreen="onFullscreen"
  >
    <div
      class="l-flex__fill l-flex--col c-device-dashboard-alarm"
      :class="{ fullscreen }"
    >
      <div class="l-flex__none l-flex--row c-device-dashboard-alarm__header">
        <div
          class="l-flex__fill"
          style="flex-grow: 2;"
        >
          时间
        </div>
        <div
          class="l-flex__fill"
          style="flex-grow: 1.5;"
        >
          内容
        </div>
        <div class="l-flex__fill">
          处理方式
        </div>
        <div class="l-flex__fill">
          处理结果
        </div>
      </div>
      <div class="l-flex__fill u-relative">
        <div
          class="c-device-dashboard-alarm__list"
          :class="{ mini: !fullscreen }"
        >
          <vue-seamless-scroll
            :data="listData"
            :class-option="classOption"
          >
            <div
              v-for="item in listData"
              :key="item.id"
              class="l-flex--row c-device-dashboard-alarm__item"
            >
              <div
                class="l-flex__fill"
                style="flex-grow: 2;"
              >
                {{ item.happenTime }}
              </div>
              <div
                class="l-flex__fill"
                style="flex-grow: 1.5;"
              >
                {{ item.type }}
              </div>
              <div class="l-flex__fill">{{ item.handle }}</div>
              <div class="l-flex__fill">{{ item.status }}</div>
            </div>
          </vue-seamless-scroll>
        </div>
      </div>
    </div>
  </box>
</template>

<script>
import Box from './Box'
import { getDeviceAlarms } from '@/api/device'
import vueSeamlessScroll from './vue-seamless-scroll.min.js'

const Alarms = {
  0: '疑似黑屏',
  1: '设备离线',
  2: '屏幕拓扑结构异常',
  3: '播放非法视频',
  4: '接收卡离线',
  5: '发送控制设备离线',
  6: '浪潮智能网关离线',
  7: '屏幕监控摄像头离线',
  8: '人流监测摄像头离线',
  9: '设备上线',
  1000: '分割器异常'
}

function getAlarmType (type) {
  if (type >= 1101 && type <= 1116) {
    return '分割器异常'
  }
  return Alarms[type] || '-'
}

export default {
  components: {
    Box,
    vueSeamlessScroll
  },
  props: {
    device: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      fullscreen: false,
      error: false,
      classOption: {
        step: 0.5,
        hoverStop: false
      },
      listData: []
    }
  },
  created () {
    this.getDeviceAlarms()
  },
  methods: {
    onFullscreen (fullscreen) {
      this.fullscreen = fullscreen
      if (this.error) {
        this.getDeviceAlarms()
      }
    },
    getDeviceAlarms () {
      getDeviceAlarms({
        pageIndex: 1,
        pageSize: 100,
        deviceId: this.device.id
      }).then(
        ({ data }) => {
          this.error = false
          this.listData = data.map(item => {
            return {
              ...item,
              type: getAlarmType(item.type),
              handle: ['应用重启', '设备重启', '恢复出厂', '未干预'][item.handle] || '-',
              status: item.handle <= 2 && item.status <= 2
                ? ['处理中', '成功', '失败'][item.status] || '-'
                : '-'
            }
          })
        },
        () => {
          this.error = true
        }
      )
    }
  }
}
</script>

<style lang="scss" scoped>
.c-device-dashboard-alarm {
  &__header {
    color: #9ea9cd;
    background-color: #232b45;
  }

  &__list {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
  }

  &__item {
    color: #ffffff;
    border-bottom: 1px solid #9ea9cd;
  }

  &__header,
  &__item {
    font-size: 12px;
    line-height: 32px;
    text-align: center;
  }

  &.fullscreen {
    .c-device-dashboard-alarm__header,
    .c-device-dashboard-alarm__item {
      font-size: 20px;
      line-height: 64px;
    }
  }
}
</style>
