<script setup>
import { ref } from 'vue';
import { useI18n } from 'vue-i18n';
import FetchData from 'components/FetchData.vue';
import VnFilterPanel from 'components/ui/VnFilterPanel.vue';

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

const emit = defineEmits(['search']);

const workers = ref();
const parkings = ref();

function setWorkers(data) {
    workers.value = data;
}

function setParkings(data) {
    parkings.value = data;
}
</script>

<template>
    <FetchData
        url="Parkings"
        :filter="{ fields: ['id', 'code'] }"
        sort-by="code ASC"
        @on-fetch="setParkings"
        auto-load
    />
    <FetchData
        url="Workers/activeWithInheritedRole"
        :filter="{ where: { role: 'salesPerson' } }"
        sort-by="firstName ASC"
        @on-fetch="setWorkers"
        auto-load
    />
    <VnFilterPanel
        :data-key="props.dataKey"
        :search-button="true"
        @search="emit('search')"
    >
        <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-my-sm">
                <QItemSection v-if="!parkings">
                    <QSkeleton type="QInput" class="full-width" />
                </QItemSection>
                <QItemSection v-if="parkings">
                    <QSelect
                        dense
                        outlined
                        rounded
                        :label="t('params.parkingFk')"
                        v-model="params.parkingFk"
                        :options="parkings"
                        option-value="id"
                        option-label="code"
                        emit-value
                        map-options
                        use-input
                        :input-debounce="0"
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-mb-sm">
                <QItemSection v-if="!workers">
                    <QSkeleton type="QInput" class="full-width" />
                </QItemSection>
                <QItemSection v-if="workers">
                    <QSelect
                        dense
                        outlined
                        rounded
                        :label="t('params.userFk')"
                        v-model="params.userFk"
                        :options="workers"
                        option-value="id"
                        option-label="name"
                        emit-value
                        map-options
                        use-input
                        :input-debounce="0"
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-mb-md">
                <QItemSection>
                    <QCheckbox
                        v-model="params.isRecyclable"
                        :label="t('params.isRecyclable')"
                        toggle-indeterminate
                    />
                </QItemSection>
            </QItem>
        </template>
    </VnFilterPanel>
</template>

<i18n>
en:
    params:
        parkingFk: Parking
        userFk: Worker
        isRecyclable: Recyclable
        search: Search
es:
    params:
        parkingFk: Parking
        userFk: Trabajador
        isRecyclable: Reciclable
        search: Contiene
</i18n>
