<template>
  <div :class="['layer-toggle-button', `layer-toggle-button-${deviceLayer.visible}`]" @click="handleClick">
    <div class="layer-content">
      <!-- 图标区域（顶部） -->
      <div class="icon-section">
        <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-img"
              @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-img"
              @error="handleSvgError"
            />
          </span>
        </span>
      </div>

      <!-- 文本信息区域（中间） -->
      <div class="text-section">
        <strong class="layer-name">{{ deviceLayer.name }}</strong>
      </div>

      <!-- 指示器区域（底部） -->
      <div class="indicator-section">
        <div class="layer-indicator" :style="{ background: deviceLayer.color }"></div>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: 'LayerItemHorizontal',
  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>
/* 横向卡片样式 */
.layer-toggle-button {
  display: flex !important;
  align-items: center;
  justify-content: center;
  height: auto !important;
  padding: 12px 8px !important;
  border: 1px solid #e8e8e8 !important;
  border-radius: 12px !important;
  color: inherit !important;
  text-align: center;
  width: 100%;
  transition: all 0.2s ease-in-out;
  cursor: pointer;
  min-height: 80px;
}

.layer-toggle-button-true {
  background: #e0effb !important;
  border-color: #1990fe !important;
  box-shadow: 0 2px 8px rgba(25, 144, 255, 0.2) !important;
}

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

/* 内容区域 - 垂直排列 */
.layer-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  height: 100%;
  gap: 0;
}

/* 图标区域 */
.icon-section {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
}

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

.layer-icon-img {
  width: 24px;
  height: 24px;
  object-fit: contain;
}

/* 文本区域 */
.text-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  width: 100%;
  overflow: hidden;
}

.layer-name {
  font-size: 13px;
  color: #333;
  font-weight: 600;
  margin-bottom: 4px;
  text-align: center;
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  width: 100%;
}

.layer-desc {
  font-size: 11px;
  color: #999;
  text-align: center;
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  width: 100%;
}

/* 指示器区域 */
.indicator-section {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 100%;
}

.layer-indicator {
  width: 40px;
  height: 4px;
  border-radius: 2px;
  transition: all 0.3s ease;
}

.layer-toggle-button:hover .layer-indicator {
  width: 50px;
  opacity: 0.9;
}
</style>
