<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 VnSubToolbar from 'src/components/ui/VnSubToolbar.vue';
import VnInputDate from 'components/common/VnInputDate.vue';
import VnInput from 'components/common/VnInput.vue';
import VnInputTime from 'components/common/VnInputTime.vue';
import VnSelectFilter from 'components/common/VnSelectFilter.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>
    <VnSubToolbar />
    <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 class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnInput v-model="data.name" :label="t('Roadmap')" clearable />
                </div>
                <div class="col">
                    <VnInputDate v-model="data.etd" :label="t('ETD date')" />
                </div>
                <div class="col">
                    <VnInputTime v-model="data.etd" :label="t('ETD hour')" />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnInput
                        v-model="data.tractorPlate"
                        :label="t('Tractor plate')"
                        clearable
                    />
                </div>
                <div class="col">
                    <VnInput
                        v-model="data.trailerPlate"
                        :label="t('Trailer plate')"
                        clearable
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnSelectFilter
                        :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>
                    </VnSelectFilter>
                </div>
                <div class="col">
                    <VnInput
                        v-model="data.price"
                        :label="t('Price')"
                        type="number"
                        clearable
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnInput
                        v-model="data.driverName"
                        :label="t('Driver name')"
                        clearable
                    />
                </div>
                <div class="col">
                    <VnInput
                        v-model="data.phone"
                        :label="t('Phone')"
                        clearable
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnInput
                        v-model="data.observations"
                        :label="t('Observations')"
                        clearable
                    />
                </div>
            </VnRow>
        </template>
    </FormModel>
</template>
<i18n>
es:
    Roadmap: Troncal
    ETD date: Fecha ETD
    ETD hour: Hora ETD
    Tractor plate: Matrícula tractor
    Trailer plate: Matrícula trailer
    Carrier: Transportista
    Price: Precio
    Driver name: Nombre del conductor
    Phone: Teléfono
    Observations: Observaciones
</i18n>
