<template>
  <div class="legend-container">
    <!-- 横向排列版本 -->
    <div v-if="layout==='horizontal'" class="horizontal-legend">
      <div class="title">{{title}}</div>
      <div v-for="(item, index) in legendItems" :key="index" class="legend-item">
        <span class="color-block" :style="{ backgroundColor: item.color }"></span>
        <span class="label">{{ item.label }}</span>
      </div>
    </div>

    <!-- 纵向排列版本 -->
    <div v-if="layout==='vertical'" class="vertical-legend">
      <div class="title">{{title}}</div>
      <div v-for="(item, index) in legendItems" :key="index" class="list-item">
        <span class="color-block" :style="{ backgroundColor: item.color }"></span>
        <span class="label">{{ item.label }}</span>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "ColorLegend",
  props: {
    legendItems: {
      type: Array,
      required: true,
      validator: (value) => {
        return value.every(item => item.color && item.label);
      },
    },
    layout: {
      type: String,
      default: 'horizontal', // 可选值：horizontal（横向）或 vertical（纵向）
      validator: (value) => {
        return ['horizontal', 'vertical'].includes(value);
      },
    },
    title: {
      type: String,
      required: true
    },
  },
}
</script>

<style scoped>
.title {
  font-size: 14px;
  font-weight: bolder;
}
/* 横向排列样式 */
.horizontal-legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 18px;
  padding: 8px;
  background: #fafafa;
  border-radius: 4px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.color-block {
  display: inline-block;
  width: 16px;
  height: 16px;
  border-radius: 3px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.label {
  font-size: 14px;
  color: rgba(0, 0, 0, 0.85);
}

/* 纵向排列样式 */
.vertical-legend {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  gap: 5px;
  margin-top: 20px;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
}

.list-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px;
}

/* 响应式适配 */
@media (max-width: 60vw) {
  .horizontal-legend {
    gap: 12px;
  }

  .color-block {
    width: 12px;
    height: 12px;
  }

  .label {
    font-size: 12px;
  }

}
</style>