<template>
  <!-- TagGroup.vue -->
  <div class="tag-group">
    <div
      v-for="(tag, index) in tags"
      :key="index"
      class="tag-item"
      :class="{
        'active': selectedIndex === index,
        'alarm-red': tag.ifAlarm === 'alarm'  // 新增的条件类
      }"
      @click="selectTag(index)"
    >
      <span class="property">{{ tag.modelName }}</span>
      <span class="value">{{ tag.value }}</span>
    </div>
  </div>
</template>

<script setup>
import {defineEmits, ref} from 'vue'

const props = defineProps({
  tags: {
    type: Array,
    required: true,
    validator: (arr) => arr.every(item => item.label && item.value)
  },
  tagWidth: {
    type: String,
    default: '220px'
  }
})
const emit = defineEmits(['select'])
const selectedIndex = ref(0)

const selectTag = (index) => {
  selectedIndex.value = selectedIndex.value === index ? -1 : index
  emit('select', props.tags[selectedIndex.value])
}
</script>

<style scoped>
.tag-group {
  display: flex;
  flex-wrap: wrap;
  row-gap: 10px;
  column-gap: 16px;
}

.tag-item {
  /* 基础样式 */
  width: v-bind('props.tagWidth');
  height: 48px;
  background: #ffffff;
  border: 1px solid #dcdfe6;
  border-radius: 6px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);

  /* 内容布局 */
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 16px;
}

/* 文字样式 */
.property {
  color: #606266;
  font-size: 14px;
  font-weight: 500;
  flex-shrink: 0;
}

.value {
  color: #909399;
  font-size: 14px;
  max-width: 60%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 悬停效果 */
.tag-item:hover {
  transform: scale(1.03);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
  z-index: 1;
}

/* 选中状态 */
.tag-item.active {
  background: #276cff;
  border-color: #276cff;
}

.tag-item.active .property,
.tag-item.active .value {
  color: #fff !important;
}

/* 选中状态阴影 */
.tag-item.active {
  box-shadow: 0 4px 16px rgba(39, 108, 255, 0.2);
}

/* 禁用状态 */
.tag-item.disabled {
  opacity: 0.6;
  cursor: not-allowed;
  filter: grayscale(0.8);
}
.tag-item.alarm-red {
  background-color: #f3afaf;
}
</style>
