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

import VnPaginate from 'src/components/ui/VnPaginate.vue';
import VnLv from 'src/components/ui/VnLv.vue';
import CardList from 'src/components/ui/CardList.vue';
import ItemTypeSummary from 'src/pages/ItemType/Card/ItemTypeSummary.vue';
import ItemTypeFilter from 'src/pages/ItemType/ItemTypeFilter.vue';
import VnSearchbar from 'src/components/ui/VnSearchbar.vue';

import { useStateStore } from 'stores/useStateStore';
import { useSummaryDialog } from 'src/composables/useSummaryDialog';

const stateStore = useStateStore();
const router = useRouter();
const { t } = useI18n();
const { viewSummary } = useSummaryDialog();

const redirectToItemTypeSummary = (id) => {
    router.push({ name: 'ItemTypeSummary', params: { id } });
};

const redirectToCreateView = () => {
    router.push({ name: 'ItemTypeCreate' });
};

const exprBuilder = (param, value) => {
    switch (param) {
        case 'name':
            return {
                name: { like: `%${value}%` },
            };
        case 'code':
            return {
                code: { like: `%${value}%` },
            };
        case 'search':
            if (value) {
                if (!isNaN(value)) {
                    return { id: value };
                } else {
                    return {
                        or: [
                            {
                                name: {
                                    like: `%${value}%`,
                                },
                            },
                            {
                                code: {
                                    like: `%${value}%`,
                                },
                            },
                        ],
                    };
                }
            }
    }
};
</script>

<template>
    <template v-if="stateStore.isHeaderMounted()">
        <Teleport to="#searchbar">
            <VnSearchbar
                data-key="ItemTypeList"
                url="ItemTypes"
                :label="t('Search item type')"
                :info="t('Search itemType by id, name or code')"
                :expr-builder="exprBuilder"
            />
        </Teleport>
        <Teleport to="#actions-append">
            <div class="row q-gutter-x-sm">
                <QBtn
                    flat
                    @click="stateStore.toggleRightDrawer()"
                    round
                    dense
                    icon="menu"
                >
                    <QTooltip bottom anchor="bottom right">
                        {{ t('globals.collapseMenu') }}
                    </QTooltip>
                </QBtn>
            </div>
        </Teleport>
    </template>
    <QDrawer v-model="stateStore.rightDrawer" side="right" :width="256" show-if-above>
        <QScrollArea class="fit text-grey-8">
            <ItemTypeFilter data-key="ItemTypeList" />
        </QScrollArea>
    </QDrawer>
    <QPage class="column items-center q-pa-md">
        <div class="vn-card-list">
            <VnPaginate
                data-key="ItemTypeList"
                url="ItemTypes"
                :order="['name']"
                auto-load
                :expr-builder="exprBuilder"
            >
                <template #body="{ rows }">
                    <CardList
                        v-for="row of rows"
                        :key="row.id"
                        :title="row.code"
                        @click="redirectToItemTypeSummary(row.id)"
                        :id="row.id"
                    >
                        <template #list-items>
                            <VnLv :label="t('Name')" :value="row.name" />
                        </template>
                        <template #actions>
                            <QBtn
                                :label="t('components.smartCard.openSummary')"
                                @click.stop="viewSummary(row.id, ItemTypeSummary)"
                                color="primary"
                                type="submit"
                            />
                        </template>
                    </CardList>
                </template>
            </VnPaginate>
        </div>
    </QPage>
    <QPageSticky :offset="[20, 20]">
        <QBtn fab icon="add" color="primary" @click="redirectToCreateView()" />
        <QTooltip>
            {{ t('New item type') }}
        </QTooltip>
    </QPageSticky>
</template>

<i18n>
es:
    New item type: Nueva familia
    Name: Nombre
    Search item type: Buscar familia
    Search itemType by id, name or code: Buscar familia por id, nombre o código
</i18n>
