<script setup lang="ts">
import { ref, watch } from "vue";
import { useVModel } from "@vueuse/core";
import { httpCompanyAll } from "/@/api/components/remoteSelect";
import RemoteSelect from "../remote-select";

const emit = defineEmits(["update:modelValue", "change"]);

const props = defineProps<{
  placeholder?: string;
  modelValue?: string;
  project_name?: string;
  disabled?: boolean;
}>();

const remoteSelectRef = ref<any>(null);

const value = useVModel(props, "modelValue");

function handleChange(item) {
  emit("change", item);
}

watch(
  () => props.project_name,
  project_name => {
    if (!project_name && !remoteSelectRef.value) return;
    remoteSelectRef.value.initalData({ project_name });
  },
  {
    deep: true
  }
);
</script>

<template>
  <RemoteSelect
    v-model="value"
    :api="httpCompanyAll"
    :placeholder="placeholder"
    :disabled="disabled"
    ref="remoteSelectRef"
    response-label-prop="title"
    response-val-prop="id"
    request-prop="title"
    :isRoot="true"
    @item-change="handleChange"
  />
</template>
