<template>
    <el-popover trigger="click" placement="top" :width="180">
        <div class="select">
            <div class="model-group">
                <div class="group-title">生成模型</div>
                <div 
                    v-for="item in generateModels" 
                    :key="item.value"
                    class="model-item"
                    :class="{ active: model === item.value }"
                    @click="selectModel(item.value)"
                >
                    {{ item.label }}
                </div>
            </div>
            
            <div class="model-group">
                <div class="group-title">编辑模型</div>
                <div 
                    v-for="item in editModels" 
                    :key="item.value"
                    class="model-item"
                    :class="{ active: model === item.value }"
                    @click="selectModel(item.value)"
                >
                    {{ item.label }}
                </div>
            </div>
            
            <div class="model-group">
                <div class="group-title">视觉理解模型</div>
                <div 
                    v-for="item in visionModels" 
                    :key="item.value"
                    class="model-item"
                    :class="{ active: model === item.value }"
                    @click="selectModel(item.value)"
                >
                    {{ item.label }}
                </div>
            </div>
        </div>
        <template #reference>
            <div class="choice-btn">
                <img src="@/assets/dialog/model.svg" alt="" style="width: 16px;">
                <span>{{ getModelLabel(model) }}</span>
            </div>
        </template>
    </el-popover>
</template>

<script setup>
const props = defineProps({
  modelValue: {
    type: String,
    default: 'flux'
  },
  typeValue: {
    type: String,
    default: 'text'
  }
})

const emit = defineEmits(['update:modelValue', 'update:typeValue'])

const model = computed({
  get: () => props.modelValue,
  set: (value) => {
    emit('update:modelValue', value)
    const newType = getModelType(value)
    emit('update:typeValue', newType)
  }
})

const generateModels = [
  { value: 'flux', label: 'flux' },
  { value: 'zImage', label: 'Z-image' },
  { value: 'jimeng', label: 'jimeng' },
  { value: 'QwenImage', label: 'QwenImage' }
]

const editModels = [
  { value: 'BananaPro', label: 'Banana-Pro' },
  { value: 'Qwen-image', label: 'Qwen-image' },
  { value: 'Kontext', label: 'Kontext' },
  { value: 'Jimeng_4.0', label: 'Jimeng.4.0' }
]

const visionModels = [
  { value: 'Qwen3.5plus', label: 'Qwen3.5plus' }
]

const selectModel = (value) => {
  model.value = value
}

const getModelType = (value) => {
  if (generateModels.find(m => m.value === value)) {
    return 'text'
  }
  if (editModels.find(m => m.value === value)) {
    return 'image'
  }
  if (visionModels.find(m => m.value === value)) {
    return 'vision'
  }
  return 'text'
}

const getModelLabel = (value) => {
  const allModels = [...generateModels, ...editModels, ...visionModels]
  const model = allModels.find(item => item.value === value)
  return model ? model.label : value
}
</script>

<style lang="less" scoped>
.choice-btn{
  display: flex;
  height: 40px;
  padding: 0 15px;
  justify-content: center;
  align-items: center;
  gap: 5px;
  border-radius: 10px;
  border: 1px solid rgba(0, 0, 0, 0.10);
  background: #ffffff;
  cursor: pointer;
  position: relative;
}
.choice-btn:hover{
  background: #E5E7EB;
}

.select{
  padding: 10px;
  max-height: 510px;
  overflow-y: auto;
}

.model-group{
  margin-bottom: 15px;
  
  &:last-child{
    margin-bottom: 0;
  }
}

.group-title{
  font-size: 12px;
  font-weight: 600;
  color: #666;
  margin-bottom: 8px;
  padding-left: 5px;
}

.model-item{
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  margin-bottom: 4px;
  transition: all 0.2s ease;
  
  &:last-child{
    margin-bottom: 0;
  }
  
  &:hover{
    background: #f0f0f0;
  }
  
  &.active{
    background: #e6f7ff;
    color: #1890ff;
    font-weight: 500;
  }
}
</style>