<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 ItemTypeSearchbar from '../ItemType/ItemTypeSearchbar.vue';
import { useSummaryDialog } from 'src/composables/useSummaryDialog';
import RightMenu from 'src/components/common/RightMenu.vue';

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>
    <ItemTypeSearchbar />
    <RightMenu>
        <template #right-panel>
            <ItemTypeFilter data-key="ItemTypeList" />
        </template>
    </RightMenu>
    <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()"
            shortcut="+"
        />
        <QTooltip>
            {{ t('New item type') }}
        </QTooltip>
    </QPageSticky>
</template>
