<template>
  <div class="panel lab-environment">
    <!-- Animated border beam -->
    <div class="border-beam"></div>
    <!-- Corner ornaments -->
    <svg class="pc tl" viewBox="0 0 14 14"><path d="M0 14 V0 H14" fill="none" stroke="rgba(30,144,255,0.85)" stroke-width="1.5"/></svg>
    <svg class="pc tr" viewBox="0 0 14 14"><path d="M0 14 V0 H14" fill="none" stroke="rgba(30,144,255,0.85)" stroke-width="1.5"/></svg>
    <svg class="pc bl" viewBox="0 0 14 14"><path d="M0 14 V0 H14" fill="none" stroke="rgba(30,144,255,0.85)" stroke-width="1.5"/></svg>
    <svg class="pc br" viewBox="0 0 14 14"><path d="M0 14 V0 H14" fill="none" stroke="rgba(30,144,255,0.85)" stroke-width="1.5"/></svg>
    <!-- Panel header -->
    <div class="panel-header">
      <div class="panel-header-icon">&#x1F321;&#xFE0F;</div>
      <span class="panel-title">实验环境安全智能感知</span>
      <div class="status-dot red" style="margin-left:auto"></div>
    </div>
    <!-- Scrolling sensor list -->
    <div class="sensor-scroll-wrap">
      <div class="sensor-scroll-inner" :class="{ scrolling: shouldScroll }">
        <div
          v-for="(item, idx) in scrollList"
          :key="idx"
          class="sensor-item"
          :class="{ alert: item.hasException }"
        >
          <div class="sensor-item-head">
            <span class="sensor-name">{{ item.subName }}（{{ item.roomNum }}）</span>
            <span class="sensor-unit">{{ item.unit }}</span>
            <span v-if="item.hasException" class="sensor-status alarm-status">&#x1F6A8; 告警</span>
            <span v-else class="sensor-status normal-status">&#x25CF; 正常</span>
          </div>
          <div class="sensor-metrics">
            <div class="sensor-metric" :class="minItem.hasException?'redClass':''"
            v-for="(minItem, mIdx) in item.sensorList" :key="mIdx">
              <span class="span-svg">
                <svgIcon class="svgIcon" :icon-class="fileBrowseEnvironment+minItem.icon"/>
              </span>
              <span class="span-text">{{ minItem.attributeName }} {{ minItem.deviceValue }}{{ minItem.unit }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getEnvMonitor } from '@/api/screen'
import svgIcon from '@/components/svgIcon.vue'

export default {
  name: 'LabEnvironment',
  components: {
    svgIcon,
  },
  data() {
    return {
      sensorList: [],
      pollTimer: null,
      fileBrowseEnvironment:localStorage.getItem('fileBrowseEnvironment'),
    }
  },
  computed: {
    shouldScroll() {
      return this.sensorList.length > 12
    },
    scrollList() {
      return this.shouldScroll
        ? [...this.sensorList, ...this.sensorList]
        : [...this.sensorList]
    }
  },
  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 getEnvMonitor({ page: 1, pageSize: 20 })
        if (res.code === 200) {
          const records = (res.data.records || [])
          this.sensorList = records
        }
      } catch (e) {
        // 错误已由拦截器处理
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.lab-environment {
  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%);
  }
}

/* ===== Corner ornaments ===== */
.pc {
  position: absolute;
  width: 35px;
  height: 35px;
  z-index: 3;
  pointer-events: none;

  &.tl { top: 0; left: 0; }
  &.tr { top: 0; right: 0; transform: scaleX(-1); }
  &.bl { bottom: 0; left: 0; transform: scaleY(-1); }
  &.br { bottom: 0; right: 0; transform: scale(-1); }

  svg { width: 100%; height: 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;
}

/* ===== Status dot ===== */
.status-dot {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  display: inline-block;

  &.red {
    background: $red;
    box-shadow: 0 0 15px $red;
    animation: blinkRed 0.8s ease-in-out infinite;
  }

  &.green {
    background: $green;
    box-shadow: 0 0 15px $green;
    animation: dotPulse 2s ease-in-out infinite;
  }
}

@keyframes dotPulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50%      { transform: scale(1.3); opacity: 0.7; }
}

@keyframes blinkRed {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.35; }
}

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

.sensor-scroll-inner {
  &.scrolling {
    animation: scrollUp 30s linear infinite;
  }

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

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

/* ===== Sensor item ===== */
.sensor-item {
  padding: 18px 20px;
  border-radius: 10px;
  margin-bottom: 12px;
  background: $bg-card;
  border: 1px solid $border;

  &.alert {
    border-color: rgba(255,59,59,0.6);
    background: rgba(255,30,30,0.07);
    animation: alertGlow 1.5s ease-in-out infinite;
  }
}

@keyframes alertGlow {
  0%, 100% { box-shadow: 0 0 12px rgba(255,59,59,0.25); }
  50%      { box-shadow: 0 0 40px rgba(255,59,59,0.6); }
}

.sensor-item-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.sensor-name {
  font-size: 28px;
  font-weight: 600;
  color: $white;
}

.sensor-unit {
  font-size: 25px;
  color: $text-dim;
}

.sensor-status {
  font-size: 25px;
}

.alarm-status {
  color: $red;
  animation: blinkRed 0.8s ease-in-out infinite;
}

.normal-status {
  color: $green;
}

/* ===== Sensor metrics ===== */
.sensor-metrics {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}
.redClass{
  background: rgba(255,0,0,0.2)!important ;
  border: 1px solid rgba(255,0,0,0.7)!important;
  .svgIcon{
    color:red;
  }
  .span-text{
    color:red;
  }
}
.sensor-metric {
  padding: 5px 15px;
  border-radius: 5px;
  font-size: 25px;
  background: rgba(4,16,45,0.7);
  border: 1px solid rgba(30,144,255,0.2);
  display: flex;
  align-items: center;
  gap: 8px;
  color: $text;

  &.alarm {
    background: rgba(255,0,30,0.12);
    border-color: $red;
    color: $red;
    animation: blinkRed 0.8s ease-in-out infinite;
  }
}

.span-svg {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  flex-shrink: 0;

  img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    color:#fff;
  }
}
</style>
