<!-- 场馆选择下拉框 -->
<template>
  <a-select
    allow-clear
    mode="multiple"
    :value="sid"
    :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 type { ThinkSite } from '@/api/think/thinkSite/model';
  import { listThinkSite } from '@/api/think/thinkSite';

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

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

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

  // 场馆数据
  const data = ref<ThinkSite[]>([]);

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

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

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