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

import FormModel from 'components/FormModel.vue';
import VnRow from 'components/ui/VnRow.vue';
import VnSelectFilter from 'src/components/common/VnSelectFilter.vue';
import FetchData from 'components/FetchData.vue';
import VnSubToolbar from 'src/components/ui/VnSubToolbar.vue';

import { toDate } from 'src/filters';

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

const newEntryForm = reactive({
    supplierFk: null,
    travelFk: route.query?.travelFk || null,
    companyFk: null,
});

const suppliersOptions = ref([]);
const travelsOptionsOptions = ref([]);
const companiesOptions = ref([]);
</script>

<template>
    <FetchData
        url="Suppliers"
        :filter="{ fields: ['id', 'nickname'] }"
        order="nickname"
        @on-fetch="(data) => (suppliersOptions = data)"
        auto-load
    />
    <FetchData
        url="Travels/filter"
        :filter="{ fields: ['id', 'warehouseInName'] }"
        order="id"
        @on-fetch="(data) => (travelsOptionsOptions = data)"
        auto-load
    />
    <FetchData
        ref="companiesRef"
        url="Companies"
        :filter="{ fields: ['id', 'code'] }"
        order="code"
        @on-fetch="(data) => (companiesOptions = data)"
        auto-load
    />

    <!-- Agregar searchbar de entries -->

    <QPage>
        <VnSubToolbar />
        <FormModel url-create="Entries" model="entry" :form-initial-data="newEntryForm">
            <template #form="{ data, validate }">
                <VnRow class="row q-gutter-md q-mb-md">
                    <VnSelectFilter
                        :label="t('Supplier')"
                        class="full-width"
                        v-model="data.supplierFk"
                        :options="suppliersOptions"
                        option-value="id"
                        option-label="nickname"
                        hide-selected
                        :required="true"
                        :rules="validate('entry.supplierFk')"
                    >
                        <template #option="scope">
                            <QItem v-bind="scope.itemProps">
                                <QItemSection>
                                    <QItemLabel>{{ scope.opt?.nickname }}</QItemLabel>
                                    <QItemLabel caption>
                                        #{{ scope.opt?.id }}
                                    </QItemLabel>
                                </QItemSection>
                            </QItem>
                        </template>
                    </VnSelectFilter>
                </VnRow>
                <VnRow class="row q-gutter-md q-mb-md">
                    <VnSelectFilter
                        :label="t('Travel')"
                        class="full-width"
                        v-model="data.travelFk"
                        :options="travelsOptionsOptions"
                        option-value="id"
                        option-label="warehouseInName"
                        map-options
                        hide-selected
                        :required="true"
                        :rules="validate('entry.travelFk')"
                    >
                        <template #option="scope">
                            <QItem v-bind="scope.itemProps">
                                <QItemSection>
                                    <QItemLabel
                                        >{{ scope.opt?.agencyModeName }} -
                                        {{ scope.opt?.warehouseInName }} ({{
                                            toDate(scope.opt?.shipped)
                                        }}) &#x2192; {{ scope.opt?.warehouseOutName }} ({{
                                            toDate(scope.opt?.landed)
                                        }})</QItemLabel
                                    >
                                </QItemSection>
                            </QItem>
                        </template>
                    </VnSelectFilter>
                </VnRow>
                <VnRow class="row q-gutter-md q-mb-md">
                    <VnSelectFilter
                        :label="t('Company')"
                        class="full-width"
                        v-model="data.companyFk"
                        :options="companiesOptions"
                        option-value="id"
                        option-label="code"
                        map-options
                        hide-selected
                        :required="true"
                        :rules="validate('entry.companyFk')"
                    />
                </VnRow>
            </template>
        </FormModel>
    </QPage>
</template>

<i18n>
    es:
        Supplier: Proveedor
        Travel: Envío
        Company: Empresa
</i18n>
