<template>
  <div class="panel alarm-info">
    <!-- Animated border beam -->
    <div class="border-beam"></div>
    <!-- Panel header -->
    <div class="panel-header">
      <div class="panel-header-icon">&#x26A0;&#xFE0F;</div>
      <span class="panel-title">实验室实时风险预警</span>
      <div class="header-right">
        <!--<div class="header-count">-->
          <!--<span class="count-label">本月</span>-->
          <!--<span class="count-value">{{ totalCount }}</span>-->
          <!--<span class="count-label">次</span>-->
        <!--</div>-->
        <div class="header-badge emergency">
          <span class="badge-icon">🚨</span>
          <span class="badge-val">{{ emergencyCount }}</span>
          <span class="badge-lbl">应急</span>
        </div>
        <div class="header-badge violation">
          <span class="badge-icon">☣️</span>
          <span class="badge-val">{{ violationCount }}</span>
          <span class="badge-lbl">违规带离</span>
        </div>
      </div>
    </div>
    <!-- Scrolling warning list -->
    <div class="warn-scroll-wrap">
      <div class="warn-scroll-inner" :class="{ scrolling: shouldScroll }">
        <div
          v-for="(item, idx) in scrollList"
          :key="idx"
          class="warn-item"
        >
          <div class="warn-item-head">
            <span class="warn-cat" :class="item.category === '应急预警' ? 'emergency' : 'violation'"><span>{{item.category === '应急预警' ? '🚨️️ ':'☣️ '}}</span>{{ item.category }}</span>
            <span class="warn-lab">{{ item.lab }}</span>
            <span class="warn-time">{{ item.time }}</span>
          </div>
          <div class="warn-detail">{{ item.content }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getRiskWarning } from '@/api/screen'


export default {
  name: 'AlarmInfo',
  data() {
    return {
      totalCount: 0,
      emergencyCount: 3,
      violationCount: 1,
      warningList: [],
      pollTimer: null
    }
  },
  computed: {
    shouldScroll() {
      return this.warningList.length > 5
    },
    scrollList() {
      return this.shouldScroll
        ? [...this.warningList, ...this.warningList]
        : [...this.warningList]
    }
  },
  mounted() {
    this.fetchData()
    this.pollTimer = setInterval(this.fetchData, 5 * 60 * 1000)
  },
  beforeDestroy() {
    if (this.pollTimer) clearInterval(this.pollTimer)
  },
  methods: {
    async fetchData() {
      try {
        const res = await getRiskWarning()
        if (res.code === 200) {
          const d = res.data
          this.totalCount     = d.monthAlarmCount
          this.emergencyCount = d.emergencyCount
          this.violationCount = d.violationCount
          this.warningList = (d.eventList || []).map(e => ({
            lab:      e.labInfo || e.subName,
            category: e.category || '',
            content:  e.content || e.eventName,
            time:     e.eventStartTime ? e.eventStartTime.replace('T', ' ') : '',
            active:   e.eventStatus
          }))
        }
      } catch (e) {
        // 错误已由拦截器处理
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.alarm-info {
  position: relative;
  border-radius: 15px;
  overflow: hidden;
  background: $bg-panel;
  border: 1px solid $border;
  display: flex;
  flex-direction: column;
  height: 100%;

  &::before {
    content: '';
    position: absolute;
    inset: 0;
    pointer-events: none;
    border-radius: inherit;
    background: linear-gradient(135deg, rgba(30,144,255,0.05) 0%, transparent 50%, rgba(0,216,255,0.03) 100%);
  }
}

/* ===== Animated border beam ===== */
.border-beam {
  position: absolute;
  inset: 0;
  pointer-events: none;
  border-radius: inherit;
  overflow: hidden;
  z-index: 2;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 40%;
    height: 3px;
    background: linear-gradient(90deg, transparent, rgba(30,144,255,0.9), rgba(0,216,255,0.7), transparent);
    animation: beamTop 5s linear infinite;
  }

  &::after {
    content: '';
    position: absolute;
    bottom: 0;
    right: -100%;
    width: 40%;
    height: 3px;
    background: linear-gradient(90deg, transparent, rgba(0,216,255,0.7), rgba(30,144,255,0.9), transparent);
    animation: beamBottom 5s linear infinite 2.5s;
  }
}

@keyframes beamTop    { from { left: -40%; }  to { left: 100%; } }
@keyframes beamBottom { from { right: -40%; } to { right: 100%; } }

/* ===== Panel header ===== */
.panel-header {
  display: flex;
  align-items: center;
  gap: 25px;
  padding: 20px 30px 18px;
  border-bottom: 1px solid $border;
  background: linear-gradient(90deg, rgba(0,60,160,0.18), transparent);
  flex-shrink: 0;
}

.panel-header-icon {
  width: 65px;
  height: 65px;
  border-radius: 12px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
  background: linear-gradient(135deg, rgba(30,144,255,0.25), rgba(0,216,255,0.15));
  border: 1px solid rgba(30,144,255,0.35);
  animation: iconGlow 3s ease-in-out infinite;
}

@keyframes iconGlow {
  0%, 100% { box-shadow: 0 0 15px rgba(30,144,255,0.3); }
  50%      { box-shadow: 0 0 35px rgba(30,144,255,0.7), 0 0 12px rgba(0,216,255,0.3); }
}

.panel-title {
  font-size: 30px;
  font-weight: 600;
  letter-spacing: 2px;
  color: $cyan;
}

/* ===== Header right group ===== */
.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-left: auto;
  flex-shrink: 0;
}

.header-count {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.count-label {
  font-size: 25px;
  color: $text-dim;
}

.count-value {
  font-size: 50px;
  font-weight: 700;
  color: #f59e0b;
}

/* ===== Header badges ===== */
.header-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 18px;
  border-radius: 8px;
  flex-shrink: 0;

  &.emergency {
    background: rgba(239,68,68,0.1);
    border: 1px solid rgba(239,68,68,0.35);
    .badge-val { color: #ef4444; }
  }

  &.violation {
    background: rgba(168,85,247,0.1);
    border: 1px solid rgba(168,85,247,0.35);
    .badge-val { color: #a855f7; }
  }
}

.badge-icon { font-size: 26px; }

.badge-val {
  font-size: 40px;
  font-weight: 700;
  line-height: 1;
}

.badge-lbl {
  font-size: 23px;
  color: $text-dim;
}

/* ===== Warning scroll ===== */
.warn-scroll-wrap {
  overflow: hidden;
  flex: 1;
  min-height: 0;
  padding: 15px 20px;
}

.warn-scroll-inner {
  &.scrolling {
    animation: scrollUp 22s linear infinite;
  }

  &:hover {
    animation-play-state: paused;
  }
}

@keyframes scrollUp {
  0%   { transform: translateY(0); }
  100% { transform: translateY(-50%); }
}

/* ===== Warning item ===== */
.warn-item {
  padding: 18px 20px;
  border-radius: 10px;
  margin-bottom: 12px;
  background: rgba(245,158,11,0.05);
  border: 1px solid rgba(245,158,11,0.2);
}

.warn-item-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 10px;
  gap: 15px;
}

.warn-cat {
  font-size: 22px;
  font-weight: 600;
  padding: 2px 12px;
  border-radius: 6px;
  white-space: nowrap;
  flex-shrink: 0;

  &.emergency {
    background: rgba(239,68,68,0.15);
    color: #ef4444;
    border: 1px solid rgba(239,68,68,0.35);
  }

  &.violation {
    background: rgba(168,85,247,0.15);
    color: #a855f7;
    border: 1px solid rgba(168,85,247,0.35);
  }
}

.warn-lab {
  font-size: 28px;
  font-weight: 600;
  color: #fcd34d;
  flex: 1;
}

.warn-time {
  font-size: 25px;
  color: $text-dim;
  white-space: nowrap;
}

.warn-detail {
  font-size: 25px;
  color: $text-dim;
  display: flex;
  align-items: center;
  gap: 10px;
}

.warn-metric-val {
  color: #fb923c;
  font-weight: 600;
}
</style>
