<!-- 选择下拉框 -->
<template>
  <a-select
    show-search
    optionFilterProp="label"
    :options="data"
    allow-clear
    :value="value"
    :placeholder="placeholder"
    @update:value="updateValue"
    @blur="onBlur"
  />
</template>

<script lang="ts" setup>
  import { listCategory } from '@/api/goods/category';
  import { ref } from 'vue';
  import { Category } from '@/api/goods/category/model';

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

  withDefaults(
    defineProps<{
      value?: string;
      placeholder?: string;
      dictCode?: string;
    }>(),
    {
      placeholder: '请选择分类'
    }
  );

  const data = ref<Category[]>([]);

  // 字典数据
  listCategory({ parentId: 0 }).then((list) => {
    data.value = list.map((d) => {
      return {
        value: d.title,
        label: d.title,
        text: d.title
      };
    });
  });

  /* 更新选中数据 */
  const updateValue = (value: string) => {
    emit('update:value', value);
  };
  /* 失去焦点 */
  const onBlur = () => {
    emit('blur');
  };
</script>
