<!-- 角色选择下拉框 -->
<template>
  <a-select
    mode="multiple"
    allow-clear
    :value="ids"
    :placeholder="placeholder"
    @update:value="updateValue"
    @blur="onBlur"
  >
    <a-select-option v-for="item in data" :key="item.id" :value="item.id">
      {{ item.name }}
    </a-select-option>
  </a-select>
</template>

<script lang="ts" setup>
  import { ref, computed } from 'vue';
  import { message } from 'ant-design-vue/es';
  import { Item } from '@/api/booking/item/model';
  import { listItem } from '@/api/booking/item';

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

  const props = withDefaults(
    defineProps<{
      // 选中的商户
      value?: Item[];
      //
      placeholder?: string;
    }>(),
    {
      placeholder: '请选择场馆'
    }
  );

  // 选中的id
  const ids = computed(() => props.value?.map((d) => d.id as number));

  // 数据列表
  const data = ref<Item[]>([]);

  /* 更新选中数据 */
  const updateValue = (value: number[]) => {
    emit(
      'update:value',
      value.map((v) => ({ id: v }))
    );
  };

  /* 获取数据 */
  listItem({})
    .then((list) => {
      data.value = list;
    })
    .catch((e) => {
      message.error(e.message);
    });

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