<template>
  <div class="service-type-selector">
    <el-radio-group :model-value="modelValue" size="default" :fill="fill" @update:model-value="handleGroupChange">
      <el-radio-button v-for="item in visibleItems" :key="item.id" :label="item.id">{{ item.name }}</el-radio-button>
    </el-radio-group>
    <el-popover v-if="hiddenItems.length > 0" placement="bottom" trigger="click" :width="200"
      popper-class="service-type-popover">
      <template #reference>
        <el-button class="more-btn" size="default">
          <el-icon>
            <ArrowDown />
          </el-icon>
        </el-button>
      </template>
      <div class="popover-list">
        <div v-for="item in allItems" :key="item.id" class="popover-item" :class="{ active: modelValue === item.id }"
          @click="handlePopoverSelect(item)">
          <span class="popover-item-dot" v-if="modelValue === item.id">●</span>
          {{ item.name }}
        </div>
      </div>
    </el-popover>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'

interface ServiceOption {
  id: string | number
  name: string
}

interface Props {
  modelValue: string | number
  options: ServiceOption[]
  allLabel?: string
  allValue?: string | number
  fill?: string
}

const props = withDefaults(defineProps<Props>(), {
  allLabel: '全部类型',
  allValue: '',
  fill: undefined
})

const emit = defineEmits<{
  'update:modelValue': [value: string | number]
  change: [value: string | number]
}>()

const maxVisible = 4

const allItems = computed<ServiceOption[]>(() => {
  return [
    { id: props.allValue, name: props.allLabel },
    ...props.options
  ]
})

const visibleItems = computed<ServiceOption[]>(() => {
  const total = allItems.value
  if (total.length <= maxVisible) return total

  const result: ServiceOption[] = []
  const selected = total.find(i => i.id === props.modelValue)

  // "全部" 始终可见
  result.push(total[0])

  // 当前选中项始终可见
  if (selected && selected.id !== props.allValue && !result.find(i => i.id === selected.id)) {
    result.push(selected)
  }

  // 用列表前部的选项填充剩余位置
  for (const item of total) {
    if (result.length >= maxVisible) break
    if (!result.find(i => i.id === item.id)) {
      result.push(item)
    }
  }

  return result
})

const hiddenItems = computed<ServiceOption[]>(() => {
  return allItems.value.filter(i => !visibleItems.value.find(v => v.id === i.id))
})

const emitChange = (value: string | number) => {
  emit('update:modelValue', value)
  emit('change', value)
}

const handleGroupChange = (value: string | number) => {
  emitChange(value)
}

const handlePopoverSelect = (item: ServiceOption) => {
  emitChange(item.id)
}
</script>

<style scoped>
.service-type-selector {
  display: inline-flex;
  align-items: center;
  gap: 0;
}

.more-btn {
  margin-left: -1px;
  border-radius: 0 4px 4px 0;
  padding: 0 10px;
  height: 32px;
}

.popover-list {
  max-height: 300px;
  overflow-y: auto;
}

.popover-item {
  padding: 8px 12px;
  cursor: pointer;
  font-size: 14px;
  color: #606266;
  transition: background-color 0.2s, color 0.2s;
}

.popover-item:hover {
  background-color: #ecf5ff;
  color: #2d8cf0;
}

.popover-item.active {
  color: #2d8cf0;
  font-weight: 600;
  background-color: #ecf5ff;
}

.popover-item-dot {
  margin-right: 6px;
  font-size: 12px;
}
</style>
