<script setup>
import { onMounted, ref } from 'vue';
import { useRoute } from 'vue-router';
import { useI18n } from 'vue-i18n';
import { useRole } from 'src/composables/useRole';
import { useState } from 'src/composables/useState';
import { checkEntryLock } from 'src/composables/checkEntryLock';
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 VnInputNumber from 'src/components/common/VnInputNumber.vue';
import VnSelectTravelExtended from 'src/components/common/VnSelectTravelExtended.vue';
import VnSelectSupplier from 'src/components/common/VnSelectSupplier.vue';
import VnCheckbox from 'src/components/common/VnCheckbox.vue';
import VnSelectWorker from 'src/components/common/VnSelectWorker.vue';
import VnDmsInput from 'src/components/common/VnDmsInput.vue';

const route = useRoute();
const { t } = useI18n();
const { hasAny } = useRole();
const isAdministrative = () => hasAny(['administrative']);
const state = useState();
const user = state.getUser().fn();

const companiesOptions = ref([]);
const currenciesOptions = ref([]);
const entryRef = ref({});

onMounted(() => {
    checkEntryLock(route.params.id, user.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
        ref="entryRef"
        model="Entry"
        :url-update="`Entries/${route.params.id}`"
        :clear-store-on-unmount="false"
        auto-load
    >
        <template #form="{ data }">
            <VnRow class="q-py-sm">
                <VnSelectTravelExtended
                    :data="data"
                    v-model="data.travelFk"
                    :onFilterTravelSelected="(data, result) => (data.travelFk = result)"
                />
                <VnSelectSupplier
                    v-model="data.supplierFk"
                    hide-selected
                    :required="true"
                />
            </VnRow>
            <VnRow class="q-py-sm">
                <VnInput
                    v-model="data.reference"
                    :label="t('entry.list.tableVisibleColumns.reference')"
                />
                <VnSelect
                    v-model="data.typeFk"
                    url="entryTypes"
                    :fields="['code', 'description']"
                    option-value="code"
                    optionLabel="description"
                    sortBy="description"
                    :label="t('entry.list.tableVisibleColumns.entryTypeDescription')"
                />
            </VnRow>
            <VnRow class="q-py-sm">
                <VnInput
                    v-model="data.invoiceNumber"
                    :label="t('entry.summary.invoiceNumber')"
                />
                <VnSelect
                    :label="t('globals.company')"
                    v-model="data.companyFk"
                    :options="companiesOptions"
                    option-value="id"
                    option-label="code"
                    sort-by="code"
                    map-options
                    hide-selected
                    :required="true"
                />
            </VnRow>
            <VnRow class="q-py-sm">
                <VnInputNumber
                    :label="t('entry.summary.commission')"
                    v-model="data.commission"
                    :step="1"
                    :positive="false"
                />
                <VnSelect
                    :label="t('entry.summary.currency')"
                    v-model="data.currencyFk"
                    :options="currenciesOptions"
                    option-value="id"
                    option-label="code"
                    sort-by="code"
                />
            </VnRow>
            <VnRow class="q-py-sm">
                <VnInputNumber
                    v-model="data.initialTemperature"
                    name="initialTemperature"
                    :label="t('entry.basicData.initialTemperature')"
                    :step="0.5"
                    :positive="false"
                />
                <VnInputNumber
                    v-model="data.finalTemperature"
                    name="finalTemperature"
                    :label="t('entry.basicData.finalTemperature')"
                    :step="0.5"
                    :positive="false"
                />
            </VnRow>
            <VnRow class="q-py-sm">
                <VnInputNumber
                    v-model="data.invoiceAmount"
                    :label="t('entry.list.tableVisibleColumns.invoiceAmount')"
                    :positive="false"
                    @update:model-value="data.buyerFk = user.id"
                />
                <VnSelectWorker v-model="data.buyerFk" hide-selected />
                <VnDmsInput :data="data" :formRef="entryRef" :disable="false" />
            </VnRow>
            <VnRow class="q-py-sm">
                <VnInputNumber
                    :label="t('entry.basicData.observation')"
                    type="textarea"
                    v-model="data.observation"
                    :maxlength="45"
                    counter
                    fill-input
                />
            </VnRow>
            <VnRow class="q-py-sm">
                <VnCheckbox
                    v-model="data.isOrdered"
                    :label="t('entry.list.tableVisibleColumns.isOrdered')"
                />
                <VnCheckbox
                    v-model="data.isConfirmed"
                    :label="t('entry.list.tableVisibleColumns.isConfirmed')"
                />
                <VnCheckbox
                    v-model="data.isExcludedFromAvailable"
                    :label="t('entry.list.tableVisibleColumns.isExcludedFromAvailable')"
                />
                <VnCheckbox
                    :disable="!isAdministrative()"
                    v-model="data.isBooked"
                    :label="t('entry.basicData.booked')"
                />
            </VnRow>
        </template>
    </FormModel>
</template>
