<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 VnSelect from 'src/components/common/VnSelect.vue';
import FetchData from 'components/FetchData.vue';

import { useState } from 'src/composables/useState';
import VnSelectWorker from 'src/components/common/VnSelectWorker.vue';

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

const route = useRoute();
const { t } = useI18n();
const state = useState();
const user = state.getUser();
const stateFetchDataRef = ref(null);
const statesOptions = ref([]);

const onStateFkChange = (formData) => (formData.userFk = user.value.id);
</script>
<template>
    <FetchData
        ref="stateFetchDataRef"
        url="States"
        auto-load
        @on-fetch="(data) => (statesOptions = data)"
    />
    <FormModelPopup
        :title="t('Create tracking')"
        url-create="Tickets/state"
        model="CreateTicketTracking"
        :form-initial-data="{ ticketFk: route.params.id }"
        @on-data-saved="() => emit('onRequestCreated')"
    >
        <template #form-inputs="{ data }">
            <VnRow>
                <VnSelect
                    v-model="data.stateFk"
                    :label="t('ticketList.state')"
                    :options="statesOptions"
                    @update:model-value="onStateFkChange(data)"
                    hide-selected
                    option-label="name"
                    option-value="id"
                />
                <VnSelectWorker v-model="data.userFk" :fields="['id', 'name']" />
            </VnRow>
        </template>
    </FormModelPopup>
</template>

<i18n>
    es:
        Create tracking: Crear estado
</i18n>
