<script setup>
import { useI18n } from 'vue-i18n';
import { useRoute, useRouter } from 'vue-router';
import VnRow from 'components/ui/VnRow.vue';
import FormModel from 'components/FormModel.vue';
import VnInputDate from 'components/common/VnInputDate.vue';
import VnInput from 'components/common/VnInput.vue';
import VnInputTime from 'components/common/VnInputTime.vue';
import VnSelect from 'components/common/VnSelect.vue';
import FetchData from 'components/FetchData.vue';
import { ref } from 'vue';

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

const supplierList = ref([]);
const filter = { include: [{ relation: 'supplier' }] };
const onSave = (data, response) => {
    router.push({ name: 'RoadmapSummary', params: { id: response?.id } });
};
</script>
<template>
    <FetchData
        url="Suppliers"
        auto-load
        :filter="{ fields: ['id', 'nickname'] }"
        sort-by="nickname"
        limit="30"
        @on-fetch="(data) => (supplierList = data)"
    />
    <FormModel
        :url="`Roadmaps/${route.params?.id}`"
        observe-form-changes
        :filter="filter"
        model="roadmap"
        auto-load
        @on-data-saved="onSave"
    >
        <template #form="{ data }">
            <VnRow>
                <VnInput v-model="data.name" :label="t('Roadmap')" clearable />
                <VnInputDate v-model="data.etd" :label="t('ETD date')" />
                <VnInputTime v-model="data.etd" :label="t('ETD hour')" />
            </VnRow>
            <VnRow>
                <VnInput
                    v-model="data.tractorPlate"
                    :label="t('Tractor plate')"
                    clearable
                />
                <VnInput
                    v-model="data.trailerPlate"
                    :label="t('Trailer plate')"
                    clearable
                />
            </VnRow>
            <VnRow>
                <VnSelect
                    :label="t('Carrier')"
                    v-model="data.supplierFk"
                    :options="supplierList"
                    option-value="id"
                    option-label="nickname"
                    emit-value
                    map-options
                    use-input
                    :input-debounce="0"
                >
                    <template #option="{ itemProps, opt }">
                        <QItem v-bind="itemProps">
                            <QItemSection>
                                <QItemLabel
                                    >{{ opt.id }} - {{ opt.nickname }}
                                </QItemLabel>
                            </QItemSection>
                        </QItem>
                    </template>
                </VnSelect>
                <VnInput
                    v-model="data.price"
                    :label="t('Price')"
                    type="number"
                    clearable
                />
            </VnRow>
            <VnRow>
                <VnInput v-model="data.driverName" :label="t('Driver name')" clearable />
                <VnInput v-model="data.phone" :label="t('Phone')" clearable />
            </VnRow>
            <VnRow>
                <VnInput
                    v-model="data.observations"
                    :label="t('Observations')"
                    clearable
                />
            </VnRow>
        </template>
    </FormModel>
</template>
<i18n>
es:
    Roadmap: Troncal
    ETD date: Fecha ETD
    ETD hour: Hora ETD
    Tractor plate: Matrícula tractora
    Trailer plate: Matrícula remolque
    Carrier: Transportista
    Price: Precio
    Driver name: Nombre del conductor
    Phone: Teléfono
    Observations: Observaciones
</i18n>
