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

import VnSelectFilter from 'components/common/VnSelectFilter.vue';
import VnFilterPanel from 'src/components/ui/VnFilterPanel.vue';
import FetchData from 'components/FetchData.vue';
import VnInput from 'src/components/common/VnInput.vue';
import VnInputDate from 'components/common/VnInputDate.vue';
import { useCapitalize } from 'src/composables/useCapitalize';
import VnCurrency from 'src/components/common/VnCurrency.vue';

const { t } = useI18n();
const props = defineProps({
    dataKey: {
        type: String,
        required: true,
    },
});
const suppliers = ref([]);
const suppliersRef = ref();
</script>

<template>
    <FetchData
        ref="suppliersRef"
        url="Suppliers"
        :filter="{ fields: ['id', 'nickname'] }"
        order="nickname"
        limit="30"
        @on-fetch="(data) => (suppliers = data)"
    />
    <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
                        :label="t('Id or Supplier')"
                        v-model="params.search"
                        is-outlined
                    >
                        <template #prepend>
                            <QIcon name="badge" size="sm"></QIcon>
                        </template>
                    </VnInput>
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        :label="useCapitalize(t('params.correctedFk'))"
                        v-model="params.correctedFk"
                        is-outlined
                    >
                        <template #prepend>
                            <QIcon name="attachment" size="sm" />
                        </template>
                    </VnInput>
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        :label="t('params.supplierRef')"
                        v-model="params.supplierRef"
                        is-outlined
                        lazy-rules
                    >
                        <template #prepend>
                            <QIcon name="vn:client" size="sm"></QIcon>
                        </template>
                    </VnInput>
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelectFilter
                        :label="t('params.supplierFk')"
                        v-model="params.supplierFk"
                        :options="suppliers"
                        option-value="id"
                        option-label="nickname"
                        @input-value="suppliersRef.fetch()"
                        dense
                        outlined
                        rounded
                    >
                    </VnSelectFilter>
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        :label="t('params.fi')"
                        v-model="params.fi"
                        is-outlined
                        lazy-rules
                    >
                        <template #prepend>
                            <QIcon name="badge" size="sm"></QIcon>
                        </template>
                    </VnInput>
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        :label="t('params.serialNumber')"
                        v-model="params.serialNumber"
                        is-outlined
                        lazy-rules
                    >
                        <template #prepend>
                            <QIcon name="badge" size="sm"></QIcon>
                        </template>
                    </VnInput>
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        :label="t('params.serial')"
                        v-model="params.serial"
                        is-outlined
                        lazy-rules
                    >
                        <template #prepend>
                            <QIcon name="badge" size="sm"></QIcon>
                        </template>
                    </VnInput>
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnCurrency v-model="params.amount" is-outlined />
                </QItemSection>
            </QItem>
            <QItem class="q-mb-md">
                <QItemSection>
                    <QCheckbox
                        :label="t('params.isBooked')"
                        v-model="params.isBooked"
                        @update:model-value="searchFn()"
                        toggle-indeterminate
                    />
                </QItemSection>
            </QItem>
            <QExpansionItem :label="t('More options')" expand-separator>
                <QItem>
                    <QItemSection>
                        <VnInput
                            :label="t('params.awb')"
                            v-model="params.awbCode"
                            is-outlined
                            lazy-rules
                        >
                            <template #prepend>
                                <QIcon name="badge" size="sm"></QIcon>
                            </template>
                        </VnInput>
                    </QItemSection>
                </QItem>
                <QItem>
                    <QItemSection>
                        <VnInput
                            :label="t('params.account')"
                            v-model="params.account"
                            is-outlined
                            lazy-rules
                        >
                            <template #prepend>
                                <QIcon name="person" size="sm" />
                            </template>
                        </VnInput>
                    </QItemSection>
                </QItem>
                <QItem>
                    <QItemSection>
                        <VnInputDate
                            :label="t('From')"
                            v-model="params.from"
                            is-outlined
                        />
                    </QItemSection>
                </QItem>
                <QItem>
                    <QItemSection>
                        <VnInputDate :label="t('To')" v-model="params.to" is-outlined />
                    </QItemSection>
                </QItem>
                <QItem>
                    <QItemSection>
                        <VnInputDate
                            :label="t('Issued')"
                            v-model="params.issued"
                            is-outlined
                        />
                    </QItemSection>
                </QItem>
            </QExpansionItem>
        </template>
    </VnFilterPanel>
</template>

<i18n>
en:
    params:
        search: ID
        supplierRef: Supplier ref.
        supplierFk: Supplier
        fi: Supplier fiscal id
        clientFk: Customer
        amount: Amount
        created: Created
        awb: AWB
        dued: Dued
        serialNumber: Serial Number
        serial: Serial
        account: Account
        isBooked: is booked
        correctedFk: Rectificatives
es:
    params:
        search: Contiene
        supplierRef: Ref. proveedor
        supplierFk: Proveedor
        clientFk: Cliente
        fi: CIF proveedor
        serialNumber: Num. serie
        serial: Serie
        awb: AWB
        amount: Importe
        issued: Emitida
        isBooked: Conciliada
        account: Cuenta
        created: Creada
        dued: Vencida
        correctedFk: Rectificativas
    From: Desde
    To: Hasta
    Amount: Importe
    Issued: Fecha factura
    Id or supplier: Id o proveedor
    More options: Más opciones
</i18n>
