<template>
  <schema-table
    ref="table"
    :schema="schema"
  >
    <preview-dialog ref="previewDialog" />
  </schema-table>
</template>

<script>
import { getDeviceAlarms } from '@/api/device'
import { createListOptions } from '@/utils'
import {
  AssetType,
  AlarmStrategy,
  AlarmStrategies
} from '@/constant'

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

export default {
  name: 'DeviceAlarm',
  props: {
    device: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      options: createListOptions({ deviceId: this.device.id }),
      schema: {
        keepalive: true,
        condition: { deviceId: this.device.id },
        list: getDeviceAlarms,
        transform: this.transform,
        cols: [
          { type: 'refresh' },
          { prop: 'asset', label: '截图', type: 'asset', on: this.onView },
          { prop: 'type', label: '类型' },
          { prop: 'handle', label: '处理方式' },
          { prop: 'status', label: '处理结果', type: 'tag' },
          ...AlarmStrategies.map(key => {
            return {
              prop: key,
              label: `${AlarmStrategy[key]}通知`,
              type: 'tag'
            }
          }),
          { prop: 'happenTime', label: '时间', 'min-width': 120 }
        ]
      }
    }
  },
  methods: {
    transform ({ pic, picUrl, type, handle, status, happenTime, ...data }) {
      const alarm = {
        asset: pic && picUrl
          ? {
            type: AssetType.IMAGE,
            url: picUrl
          }
          : null,
        type: this.getType(type),
        handle: ['应用重启', '设备重启', '恢复出厂', '未干预'][handle] || '-',
        status: handle <= 2 && status <= 2
          ? {
            type: ['primary', 'success', 'danger'][status],
            label: ['处理中', '成功', '失败'][status]
          }
          : null,
        happenTime
      }
      AlarmStrategies.forEach(key => {
        alarm[key] = this.getTag(data[key])
      })
      return alarm
    },
    getType (type) {
      if (type >= 1101 && type <= 1116) {
        return '分割器异常'
      }
      return Alarm[type] || '-'
    },
    getTag (value) {
      switch (value) {
        case 0:
          return {
            type: 'danger',
            label: '否'
          }
        case 1:
          return {
            type: 'success',
            label: '是'
          }
        default:
          return null
      }
    },
    onView (asset) {
      this.$refs.previewDialog.show(asset)
    }
  }
}
</script>
