<template>
  <q-select
    v-model="model"
    label="Selecione empreendimento"
    outlined
    clearable
    :options="businessOptions"
    :loading="loading"
    option-label="name"
    emit-value
    map-options
  />
</template>

<script setup>
import { getBusinessToSelect } from "src/api/business";
import { onMounted, ref } from "vue";

const model = defineModel();
const businesses = ref([]);
const loading = ref(false);
const businessOptions = ref([]);

const fetchBusiness = async () => {
  try {
    loading.value = true;
    businesses.value = await getBusinessToSelect();
    businessOptions.value = businesses.value;
  } catch (error) {
    console.error("Erro ao carregar empreendimentos:", error);
  } finally {
    loading.value = false;
  }
};

onMounted(fetchBusiness);

defineExpose({
  businesses: businesses,
  getBusinessById: (id) => businesses.value.find((b) => b.id === id),
});
</script>
