<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 VnSelectFilter from 'src/components/common/VnSelectFilter.vue';
import VnSelectDialog from 'src/components/common/VnSelectDialog.vue';
import FilterTravelForm from 'src/components/FilterTravelForm.vue';

import { toDate } from 'src/filters';

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

const suppliersOptions = ref([]);
const travelsOptions = ref([]);
const companiesOptions = ref([]);
const currenciesOptions = ref([]);

const onFilterTravelSelected = (formData, id) => {
    formData.travelFk = id;
};
</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) => (travelsOptions = data)"
        auto-load
    />
    <FetchData
        ref="companiesRef"
        url="Companies"
        :filter="{ fields: ['id', 'code'] }"
        order="code"
        @on-fetch="(data) => (companiesOptions = data)"
        auto-load
    />
    <FetchData
        ref="currenciesRef"
        url="Currencies"
        :filter="{ fields: ['id', 'code'] }"
        order="code"
        @on-fetch="(data) => (currenciesOptions = data)"
        auto-load
    />
    <FormModel
        :url="`Entries/${route.params.id}`"
        :url-update="`Entries/${route.params.id}`"
        model="entry"
        auto-load
        :clear-store-on-unmount="false"
    >
        <template #form="{ data }">
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnSelectFilter
                        :label="t('entry.basicData.supplier')"
                        v-model="data.supplierFk"
                        :options="suppliersOptions"
                        option-value="id"
                        option-label="nickname"
                        hide-selected
                        :required="true"
                        map-options
                    >
                        <template #option="scope">
                            <QItem v-bind="scope.itemProps">
                                <QItemSection>
                                    <QItemLabel>{{ scope.opt?.name }}</QItemLabel>
                                    <QItemLabel caption>
                                        {{ scope.opt?.nickname }}, {{ scope.opt?.id }}
                                    </QItemLabel>
                                </QItemSection>
                            </QItem>
                        </template>
                    </VnSelectFilter>
                </div>
                <div class="col">
                    <VnSelectDialog
                        :label="t('entry.basicData.travel')"
                        v-model="data.travelFk"
                        :options="travelsOptions"
                        option-value="id"
                        option-label="warehouseInName"
                        map-options
                        hide-selected
                        :required="true"
                        action-icon="filter_alt"
                    >
                        <template #form>
                            <FilterTravelForm
                                @travel-selected="onFilterTravelSelected(data, $event)"
                            />
                        </template>
                        <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>
                    </VnSelectDialog>
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnInput
                        v-model="data.reference"
                        :label="t('entry.basicData.reference')"
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnInput
                        v-model="data.invoiceNumber"
                        :label="t('entry.basicData.invoiceNumber')"
                    />
                </div>
                <div class="col">
                    <VnSelectFilter
                        :label="t('entry.basicData.company')"
                        v-model="data.companyFk"
                        :options="companiesOptions"
                        option-value="id"
                        option-label="code"
                        map-options
                        hide-selected
                        :required="true"
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnSelectFilter
                        :label="t('entry.basicData.currency')"
                        v-model="data.currencyFk"
                        :options="currenciesOptions"
                        option-value="id"
                        option-label="code"
                    />
                </div>
                <div class="col">
                    <QInput
                        :label="t('entry.basicData.commission')"
                        v-model="data.commission"
                        type="number"
                        autofocus
                        min="0"
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <QInput
                        :label="t('entry.basicData.observation')"
                        type="textarea"
                        v-model="data.observation"
                        :maxlength="45"
                        counter
                        fill-input
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <QCheckbox
                        v-model="data.isOrdered"
                        :label="t('entry.basicData.ordered')"
                    />
                </div>
                <div class="col">
                    <QCheckbox
                        v-model="data.isConfirmed"
                        :label="t('entry.basicData.confirmed')"
                    />
                </div>
                <div class="col">
                    <QCheckbox
                        v-model="data.isExcludedFromAvailable"
                        :label="t('entry.basicData.excludedFromAvailable')"
                    />
                </div>
                <div class="col">
                    <QCheckbox v-model="data.isRaid" :label="t('entry.basicData.raid')" />
                </div>
                <div class="col">
                    <QCheckbox
                        v-model="data.isBooked"
                        :label="t('entry.basicData.booked')"
                    />
                </div>
            </VnRow>
        </template>
    </FormModel>
</template>
