<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 VnSelectFilter from 'components/common/VnSelectFilter.vue';
import VnInput from 'src/components/common/VnInput.vue';

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

const provinces = ref();
const workers = ref();
const zones = ref();
</script>

<template>
    <FetchData url="Provinces" @on-fetch="(data) => (provinces = data)" auto-load />
    <FetchData url="Zones" @on-fetch="(data) => (zones = data)" auto-load />
    <FetchData
        url="Workers/activeWithInheritedRole"
        :filter="{ where: { role: 'salesPerson' } }"
        @on-fetch="(data) => (workers = 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 class="q-my-sm">
                <QItemSection>
                    <VnInput :label="t('FI')" v-model="params.fi" is-outlined>
                        <template #prepend>
                            <QIcon name="badge" size="xs" />
                        </template>
                    </VnInput>
                </QItemSection>
            </QItem>
            <QItem class="q-mb-sm">
                <QItemSection>
                    <VnInput :label="t('Name')" v-model="params.name" is-outlined />
                </QItemSection>
            </QItem>
            <QItem class="q-mb-sm">
                <QItemSection>
                    <VnInput
                        :label="t('Social Name')"
                        v-model="params.socialName"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-mb-sm">
                <QItemSection v-if="!workers">
                    <QSkeleton type="QInput" class="full-width" />
                </QItemSection>
                <QItemSection v-if="workers">
                    <VnSelectFilter
                        :label="t('Salesperson')"
                        v-model="params.salesPersonFk"
                        @update:model-value="searchFn()"
                        :options="workers"
                        option-value="id"
                        option-label="name"
                        emit-value
                        map-options
                        use-input
                        hide-selected
                        dense
                        outlined
                        rounded
                        :input-debounce="0"
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-mb-sm">
                <QItemSection v-if="!provinces">
                    <QSkeleton type="QInput" class="full-width" />
                </QItemSection>
                <QItemSection v-if="provinces">
                    <VnSelectFilter
                        :label="t('Province')"
                        v-model="params.provinceFk"
                        @update:model-value="searchFn()"
                        :options="provinces"
                        option-value="id"
                        option-label="name"
                        emit-value
                        map-options
                        hide-selected
                        dense
                        outlined
                        rounded
                        :input-debounce="0"
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-mb-md">
                <QItemSection>
                    <VnInput :label="t('City')" v-model="params.city" is-outlined />
                </QItemSection>
            </QItem>
            <QSeparator />
            <QExpansionItem :label="t('More options')" expand-separator>
                <QItem>
                    <QItemSection>
                        <VnInput :label="t('Phone')" v-model="params.phone" is-outlined>
                            <template #prepend>
                                <QIcon name="phone" size="xs" />
                            </template>
                        </VnInput>
                    </QItemSection>
                </QItem>
                <QItem>
                    <QItemSection>
                        <VnInput :label="t('Email')" v-model="params.email" is-outlined>
                            <template #prepend>
                                <QIcon name="email" size="sm" />
                            </template>
                        </VnInput>
                    </QItemSection>
                </QItem>
                <QItem>
                    <QItemSection v-if="!zones">
                        <QSkeleton type="QInput" class="full-width" />
                    </QItemSection>
                    <QItemSection v-if="zones">
                        <VnSelectFilter
                            :label="t('Zone')"
                            v-model="params.zoneFk"
                            @update:model-value="searchFn()"
                            :options="zones"
                            option-value="id"
                            option-label="name"
                            emit-value
                            map-options
                            hide-selected
                            dense
                            outlined
                            rounded
                        />
                    </QItemSection>
                </QItem>
                <QItem>
                    <QItemSection>
                        <VnInput
                            :label="t('Postcode')"
                            v-model="params.postcode"
                            is-outlined
                        />
                    </QItemSection>
                </QItem>
            </QExpansionItem>
        </template>
    </VnFilterPanel>
</template>

<i18n>
en:
    params:
        search: Contains
        fi: FI
        name: Name
        socialName: Social Name
        salesPersonFk: Salesperson
        provinceFk: Province
        city: City
        phone: Phone
        email: Email
        zoneFk: Zone
        postcode: Postcode
es:
    params:
        search: Contiene
        fi: NIF
        name: Nombre
        socialName: Razón Social
        salesPersonFk: Comercial
        provinceFk: Provincia
        city: Ciudad
        phone: Teléfono
        email: Email
        zoneFk: Zona
        postcode: CP
    FI: NIF
    Name: Nombre
    Social Name: Razón social
    Salesperson: Comercial
    Province: Provincia
    City: Ciudad
    More options: Más opciones
    Phone: Teléfono
    Email: Email
    Zone: Zona
    Postcode: Código postal
</i18n>
