<template>
  <div class="custom-select-wrapper">
    <div class="custom-select" :class="{ 'open': isOpen }">
      <div class="select-trigger" @click="toggleOpen">
        <span class="select-value">{{ selectedLabel || placeholder }}</span>
        <svg class="select-arrow" viewBox="0 0 24 24">
          <path d="M7 10l5 5 5-5z" fill="currentColor"/>
        </svg>
      </div>
      
      <div v-if="isOpen" class="select-dropdown">
        <div
          v-for="option in options"
          :key="option.value"
          class="select-option"
          :class="{ 'selected': String(modelValue) === String(option.value) }"
          @click="selectOption(option)"
        >
          {{ option.label }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CustomSelect',
  props: {
    modelValue: {
      type: [String, Number],
      default: ''
    },
    options: {
      type: Array,
      required: true,
      validator: (arr) => arr.every(item => 'value' in item && 'label' in item)
    },
    placeholder: {
      type: String,
      default: '请选择'
    }
  },
  emits: ['update:modelValue'],
  data() {
    return {
      isOpen: false
    }
  },
  computed: {
    selectedLabel() {
      const option = this.options.find(opt => String(opt.value) === String(this.modelValue))
      return option ? option.label : ''
    }
  },
  methods: {
    toggleOpen() {
      this.isOpen = !this.isOpen
    },
    selectOption(option) {
      this.$emit('update:modelValue', option.value)
      this.isOpen = false
    },
    closeDropdown() {
      this.isOpen = false
    },
    handleClickOutside(event) {
      if (this.$el && !this.$el.contains(event.target)) {
        this.isOpen = false
      }
    }
  },
  mounted() {
    document.addEventListener('click', this.handleClickOutside)
  },
  beforeUnmount() {
    document.removeEventListener('click', this.handleClickOutside)
  }
}
</script>

<style scoped>
.custom-select-wrapper {
  width: 100%;
}

.custom-select {
  position: relative;
  width: 100%;
}

.select-trigger {
  width: 100%;
  padding: 10px 14px;
  border: 2px solid #e8eef5;
  border-radius: 10px;
  background: white;
  color: #2d3748;
  font-size: 13px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: border-color 0.2s ease;
  user-select: none;
}

.select-trigger:hover {
  border-color: #d0d8e8;
}

.custom-select.open .select-trigger {
  border-color: #4a90e2;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
}

.select-value {
  flex: 1;
  text-align: left;
}

.select-arrow {
  width: 14px;
  height: 14px;
  color: #999;
  transition: transform 0.2s ease;
  flex-shrink: 0;
  margin-left: 8px;
}

.custom-select.open .select-arrow {
  transform: rotate(180deg);
  color: #4a90e2;
}

.select-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: white;
  border: 2px solid #4a90e2;
  border-top: none;
  border-bottom-left-radius: 10px;
  border-bottom-right-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  z-index: 1000;
  max-height: 180px;
  overflow-y: auto;
}

.select-option {
  padding: 10px 14px;
  color: #2d3748;
  cursor: pointer;
  transition: background-color 0.15s ease;
  font-size: 13px;
  line-height: 1.4;
}

.select-option:hover {
  background: #f5f7fa;
}

.select-option.selected {
  background: #f0f4f8;
  color: #4a90e2;
  font-weight: 500;
}

/* 滚动条美化 */
.select-dropdown::-webkit-scrollbar {
  width: 5px;
}

.select-dropdown::-webkit-scrollbar-track {
  background: transparent;
}

.select-dropdown::-webkit-scrollbar-thumb {
  background: #d0d8e8;
  border-radius: 2px;
}

.select-dropdown::-webkit-scrollbar-thumb:hover {
  background: #b0b8d8;
}
</style>
