<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useVModel } from "@vueuse/core";
import RemoteSelect from "/@/components/RemoteSelect";
import { httpArea } from "/@/api/netOrderEnter/netOrderEntry";

const props = defineProps<{
  modelValue?: string;
  level?: number
}>();

const emits = defineEmits(['change']);

const value = useVModel(props, "modelValue");
const RemoteSelectRef = ref<InstanceType<typeof RemoteSelect> | null>(null);

onMounted(() => {
  value.value && (RemoteSelectRef.value as any).initalData(value.value);
});

defineExpose({
  initialData: data => (RemoteSelectRef.value as any).initalData(data),
  getRecurrentValue:  data => (RemoteSelectRef.value as any).getRecurrentValue(data)
});
</script>

<template>
  <RemoteSelect
    v-model="value"
    is-root
    ref="RemoteSelectRef"
    style="width: 100%"
    :api="httpArea"
    request-prop="name"
    response-label-prop="search_name"
    response-val-prop="code"
    @item-change="val => emits('change', val)"
    @clear="emits('change')"
    :pro-data="() => ({ level: level || 2 })"
  />
</template>
