<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-count" style="margin-left:auto">
        <span class="count-label">本月</span>
        <span class="count-value">{{ totalCount }}</span>
        <span class="count-label">次</span>
      </div>
    </div>
    <!-- Scrolling warning list -->
    <div class="warn-scroll-wrap">
      <div class="warn-scroll-inner">
        <div
          v-for="(item, idx) in scrollList"
          :key="idx"
          class="warn-item"
        >
          <div class="warn-item-head">
            <span class="warn-lab">&#x1F6A8; {{ item.lab }}（{{ item.room }}）- {{ item.unit }}</span>
            <span class="warn-time">{{ item.time }}</span>
          </div>
          <div class="warn-detail">
            异常指标：<span class="warn-metric-val">{{ item.metric }} {{ item.val }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

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

const LEVEL_LABELS = { 1: '低风险', 2: '中风险', 3: '较高风险', 4: '高风险' }

export default {
  name: 'AlarmInfo',
  data() {
    return {
      totalCount: 0,
      warningList: [],
      pollTimer: null
    }
  },
  computed: {
    /** Duplicate data for seamless CSS scroll loop */
    scrollList() {
      if(this.warningList[6]){
        return [...this.warningList, ...this.warningList]
      }else if(this.warningList[0]){
        return [...this.warningList]
      }else{
        return []
      }
    }
  },
  mounted() {
    this.fetchData()
    if(this.warningList[6]){
      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) {
          this.totalCount = res.data.monthAlarmCount
          this.warningList = (res.data.eventList || []).map(e => ({
            lab:    e.subName,
            room:   '',
            unit:   '',
            metric: e.eventName,
            val:    LEVEL_LABELS[e.riskPlanLevel] || '',
            time:   e.eventStartTime ? e.eventStartTime.replace('T', ' ') : ''
          }))
        }
      } 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 count ===== */
.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;
}

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

.warn-scroll-inner {
  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-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>
