<script setup>
import { ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { onMounted } from 'vue';
import { useStateStore } from 'stores/useStateStore';

import VnFilterPanel from 'src/components/ui/VnFilterPanel.vue';
import VnSelect from 'src/components/common/VnSelect.vue';
import VnInput from 'src/components/common/VnInput.vue';
import FetchData from 'components/FetchData.vue';
import VnInputDate from 'src/components/common/VnInputDate.vue';

const { t } = useI18n();
const props = defineProps({
    dataKey: {
        type: String,
        required: true,
    },
});

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

const stateStore = useStateStore();
onMounted(async () => {
    stateStore.rightDrawer = true;
});
</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', 'name'] }"
        order="code"
        @on-fetch="(data) => (currenciesOptions = data)"
        auto-load
    />
    <VnFilterPanel :data-key="props.dataKey" :search-button="true">
        <template #tags="{ tag, formatFn }">
            <div class="q-gutter-x-xs">
                <strong>{{ t(`params.${tag.label}`) }}: </strong>
                <span>{{ formatFn(tag.value) }}</span>
            </div>
        </template>
        <template #body="{ params, searchFn }">
            <QItem>
                <QItemSection>
                    <VnInput
                        v-model="params.search"
                        :label="t('entryFilter.filter.search')"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        v-model="params.reference"
                        :label="t('entryFilter.filter.reference')"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        v-model="params.invoiceNumber"
                        :label="t('params.invoiceNumber')"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        v-model="params.travelFk"
                        :label="t('params.travelFk')"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelect
                        :label="t('params.companyFk')"
                        v-model="params.companyFk"
                        @update:model-value="searchFn()"
                        :options="companiesOptions"
                        option-value="id"
                        option-label="code"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelect
                        :label="t('params.currencyFk')"
                        v-model="params.currencyFk"
                        @update:model-value="searchFn()"
                        :options="currenciesOptions"
                        option-value="id"
                        option-label="name"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelect
                        :label="t('params.supplierFk')"
                        v-model="params.supplierFk"
                        @update:model-value="searchFn()"
                        url="Suppliers"
                        option-value="id"
                        option-label="name"
                        :fields="['id', 'name', 'nickname']"
                        sort-by="nickname"
                        hide-selected
                        dense
                        outlined
                        rounded
                    >
                        <template #option="scope">
                            <QItem v-bind="scope.itemProps">
                                <QItemSection>
                                    <QItemLabel>{{
                                        scope.opt?.name + ': ' + scope.opt?.nickname
                                    }}</QItemLabel>
                                </QItemSection>
                            </QItem>
                        </template>
                    </VnSelect>
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInputDate
                        :label="t('params.created')"
                        v-model="params.created"
                        @update:model-value="searchFn()"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInputDate
                        :label="t('params.from')"
                        v-model="params.from"
                        @update:model-value="searchFn()"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInputDate
                        :label="t('params.to')"
                        v-model="params.to"
                        @update:model-value="searchFn()"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <QCheckbox
                        :label="t('params.isBooked')"
                        v-model="params.isBooked"
                        toggle-indeterminate
                    />
                </QItemSection>
                <QItemSection>
                    <QCheckbox
                        :label="t('params.isConfirmed')"
                        v-model="params.isConfirmed"
                        toggle-indeterminate
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <QCheckbox
                        :label="t('params.isOrdered')"
                        v-model="params.isOrdered"
                        toggle-indeterminate
                    />
                </QItemSection>
            </QItem>
        </template>
    </VnFilterPanel>
</template>

<i18n>
en:
    params:
        
        invoiceNumber: Invoice number
        travelFk: Travel
        companyFk: Company
        currencyFk: Currency
        supplierFk: Supplier
        from: From
        to: To
        created: Created
        isBooked: Booked
        isConfirmed: Confirmed
        isOrdered: Ordered
es:
    params:
       
        invoiceNumber: Núm. factura
        travelFk: Envío
        companyFk: Empresa
        currencyFk: Moneda
        supplierFk: Proveedor
        from: Desde
        to: Hasta
        created: Fecha creación
        isBooked: Asentado
        isConfirmed: Confirmado
        isOrdered: Pedida
</i18n>
