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

<script lang="ts" setup>
  import { ref } from 'vue';
  import { message } from 'ant-design-vue';
  import { pageCustomer } from '@/api/oa/customer';
  import type { SelectProps } from 'ant-design-vue';

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

  withDefaults(
    defineProps<{
      value?: number;
      placeholder?: string;
    }>(),
    {
      placeholder: '请选择所属客户'
    }
  );

  // 字典数据
  const data = ref<SelectProps['options']>([]);

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

  /* 获取字典数据 */
  pageCustomer({ customerType: '' })
    .then((result) => {
      data.value = result?.list.map((d) => {
        return {
          value: d.customerId,
          label: d.customerName
        };
      });
    })
    .catch((e) => {
      message.error(e.message);
    });

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

  // 联动搜索
  const onSearch = (customerName) => {
    pageCustomer({ customerName }).then((res) => {
    });
  };
</script>
