<template>
  <div class="card">
    <div class="card-header">
      <div class="card-title">
        <span class="card-title-icon">
          <Icon icon="fa-solid:bell" />
        </span>
        <span>实时报警信息</span>
      </div>
      <span class="tag">滚动展示</span>
    </div>
    <div class="card-body">
      <ul ref="listRef" class="alarm-list">
        <li
          v-for="(a, index) in alarms"
          :key="index"
          class="alarm-item"
          :class="[a.type, { 'animating-out': index === 0 && isAnimating }]"
          :title="a.msg"
        >
          <span class="alarm-time">[{{ a.time }}]</span>
          <span class="alarm-msg">{{ a.msg }}</span>
          <span class="alarm-level">{{ a.level }}</span>
        </li>
      </ul>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { useRoute } from 'vue-router'
import { DeviceWarningRecordApi, DeviceWarningRecordVO } from '@/api/iot/deviceWarningRecord'

const route = useRoute()
const orgId = route.query.orgId

const alarms = ref<any[]>([])
const isAnimating = ref(false)
let timer: ReturnType<typeof setInterval> | null = null

const getAlarms = async () => {
  try {
    const data = (await DeviceWarningRecordApi.getList({ orgId })) || []
    alarms.value = data.map((item: DeviceWarningRecordVO) => {
      let timeStr = ''
      if (item.createTime) {
        const d = new Date(item.createTime)
        timeStr = d.toTimeString().slice(0, 8)
      }

      let type = 'safe'
      let levelText = item.alarmLevel

      if (item.alarmLevel === '2') {
        type = 'danger'
        levelText = '严重'
      } else if (item.alarmLevel === '1') {
        type = 'warn'
        levelText = '警告'
      } else if (item.alarmLevel === '0') {
        type = 'safe'
        levelText = '提示'
      } else {
        if (item.alarmLevel === '严重') type = 'danger'
        else if (item.alarmLevel === '警告') type = 'warn'
        else if (item.alarmLevel === '提示') type = 'safe'
      }

      return {
        time: timeStr,
        level: levelText,
        type,
        msg: `${item.deviceName}-${item.ruleName}`
      }
    })

    startAnimation()
  } catch (error) {
    console.error('Failed to fetch alarms:', error)
  }
}

const startAnimation = () => {
  if (timer) clearInterval(timer)
  if (alarms.value.length === 0) return

  timer = setInterval(() => {
    isAnimating.value = true
    setTimeout(() => {
      const first = alarms.value.shift()
      if (first) {
        alarms.value.push(first)
      }
      isAnimating.value = false
    }, 360)
  }, 3000)
}

onMounted(() => {
  getAlarms()
})

onUnmounted(() => {
  if (timer) clearInterval(timer)
})
</script>

<style scoped>
.card {
  background: linear-gradient(135deg, rgba(15,23,42,0.96), rgba(15,23,42,0.88));
  border-radius: 8px;
  border: 1px solid rgba(30,64,175,0.85);
  box-shadow:
    0 18px 45px rgba(15,23,42,0.95),
    0 0 0 1px rgba(15,23,42,1),
    inset 0 0 0 1px rgba(56,189,248,0.05);
  position: relative;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.card::before,
.card::after {
  content: "";
  position: absolute;
  width: 12px;
  height: 12px;
  border-radius: 2px;
  border: 1px solid rgba(56,189,248,0.75);
  opacity: 0.6;
  pointer-events: none;
}

.card::before {
  top: -1px;
  left: -1px;
  border-right: none;
  border-bottom: none;
}

.card::after {
  right: -1px;
  bottom: -1px;
  border-left: none;
  border-top: none;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
  padding: 10px 12px 4px;
  border-bottom: 1px solid rgba(41, 54, 95, 0.9);
}

.card-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
  font-weight: 700;
  color: #e5f0ff;
}

.card-title-icon {
  color: #22d3ee;
}

.card-body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 12px 12px;
  overflow: hidden;
}

.tag {
  border-radius: 999px;
  padding: 2px 6px;
  font-size: 10px;
  border: 1px solid rgba(148,163,184,0.4);
  color: #94a3b8;
}

.alarm-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  height: 100%;
  overflow: hidden;
}

.alarm-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 8px;
  border-radius: 6px;
  background: rgba(15,23,42,0.92);
  border: 1px solid rgba(30,64,175,0.9);
  font-size: 11px;
  color: #e5f0ff;
  transition: background 0.3s, box-shadow 0.3s;
  flex-shrink: 0;
}

.alarm-item.danger {
  border-left: 3px solid #ef4444;
  box-shadow: 0 0 18px rgba(239,68,68,0.4);
}

.alarm-item.warn {
  border-left: 3px solid #f59e0b;
  box-shadow: 0 0 14px rgba(245,158,11,0.35);
}

.alarm-item.safe {
  border-left: 3px solid #22c55e;
  box-shadow: 0 0 12px rgba(34,197,94,0.35);
}

.alarm-time {
  width: 64px;
  flex: 0 0 auto;
  color: #94a3b8;
}

.alarm-msg {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.alarm-level {
  flex: 0 0 auto;
  border-radius: 999px;
  padding: 2px 6px;
  border: 1px solid rgba(148,163,184,0.6);
  font-size: 10px;
}

.animating-out {
  transition: all 0.35s;
  transform: translateY(-48px);
  opacity: 0;
}
</style>
