<script setup lang="ts">
import {onMounted, reactive} from "vue";

const {api, fieldName, apiPo, init} = defineProps({
    api: {
        type: Function,
        default: async () => {
        },
        required: true,
    },
    apiPo: {
        type: Object,
        default: {}
    },
    fieldName: {
        type: Object,
        default: {value: 'id', label: 'name'},
    },
    init: {
        type: Boolean,
        default: false,
    },
});

const list = reactive([]);

const popupChange = async (visible) => {
    if (visible) {
        api && api(apiPo).then(({data}) => {
            list.length = 0;
            list.push(...data);
        });
    }
}

onMounted(() => {
    if (init) api && api(apiPo).then(({data}) => {
        list.length = 0;
        list.push(...data.map(v => v.name));
    });
});
</script>

<template>
    <a-auto-complete
        v-bind="$attrs"
        :data="list"
        :field-names="fieldName"
        placeholder="请输入或选择">
    </a-auto-complete>
</template>

<style scoped lang="scss">

</style>
