<!-- 选择下拉框 -->
<template>
  <a-select
    :allow-clear="allowClear"
    :show-search="showSearch"
    optionFilterProp="label"
    :options="data"
    :value="value"
    :placeholder="placeholder"
    @update:value="updateValue"
    :style="`width: ${width}px`"
    @change="change"
    @blur="onBlur"
  />
</template>

<script lang="ts" setup>
  import { getDictionaryOptions } from '@/utils/common';

  const emit = defineEmits<{
    (e: 'update:value', value: string): void;
    (e: 'index', index: number): void;
    (e: 'blur'): void;
    (e: 'done', item: any): void;
  }>();

  const props = withDefaults(
    defineProps<{
      value?: string;
      placeholder?: string;
      dictCode?: string;
      showSearch?: string;
      allowClear?: boolean;
      width?: number;
      index?: number;
    }>(),
    {
      placeholder: '请选择服务器厂商'
    }
  );

  // 字典数据
  const data = getDictionaryOptions(props.dictCode);

  /* 更新选中数据 */
  const updateValue = (value: string) => {
    emit('update:value', value);
    emit('index', Number(props.index));
  };

  /* 失去焦点 */
  const onBlur = () => {
    emit('blur');
  };

  const change = (e, item) => {
    emit('done', item);
  };
</script>
