<script setup>
import { ref } from 'vue';
import { useRoute } from 'vue-router';
import { useI18n } from 'vue-i18n';

import FormModelPopup from 'components/FormModelPopup.vue';
import VnRow from 'components/ui/VnRow.vue';
import VnInput from 'src/components/common/VnInput.vue';
import VnSelect from 'src/components/common/VnSelect.vue';
import FetchData from 'components/FetchData.vue';

const emit = defineEmits(['onRequestCreated']);

const route = useRoute();
const { t } = useI18n();

const attendersOptions = ref([]);
</script>
<template>
    <FetchData
        url="TicketRequests/getItemTypeWorker"
        :filter="{ fields: ['id', 'nickname'], order: 'nickname ASC' }"
        auto-load
        @on-fetch="(data) => (attendersOptions = data)"
    />
    <FormModelPopup
        :title="t('Create request')"
        url-create="TicketRequests"
        model="CreateTicketRequest"
        :form-initial-data="{ ticketFk: route.params.id }"
        @on-data-saved="() => emit('onRequestCreated')"
    >
        <template #form-inputs="{ data }">
            <VnRow>
                <VnInput
                    v-model="data.description"
                    :label="t('purchaseRequest.description')"
                />
                <VnSelect
                    :label="t('purchaseRequest.atender')"
                    v-model="data.attenderFk"
                    :options="attendersOptions"
                    hide-selected
                    option-label="nickname"
                    option-value="id"
                />
            </VnRow>
            <VnRow>
                <VnInput
                    v-model="data.quantity"
                    :label="t('purchaseRequest.quantity')"
                    type="number"
                    min="1"
                />
                <VnInput
                    v-model="data.price"
                    :label="t('purchaseRequest.price')"
                    type="number"
                    min="0"
                />
            </VnRow>
        </template>
    </FormModelPopup>
</template>

<i18n>
    es:
        Create request: Crear petición de compra
</i18n>
