<script setup>
import { ref } from 'vue';
import { useI18n } from 'vue-i18n';

import VnFilterPanel from 'src/components/ui/VnFilterPanel.vue';
import VnSelectFilter from 'src/components/common/VnSelectFilter.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 suppliersOptions = ref([]);
</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
    />
    <FetchData
        url="Suppliers"
        :filter="{ fields: ['id', 'nickname', 'name'] }"
        order="nickname"
        @on-fetch="(data) => (suppliersOptions = 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 }">
            <QItem>
                <QItemSection>
                    <VnInput
                        v-model="params.search"
                        :label="t('params.search')"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        v-model="params.reference"
                        :label="t('params.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>
                    <VnSelectFilter
                        :label="t('params.companyFk')"
                        v-model="params.companyFk"
                        :options="companiesOptions"
                        option-value="id"
                        option-label="code"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelectFilter
                        :label="t('params.currencyFk')"
                        v-model="params.currencyFk"
                        :options="currenciesOptions"
                        option-value="id"
                        option-label="name"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelectFilter
                        :label="t('params.supplierFk')"
                        v-model="params.supplierFk"
                        :options="suppliersOptions"
                        option-value="id"
                        option-label="name"
                        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>
                    </VnSelectFilter>
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInputDate
                        :label="t('params.created')"
                        is-outlined
                        v-model="params.created"
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInputDate
                        :label="t('params.from')"
                        is-outlined
                        v-model="params.from"
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInputDate
                        :label="t('params.to')"
                        is-outlined
                        v-model="params.to"
                    />
                </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:
        search: General search
        reference: Reference
        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:
        search: Búsqueda general
        reference: Referencia
        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>
