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

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 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 { hasAny } = useRole();
const isAdministrative = () => hasAny(['administrative']);

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

const onFilterTravelSelected = (formData, id) => {
    formData.travelFk = id;
};
</script>
<template>
    <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>
                <VnSelect
                    :label="t('entry.basicData.supplier')"
                    v-model="data.supplierFk"
                    url="Suppliers"
                    option-value="id"
                    option-label="nickname"
                    :fields="['id', '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>
                </VnSelect>
                <VnSelectDialog
                    :label="t('entry.basicData.travel')"
                    v-model="data.travelFk"
                    url="Travels/filter"
                    :fields="['id', 'warehouseInName']"
                    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>
            </VnRow>
            <VnRow>
                <VnInput
                    v-model="data.reference"
                    :label="t('entry.basicData.reference')"
                />
            </VnRow>
            <VnRow>
                <VnInput
                    v-model="data.invoiceNumber"
                    :label="t('entry.basicData.invoiceNumber')"
                />
                <VnSelect
                    :label="t('entry.basicData.company')"
                    v-model="data.companyFk"
                    :options="companiesOptions"
                    option-value="id"
                    option-label="code"
                    map-options
                    hide-selected
                    :required="true"
                />
            </VnRow>
            <VnRow>
                <VnSelect
                    :label="t('entry.basicData.currency')"
                    v-model="data.currencyFk"
                    :options="currenciesOptions"
                    option-value="id"
                    option-label="code"
                />
                <QInput
                    :label="t('entry.basicData.commission')"
                    v-model="data.commission"
                    type="number"
                    autofocus
                    min="0"
                />
            </VnRow>
            <VnRow>
                <QInput
                    :label="t('entry.basicData.observation')"
                    type="textarea"
                    v-model="data.observation"
                    :maxlength="45"
                    counter
                    fill-input
                />
            </VnRow>
            <VnRow>
                <QCheckbox
                    v-model="data.isOrdered"
                    :label="t('entry.basicData.ordered')"
                />
                <QCheckbox
                    v-model="data.isConfirmed"
                    :label="t('entry.basicData.confirmed')"
                />
                <QCheckbox
                    v-model="data.isExcludedFromAvailable"
                    :label="t('entry.basicData.excludedFromAvailable')"
                />
                <QCheckbox v-model="data.isRaid" :label="t('entry.basicData.raid')" />
                <QCheckbox
                    v-if="isAdministrative()"
                    v-model="data.isBooked"
                    :label="t('entry.basicData.booked')"
                />
            </VnRow>
        </template>
    </FormModel>
</template>
