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

import FetchData from 'components/FetchData.vue';
import VnSelectFilter from 'src/components/common/VnSelectFilter.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 newTravelForm = reactive({
    ref: null,
    agencyModeFk: null,
    shipped: null,
    landed: null,
    warehouseOutFk: null,
    warehouseInFk: null,
});

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

onBeforeMount(() => {
    // Esto nos permite decirle a FormModel si queremos observar los cambios o no
    // Ya que si queremos clonar queremos que nos permita guardar inmediatamente sin realizar cambios en el form
    viewAction.value = route.query.travelData ? 'clone' : 'create';

    if (route.query.travelData) {
        const travelData = JSON.parse(route.query.travelData);
        for (let key in newTravelForm) {
            newTravelForm[key] = travelData[key];
        }
    }
});

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

<template>
    <FetchData
        url="AgencyModes"
        @on-fetch="(data) => (agenciesOptions = data)"
        auto-load
    />
    <FetchData
        url="Warehouses"
        @on-fetch="(data) => (warehousesOptions = data)"
        auto-load
    />
    <QPage>
        <VnSubToolbar />
        <FormModel
            url-update="Travels"
            model="travel"
            :form-initial-data="newTravelForm"
            :observe-form-changes="viewAction === 'create'"
            @on-data-saved="redirectToTravelBasicData"
        >
            <template #form="{ data }">
                <VnRow class="row q-gutter-md q-mb-md">
                    <div class="col">
                        <VnInput v-model="data.ref" :label="t('globals.reference')" />
                    </div>
                    <div class="col">
                        <VnSelectFilter
                            :label="t('globals.agency')"
                            v-model="data.agencyModeFk"
                            :options="agenciesOptions"
                            option-value="agencyFk"
                            option-label="name"
                            hide-selected
                        />
                    </div>
                </VnRow>
                <VnRow class="row q-gutter-md q-mb-md">
                    <div class="col">
                        <VnInputDate
                            v-model="data.shipped"
                            :label="t('globals.shipped')"
                        />
                    </div>
                    <div class="col">
                        <VnInputDate :label="t('globals.landed')" v-model="data.landed" />
                    </div>
                </VnRow>
                <VnRow class="row q-gutter-md q-mb-md">
                    <div class="col">
                        <VnSelectFilter
                            :label="t('globals.wareHouseOut')"
                            v-model="data.warehouseOutFk"
                            :options="warehousesOptions"
                            option-value="id"
                            option-label="name"
                            hide-selected
                        />
                    </div>
                    <div class="col">
                        <VnSelectFilter
                            :label="t('globals.wareHouseIn')"
                            v-model="data.warehouseInFk"
                            :options="warehousesOptions"
                            option-value="id"
                            option-label="name"
                            hide-selected
                        />
                    </div>
                </VnRow>
            </template>
        </FormModel>
    </QPage>
</template>
