<template>
    <field-list v-model="selected" type="single" :filter="filter" :appId="sourceApp ? sourceApp : appId"
        :fields="sourceFields" @update:modelValue="handleSelect" />
</template>
<script lang="ts">
import { computed, inject, ref } from 'vue';
import FieldList from '../FieldList.vue';
export default {
    name: 'FieldAdd',
    components: {
        FieldList,
    },
    props: {
        appId: Number,
        filter: String
    },
    setup(props, { emit }) {
        const sourceFields = inject<Array<unknown>>('sourceFields')
        const sourceApp = inject<number>('sourceApp')
        const appId = computed(() => {
            if (sourceFields || sourceApp) {
                return sourceApp.value
            } else {
                return props.appId
            }
        });
        return {
            sourceFields,
            sourceApp,
            selected: ref([]),
            handleSelect: (newSelection: any[]) => {
                
                if (newSelection.length > 0) {
                    const v = newSelection[0]
                    emit('select', { _t: `field(${appId.value},${v.name})`, ...v }); // 假设您只需要选择的第一个字段的名称
                }
            }
        }
    },
}
</script>