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

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

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

const clients = ref();
const salespersons = ref();
const countries = ref();
const authors = ref();
</script>

<template>
    <FetchData @on-fetch="(data) => (clients = data)" auto-load url="Clients" />
    <FetchData
        :filter="{ where: { role: 'salesPerson' } }"
        @on-fetch="(data) => (salespersons = data)"
        auto-load
        url="Workers/activeWithInheritedRole"
    />
    <FetchData @on-fetch="(data) => (countries = data)" auto-load url="Countries" />
    <FetchData
        @on-fetch="(data) => (authors = data)"
        auto-load
        url="Workers/activeWithInheritedRole"
    />

    <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 class="q-mb-sm q-mt-sm">
                <QItemSection v-if="clients">
                    <VnSelect
                        :input-debounce="0"
                        :label="t('Client')"
                        :options="clients"
                        dense
                        emit-value
                        hide-selected
                        map-options
                        option-label="name"
                        option-value="clientTypeFk"
                        outlined
                        rounded
                        use-input
                        v-model="params.clientFk"
                    />
                </QItemSection>
                <QItemSection v-else>
                    <QSkeleton class="full-width" type="QInput" />
                </QItemSection>
            </QItem>

            <QItem class="q-mb-sm">
                <QItemSection v-if="salespersons">
                    <VnSelect
                        :input-debounce="0"
                        :label="t('Salesperson')"
                        :options="salespersons"
                        dense
                        emit-value
                        hide-selected
                        map-options
                        option-label="name"
                        option-value="id"
                        outlined
                        rounded
                        use-input
                        v-model="params.salesPersonFk"
                    />
                </QItemSection>
                <QItemSection v-else>
                    <QSkeleton class="full-width" type="QInput" />
                </QItemSection>
            </QItem>

            <QItem class="q-mb-sm">
                <QItemSection v-if="countries">
                    <VnSelect
                        :input-debounce="0"
                        :label="t('Country')"
                        :options="countries"
                        dense
                        emit-value
                        hide-selected
                        map-options
                        option-label="country"
                        option-value="id"
                        outlined
                        rounded
                        use-input
                        v-model="params.countryFk"
                    />
                </QItemSection>
                <QItemSection v-else>
                    <QSkeleton class="full-width" type="QInput" />
                </QItemSection>
            </QItem>

            <QItem class="q-mb-sm">
                <QItemSection>
                    <VnInput
                        :label="t('P. Method')"
                        clearable
                        is-outlined
                        v-model="params.paymentMethod"
                    />
                </QItemSection>
            </QItem>

            <QItem class="q-mb-sm">
                <QItemSection>
                    <VnInput
                        :label="t('Balance D.')"
                        clearable
                        is-outlined
                        v-model="params.balance"
                    />
                </QItemSection>
            </QItem>

            <QItem class="q-mb-sm">
                <QItemSection v-if="authors">
                    <VnSelect
                        :input-debounce="0"
                        :label="t('Author')"
                        :options="authors"
                        dense
                        emit-value
                        hide-selected
                        map-options
                        option-label="name"
                        option-value="id"
                        outlined
                        rounded
                        use-input
                        v-model="params.workerFk"
                    />
                </QItemSection>
                <QItemSection v-else>
                    <QSkeleton class="full-width" type="QInput" />
                </QItemSection>
            </QItem>

            <QItem class="q-mb-sm">
                <QItemSection>
                    <VnInput
                        :label="t('L. O. Date')"
                        clearable
                        is-outlined
                        v-model="params.date"
                    />
                </QItemSection>
            </QItem>

            <QItem class="q-mb-sm">
                <QItemSection>
                    <VnInput
                        :label="t('Credit I.')"
                        clearable
                        is-outlined
                        v-model="params.credit"
                    />
                </QItemSection>
            </QItem>

            <QItem class="q-mb-sm">
                <QItemSection>
                    <VnInputDate
                        :label="t('From')"
                        is-outlined
                        v-model="params.defaulterSinced"
                    />
                </QItemSection>
            </QItem>
            <QSeparator />
        </template>
    </VnFilterPanel>
</template>

<i18n>
en:
    params:
        clientFk: Client
        salesPersonFk: Salesperson
        countryFk: Country
        paymentMethod: P. Method
        balance: Balance D.
        workerFk: Author
        date: L. O. Date
        credit: Credit I.
        defaulterSinced: From
es:
    params:
        clientFk: Cliente
        salesPersonFk: Comercial
        countryFk: País
        paymentMethod: F. Pago
        balance: Saldo V.
        workerFk: Autor
        date: Fecha Ú. O.
        credit: Crédito A.
        defaulterSinced: Desde
    Client: Cliente
    Salesperson: Comercial
    Country: País
    P. Method: F. Pago
    Balance D.: Saldo V.
    Author: Autor
    L. O. Date: Fecha Ú. O.
    Credit I.: Crédito A.
    From: Desde
</i18n>
