<script setup>
import { useStateStore } from 'stores/useStateStore';
import { useRoute } from 'vue-router';
import { onMounted, onUnmounted, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import VnPaginate from 'components/ui/VnPaginate.vue';
import VnSearchbar from 'components/ui/VnSearchbar.vue';
import OrderCatalogItem from 'pages/Order/Card/OrderCatalogItem.vue';
import OrderCatalogFilter from 'pages/Order/Card/OrderCatalogFilter.vue';

const route = useRoute();
const stateStore = useStateStore();
const { t } = useI18n();

onMounted(() => (stateStore.rightDrawer = true));
onUnmounted(() => (stateStore.rightDrawer = false));

const catalogParams = {
    orderFk: route.params.id,
    orderBy: JSON.stringify({ field: 'relevancy DESC, name', way: 'ASC', isTag: false }),
};

const tags = ref([]);

function extractTags(items) {
    const resultTags = [];
    (items || []).forEach((item) => {
        (item.tags || []).forEach((tag) => {
            const index = resultTags.findIndex((item) => item.tagFk === tag.tagFk);
            if (index === -1) {
                resultTags.push({ ...tag, priority: 1 });
            } else {
                resultTags[index].priority += 1;
            }
        });
    });
    tags.value = resultTags;
}
</script>

<template>
    <Teleport to="#searchbar" v-if="stateStore.isHeaderMounted()">
        <VnSearchbar
            data-key="OrderCatalogList"
            url="Orders/CatalogFilter"
            :limit="50"
            :user-params="catalogParams"
            :static-params="['orderFk', 'orderBy']"
            :redirect="false"
        />
    </Teleport>
    <Teleport v-if="stateStore.isHeaderMounted()" to="#actions-append">
        <div class="row q-gutter-x-sm">
            <QBtn
                flat
                @click.stop="stateStore.toggleRightDrawer()"
                round
                dense
                icon="menu"
            >
                <QTooltip bottom anchor="bottom right">
                    {{ t('globals.collapseMenu') }}
                </QTooltip>
            </QBtn>
        </div>
    </Teleport>
    <QDrawer v-model="stateStore.rightDrawer" side="right" :width="256" show-if-above>
        <QScrollArea class="fit text-grey-8">
            <OrderCatalogFilter data-key="OrderCatalogList" :tags="tags" />
        </QScrollArea>
    </QDrawer>
    <QPage class="column items-center q-pa-md">
        <div class="full-width">
            <VnPaginate
                data-key="OrderCatalogList"
                url="Orders/CatalogFilter"
                :limit="50"
                :user-params="catalogParams"
                auto-load
                @on-fetch="extractTags"
            >
                <template #body="{ rows }">
                    <div class="catalog-list">
                        <div v-if="rows && !rows?.length" class="no-result">
                            {{ t('globals.noResults') }}
                        </div>
                        <OrderCatalogItem v-for="row in rows" :key="row.id" :item="row" />
                    </div>
                </template>
            </VnPaginate>
        </div>
    </QPage>
</template>

<style lang="scss" scoped>
.catalog-list {
    display: flex;
    align-items: flex-start;
    flex-wrap: wrap;
    justify-content: center;
    gap: 16px;
}

.no-result {
    font-size: 24px;
    font-weight: bold;
    color: var(--vn-label);
    text-align: center;
}
</style>
