<template>
  <div :class="['layer-toggle-button', `layer-toggle-button-${deviceLayer.visible}`]" @click="handleClick">
    <div class="layer-info">
      <!-- 动态内容区域 -->
      <span class="layer-icon" :style="{ color: deviceLayer.color }">
        <!-- 如果 type 是 'point'，显示动态加载的 SVG -->
        <span v-if="deviceLayer.iconType === 'point'">
          <img
            :src="getDynamicIcon(deviceLayer.icon)"
            :title="`${deviceLayer.name}`"
            class="layer-icon"
            @error="handleSvgError"
          />
        </span>
        <!-- 如果 type 是 'line'，显示一个自定义的线条图标 -->
        <span v-else-if="deviceLayer.iconType === 'line'">
          <span :style="{ color: deviceLayer.color, fontWeight: 700 }">一</span>
        </span>
        <!-- 未来可以在这里添加其他类型的判断，例如 v-else-if="layer.type === 'polygon'" -->
        <span v-else>
          <img
            :src="getDynamicIcon(deviceLayer.icon)"
            :title="`${deviceLayer.name}`"
            class="layer-icon"
            @error="handleSvgError"
          />
        </span>
      </span>
      <div>
        <strong>{{ deviceLayer.name }}</strong>
        <span class="layer-desc">{{ deviceLayer.desc }}</span>
      </div>
    </div>
    <div class="layer-indicator" :style="{ background: deviceLayer.color }"></div>
  </div>
</template>

<script>
export default {
  name: 'LayerItemVertical',
  props: {
    deviceLayer: {
      type: Object,
      required: true
    },
    getDynamicIcon: {
      type: Function,
      required: true
    },
    handleSvgError: {
      type: Function,
      required: true
    }
  },
  data () {
    return {
      svgContent: '' // 用于存储加载到的 SVG 字符串
    }
  },
  methods: {
    handleClick () {
      // 向上传递点击事件
      this.$emit('click', this.deviceLayer)
    }
  }
}
</script>

<style scoped>
/* 将原来在 LayerPanel 中的按钮样式移到这里 */
.layer-toggle-button {
  display: flex !important;
  align-items: center;
  justify-content: space-between;
  height: auto !important;
  padding: 8px 12px !important;
  border: 1px solid #e8e8e8 !important;
  border-radius: 16px !important;
  color: inherit !important;
  text-align: left;
  width: 100%;
  transition: all 0.2s ease-in-out;
}
.layer-toggle-button-true {
  background: #e0effb !important;
  border-color: #1990fe !important;
  box-shadow: 0 2px 4px rgba(25, 144, 255, 0.2) !important;
}

.layer-toggle-button:hover,
.layer-toggle-button:focus {
  border-color: #1990fe !important;
  box-shadow: 0 2px 4px rgba(25, 144, 255, 0.2) !important;
  color: inherit !important;
  transform: translateX(-2px);
}

.layer-info {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
}

.layer-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  flex-shrink: 0;
}

.layer-icon svg {
  width: 20px;
  height: 20px;
}

.layer-info > div {
  display: flex;
  flex-direction: column;
}

.layer-info strong {
  font-size: 14px;
  color: #333;
}

.layer-desc {
  font-size: 12px;
  color: #999;
}

.layer-indicator {
  width: 30px;
  height: 5px;
  border-radius: 8px;
  flex-shrink: 0;
}
</style>
