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

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

const { t } = useI18n();

defineProps({
    dataKey: {
        type: String,
        required: true,
    },
});

const itemTypeWorkersOptions = ref([]);
const suppliersOptions = ref([]);
const tagValues = ref([]);
</script>

<template>
    <FetchData
        url="TicketRequests/getItemTypeWorker"
        limit="30"
        auto-load
        :filter="{ fields: ['id', 'nickname'], order: 'nickname ASC', limit: 30 }"
        @on-fetch="(data) => (itemTypeWorkersOptions = data)"
    />
    <FetchData
        url="Suppliers"
        limit="30"
        auto-load
        :filter="{ fields: ['id', 'name', 'nickname'], order: 'name ASC', limit: 30 }"
        @on-fetch="(data) => (suppliersOptions = data)"
    />
    <ItemsFilterPanel :data-key="dataKey" :custom-tags="['tags']">
        <template #body="{ params, searchFn }">
            <QItem class="q-my-md">
                <QItemSection>
                    <VnSelect
                        :label="t('components.itemsFilterPanel.salesPersonFk')"
                        v-model="params.salesPersonFk"
                        :options="itemTypeWorkersOptions"
                        option-value="id"
                        option-label="nickname"
                        dense
                        outlined
                        rounded
                        use-input
                        @update:model-value="searchFn()"
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-my-md">
                <QItemSection>
                    <VnSelect
                        :label="t('components.itemsFilterPanel.supplierFk')"
                        v-model="params.supplierFk"
                        :options="suppliersOptions"
                        option-value="id"
                        option-label="name"
                        dense
                        outlined
                        rounded
                        use-input
                        @update:model-value="searchFn()"
                    >
                        <template #option="{ itemProps, opt }">
                            <QItem v-bind="itemProps">
                                <QItemSection>
                                    <QItemLabel>{{ opt.name }}</QItemLabel>
                                    <QItemLabel caption>
                                        {{ opt.nickname }}
                                    </QItemLabel>
                                </QItemSection>
                            </QItem>
                        </template>
                    </VnSelect>
                </QItemSection>
            </QItem>
            <QItem class="q-my-md">
                <QItemSection>
                    <VnInputDate
                        :label="t('components.itemsFilterPanel.from')"
                        v-model="params.from"
                        is-outlined
                        @update:model-value="searchFn()"
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-my-md">
                <QItemSection>
                    <VnInputDate
                        :label="t('components.itemsFilterPanel.to')"
                        v-model="params.to"
                        is-outlined
                        @update:model-value="searchFn()"
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <QCheckbox
                        :label="t('components.itemsFilterPanel.active')"
                        v-model="params.active"
                        toggle-indeterminate
                        @update:model-value="searchFn()"
                    />
                </QItemSection>
                <QItemSection>
                    <QCheckbox
                        :label="t('components.itemsFilterPanel.visible')"
                        v-model="params.visible"
                        toggle-indeterminate
                        @update:model-value="searchFn()"
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <QCheckbox
                        :label="t('components.itemsFilterPanel.floramondo')"
                        v-model="params.floramondo"
                        toggle-indeterminate
                        @update:model-value="searchFn()"
                    />
                </QItemSection>
            </QItem>

            <QItem
                v-for="(value, index) in tagValues"
                :key="value"
                class="q-mt-md filter-value"
            >
                <QItemSection class="col">
                    <VnSelect
                        :label="t('params.tag')"
                        v-model="value.selectedTag"
                        :options="tagOptions"
                        option-label="name"
                        dense
                        outlined
                        rounded
                        :emit-value="false"
                        use-input
                        :is-clearable="false"
                        @update:model-value="getSelectedTagValues(value)"
                    />
                </QItemSection>
                <QItemSection class="col">
                    <VnSelect
                        v-if="!value?.selectedTag?.isFree && value.valueOptions"
                        :label="t('params.value')"
                        v-model="value.value"
                        :options="value.valueOptions || []"
                        option-value="value"
                        option-label="value"
                        dense
                        outlined
                        rounded
                        emit-value
                        use-input
                        :disable="!value"
                        :is-clearable="false"
                        class="filter-input"
                        @update:model-value="applyTags(params, searchFn)"
                    />
                    <VnInput
                        v-else
                        v-model="value.value"
                        :label="t('params.value')"
                        :disable="!value"
                        is-outlined
                        class="filter-input"
                        :is-clearable="false"
                        @keyup.enter="applyTags(params, searchFn)"
                    />
                </QItemSection>
                <QIcon
                    name="delete"
                    class="filter-icon"
                    @click="removeTag(index, params, searchFn)"
                />
            </QItem>
        </template>
    </ItemsFilterPanel>
</template>
