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

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

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

const agenciesOptions = ref([]);
const warehousesOptionsOut = ref([]);
const warehousesOptionsIn = ref([]);
</script>
<template>
    <FetchData
        url="AgencyModes"
        @on-fetch="(data) => (agenciesOptions = data)"
        auto-load
    />
    <FetchData
        url="Warehouses"
        @on-fetch="(data) => (warehousesOptionsOut = data)"
        auto-load
        :filter="{ where: { isOrigin: TRUE } }"
    />
    <FetchData
        url="Warehouses"
        @on-fetch="(data) => (warehousesOptionsIn = data)"
        auto-load
        :filter="{ where: { isDestiny: TRUE } }"
    />
    <FormModel :url-update="`Travels/${route.params.id}`" model="Travel" auto-load>
        <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="id"
                    option-label="name"
                    map-options
                    hide-selected
                />
            </VnRow>
            <VnRow>
                <VnInputDate v-model="data.shipped" :label="t('globals.shipped')" />
                <VnInputDate v-model="data.landed" :label="t('globals.landed')" />
            </VnRow>

            <VnRow>
                <VnSelect
                    :label="t('globals.warehouseOut')"
                    v-model="data.warehouseOutFk"
                    :options="warehousesOptionsOut"
                    option-value="id"
                    option-label="name"
                    map-options
                    hide-selected
                />
                <VnSelect
                    :label="t('globals.warehouseIn')"
                    v-model="data.warehouseInFk"
                    :options="warehousesOptionsIn"
                    option-value="id"
                    option-label="name"
                    map-options
                    hide-selected
                />
            </VnRow>
            <VnRow>
                <VnInput v-model="data.m3" label="m3" />
                <VnInput
                    :label="t('travel.basicData.daysInForward')"
                    v-model="data.daysInForward"
                >
                    <template #append>
                        <QIcon name="info" class="cursor-info">
                            <QTooltip>{{ t('raidDays') }}</QTooltip>
                        </QIcon>
                    </template>
                </VnInput>
            </VnRow>
            <VnRow>
                <QCheckbox v-model="data.isRaid" :label="t('travel.basicData.isRaid')" />
                <QCheckbox
                    :label="t('travel.summary.delivered')"
                    v-model="data.isDelivered"
                />
                <QCheckbox
                    :label="t('travel.summary.received')"
                    v-model="data.isReceived"
                />
            </VnRow>
        </template>
    </FormModel>
</template>

<i18n>
es:
    raidDays: El travel se desplaza automáticamente cada día para estar desde hoy al número de días indicado. Si se deja vacio no se moverá
en:
    raidDays: The travel adjusts itself daily to match the number of days set, starting from today. If left blank, it won’t move
</i18n>
