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

import FetchData from 'components/FetchData.vue';
import VnSelect from 'src/components/common/VnSelect.vue';
import FormModel from 'components/FormModel.vue';
import VnRow from 'components/ui/VnRow.vue';
import VnInput from 'src/components/common/VnInput.vue';
import VnSubToolbar from 'src/components/ui/VnSubToolbar.vue';
import VnInputDate from 'components/common/VnInputDate.vue';

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

const agenciesOptions = ref([]);
const warehousesOptions = ref([]);
const viewAction = ref();

const newTravelForm = ref({});
onBeforeMount(() => {
    viewAction.value = route.query.travelData ? 'clone' : 'create';

    if (route.query.travelData) {
        const travelData = JSON.parse(route.query.travelData);

        newTravelForm.value = { ...newTravelForm.value, ...travelData };
        delete newTravelForm.value.id;
    }
});

const redirectToTravelBasicData = (_, { id }) => {
    router.push({ name: 'TravelBasicData', params: { id } });
};
</script>

<template>
    <FetchData
        url="AgencyModes"
        :filter="{ fields: ['id', 'name'], order: ['name ASC'] }"
        @on-fetch="(data) => (agenciesOptions = data)"
        auto-load
    />
    <FetchData
        url="Warehouses"
        @on-fetch="(data) => (warehousesOptions = data)"
        auto-load
    />
    <QPage>
        <VnSubToolbar />
        <FormModel
            url-create="Travels"
            model="travelCreate"
            :form-initial-data="newTravelForm"
            :observe-form-changes="viewAction === 'create'"
            @on-data-saved="redirectToTravelBasicData"
        >
            <template #form="{ data }">
                <VnRow>
                    <VnInput v-model="data.ref" :label="t('globals.reference')" />
                    <VnSelect
                        :label="t('globals.agency')"
                        v-model="data.agencyModeFk"
                        :options="agenciesOptions"
                        option-value="agencyFk"
                        option-label="name"
                        hide-selected
                    />
                </VnRow>
                <VnRow>
                    <VnInputDate v-model="data.shipped" :label="t('globals.shipped')" />
                    <VnInputDate :label="t('globals.landed')" v-model="data.landed" />
                </VnRow>
                <VnRow>
                    <VnSelect
                        :label="t('globals.warehouseOut')"
                        v-model="data.warehouseOutFk"
                        :options="warehousesOptions"
                        option-value="id"
                        option-label="name"
                        hide-selected
                        :where="{
                            isOrigin: true,
                        }"
                    />
                    <VnSelect
                        :label="t('globals.warehouseIn')"
                        v-model="data.warehouseInFk"
                        :options="warehousesOptions"
                        option-value="id"
                        option-label="name"
                        hide-selected
                        :where="{
                            isDestiny: true,
                        }"
                    />
                </VnRow>
            </template>
        </FormModel>
    </QPage>
</template>
