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

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

import axios from 'axios';
import { getParamWhere } from 'src/filters';
import { useRoute } from 'vue-router';

const { t } = useI18n();
const props = defineProps({
    dataKey: {
        type: String,
        required: true,
    },
    customTags: {
        type: Array,
        default: () => [],
    },
    exprBuilder: {
        type: Function,
        default: null,
    },
});

const route = useRoute();

const itemTypesOptions = ref([]);
const tagOptions = ref([]);
const tagValues = ref([]);
const categoryList = ref(null);
const selectedCategoryFk = ref(getParamWhere(route.query.table, 'categoryFk', false));
const selectedTypeFk = ref(getParamWhere(route.query.table, 'typeFk', false));

const selectedCategory = computed(() => {
    return (categoryList.value || []).find(
        (category) => category?.id === selectedCategoryFk.value,
    );
});

const selectedType = computed(() => {
    return (itemTypesOptions.value || []).find(
        (type) => type?.id === selectedTypeFk.value,
    );
});

const selectCategory = async (params, categoryId, search) => {
    if (params.categoryFk === categoryId) {
        resetCategory(params);
        search();
        return;
    }
    selectedCategoryFk.value = categoryId;
    params.categoryFk = categoryId;
    await fetchItemTypes(categoryId);
    search();
};

const resetCategory = (params) => {
    selectedCategoryFk.value = null;
    itemTypesOptions.value = null;
    if (params) {
        params.categoryFk = null;
        params.typeFk = null;
    }
};

const applyTags = (params, search) => {
    params.tags = tagValues.value
        .filter((tag) => tag.selectedTag && tag.value)
        .map((tag) => ({
            tagFk: tag.selectedTag.id,
            tagName: tag.selectedTag.name,
            value: tag.value,
        }));
    search();
};

const fetchItemTypes = async (id = selectedCategoryFk.value) => {
    const filter = {
        fields: ['id', 'name', 'categoryFk'],
        where: { categoryFk: id },
        include: 'category',
        order: 'name ASC',
    };
    const { data } = await axios.get('ItemTypes', {
        params: { filter: JSON.stringify(filter) },
    });
    itemTypesOptions.value = data;
};

const getCategoryClass = (category, params) => {
    if (category.id === params?.categoryFk) {
        return 'active';
    }
};

const getSelectedTagValues = async (tag) => {
    if (!tag?.selectedTag?.id) return;
    tag.value = null;
    const filter = {
        fields: ['value'],
        order: 'value ASC',
        limit: 30,
    };

    const params = { filter: JSON.stringify(filter) };
    const { data } = await axios.get(`Tags/${tag.selectedTag.id}/filterValue`, {
        params,
    });
    tag.valueOptions = data;
};

const removeTag = (index, params, search) => {
    (tagValues.value || []).splice(index, 1);
    applyTags(params, search);
};
const setCategoryList = (data) => {
    categoryList.value = (data || []).map((category) => ({
        ...category,
        icon: `vn:${(category.icon || '').split('-')[1]}`,
    }));
    fetchItemTypes();
};
</script>

<template>
    <FetchData
        url="ItemCategories"
        auto-load
        @on-fetch="setCategoryList"
        :where="{ display: { neq: 0 } }"
    />
    <FetchData
        url="Tags"
        :filter="{ fields: ['id', 'name', 'isFree'] }"
        auto-load
        @on-fetch="(data) => (tagOptions = data)"
    />
    <VnFilterPanel
        :data-key="props.dataKey"
        :expr-builder="props.exprBuilder"
        :custom-tags="props.customTags"
    >
        <template #tags="{ tag, formatFn }">
            <strong v-if="tag.label === 'categoryFk'">
                {{ t(selectedCategory?.name || '') }}
            </strong>
            <strong v-else-if="tag.label === 'typeFk'">
                {{ t(selectedType?.name || '') }}
            </strong>
            <div v-else class="q-gutter-x-xs">
                <strong>{{ t(`components.itemsFilterPanel.${tag.label}`) }}: </strong>
                <span>{{ formatFn(tag.value) }}</span>
            </div>
        </template>
        <template #customTags="{ tags, params }">
            <template v-for="tag in tags" :key="tag.label">
                <VnFilterPanelChip
                    v-for="chip in tag.value"
                    :key="chip"
                    removable
                    @remove="removeTagChip(chip, params, searchFn)"
                >
                    <div class="q-gutter-x-xs">
                        <strong>{{ chip.tagName }}: </strong>
                        <span>"{{ chip.value }}"</span>
                    </div>
                </VnFilterPanelChip>
            </template>
        </template>
        <template #body="{ params, searchFn }">
            <QItem class="category-filter q-mt-md">
                <QBtn
                    dense
                    flat
                    round
                    v-for="category in categoryList"
                    :key="category.name"
                    :class="['category', getCategoryClass(category, params)]"
                    :icon="category.icon"
                    @click="selectCategory(params, category.id, searchFn)"
                >
                    <QTooltip>
                        {{ t(category.name) }}
                    </QTooltip>
                </QBtn>
            </QItem>
            <QItem class="q-my-md">
                <QItemSection>
                    <VnSelect
                        :label="t('components.itemsFilterPanel.typeFk')"
                        v-model="params.typeFk"
                        :options="itemTypesOptions"
                        dense
                        outlined
                        rounded
                        use-input
                        :disable="!selectedCategoryFk"
                        @update:model-value="
                            (value) => {
                                selectedTypeFk = value;
                                searchFn();
                            }
                        "
                    >
                        <template #option="{ itemProps, opt }">
                            <QItem v-bind="itemProps">
                                <QItemSection>
                                    <QItemLabel>{{ opt.name }}</QItemLabel>
                                    <QItemLabel caption>
                                        {{ opt.categoryName }}
                                    </QItemLabel>
                                </QItemSection>
                            </QItem>
                        </template>
                    </VnSelect>
                </QItemSection>
            </QItem>
            <QSeparator />
            <slot name="body" :params="params" :search-fn="searchFn" />
            <QItem
                v-for="(value, index) in tagValues"
                :key="value"
                class="q-mt-md filter-value"
            >
                <QItemSection class="col">
                    <VnSelect
                        :label="t('globals.tag')"
                        v-model="value.selectedTag"
                        :options="tagOptions"
                        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('components.itemsFilterPanel.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"
                        @update:model-value="applyTags(params, searchFn)"
                    />
                    <VnInput
                        v-else
                        v-model="value.value"
                        :label="t('components.itemsFilterPanel.value')"
                        :disable="!value"
                        is-outlined
                        :is-clearable="false"
                        @keyup.enter="applyTags(params, searchFn)"
                    />
                </QItemSection>
                <QIcon
                    name="delete"
                    class="fill-icon-on-hover q-px-xs"
                    color="primary"
                    size="sm"
                    @click="removeTag(index, params, searchFn)"
                />
            </QItem>
            <QItem class="q-mt-lg">
                <QBtn
                    icon="add_circle"
                    v-shortcut="'+'"
                    flat
                    class="fill-icon-on-hover q-px-xs"
                    color="primary"
                    @click="tagValues.push({})"
                />
            </QItem>
        </template>
    </VnFilterPanel>
</template>

<style lang="scss" scoped>
.category-filter {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 12px;

    .category {
        padding: 8px;
        width: 60px;
        height: 60px;
        font-size: 1.4rem;
        background-color: var(--vn-accent-color);

        &.active {
            background-color: $primary;
        }
    }
}

.filter-value {
    display: flex;
    align-items: center;
}
</style>

<i18n>
en:
    params:
        supplier: Supplier
        from: From
        to: To
        active: Is active
        visible: Is visible
        floramondo: Is floramondo
        categoryFk: Category

es:
    params:
        supplier: Proveedor
        from: Desde
        to: Hasta
        active: Activo
        visible: Visible
        floramondo: Floramondo
        categoryFk: Categoría
    Plant: Planta natural
    Flower: Flor fresca
    Handmade: Hecho a mano
    Artificial: Artificial
    Green: Verdes frescos
    Accessories: Complementos florales
    Fruit: Fruta
</i18n>
