<script setup>
    import { ref } from 'vue';
    import VueMultiselect from 'vue-multiselect'
    import { getSettingsQuery } from '../../services/public';

    const options = ref([]);
    const isLoading = ref(false);

    // defineProps(['selectedTruckType']);
    defineProps({
        selectedTruckType: {
            type: Array
        },
        multiple: {
            type: Boolean,
            default: false
        }
    });
    defineEmits(['update:selectedTruckType', 'clear-option'])

    const getTruckTypesQuery = async(query) => {
        isLoading.value = true;
        let filter = {
            meta_key:'truck_type',
            query: query
        }
        const resp = await getSettingsQuery(filter);
        isLoading.value = false;
        options.value = resp;
    }
</script>

<template>
    <VueMultiselect
        :value="selectedTruckType"
        @input="$event => $emit('update:selectedTruckType', $event.target.value)"
        :options="options"
        :multiple="multiple"
        :loading="isLoading"
        :searchable="true"
        :close-on-select="true"
        @search-change="getTruckTypesQuery"
        @remove="$emit('clear-option')"
        placeholder="Busca por tipo de transporte"
        label="meta_value"
        track-by="meta_value"
        selectLabel="Presione para seleccionar"
        selectedLabel="Selecionado"
        deselectLabel="Presione para remover selecion"
    >
        <template #noResult>
            Oops! No se encontro coincidencias.
        </template>
        <template #noOptions>
            Lista vacia.
        </template>
    </VueMultiselect>
</template>

<style src="vue-multiselect/dist/vue-multiselect.css"></style>

<style scoped>

</style>