<script setup>
import VnPaginate from 'components/ui/VnPaginate.vue';
import { useStateStore } from 'stores/useStateStore';
import { useI18n } from 'vue-i18n';
import { onMounted, onUnmounted } from 'vue';
import CardList from 'components/ui/CardList.vue';
import VnLv from 'components/ui/VnLv.vue';
import { useRouter } from 'vue-router';
import ShelvingFilter from 'pages/Shelving/Card/ShelvingFilter.vue';
import ShelvingSummary from 'pages/Shelving/Card/ShelvingSummary.vue';
import ShelvingSearchbar from 'pages/Shelving/Card/ShelvingSearchbar.vue';
import { useSummaryDialog } from 'src/composables/useSummaryDialog';
import RightMenu from 'src/components/common/RightMenu.vue';

const stateStore = useStateStore();
const router = useRouter();
const { t } = useI18n();
const { viewSummary } = useSummaryDialog();
const filter = {
    include: [{ relation: 'parking' }],
};

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

function navigate(id) {
    router.push({ path: `/shelving/${id}` });
}

function exprBuilder(param, value) {
    switch (param) {
        case 'search':
            return { code: { like: `%${value}%` } };
        case 'parkingFk':
        case 'userFk':
        case 'isRecyclable':
            return { [param]: value };
    }
}
</script>

<template>
    <ShelvingSearchbar />
    <RightMenu>
        <template #right-panel>
            <ShelvingFilter data-key="ShelvingList" />
        </template>
    </RightMenu>
    <QPage class="column items-center q-pa-md">
        <div class="vn-card-list">
            <VnPaginate
                data-key="ShelvingList"
                url="Shelvings"
                :filter="filter"
                :expr-builder="exprBuilder"
                :limit="20"
            >
                <template #body="{ rows }">
                    <CardList
                        v-for="row of rows"
                        :key="row.id"
                        :id="row.id"
                        :title="row.code"
                        @click="navigate(row.id)"
                    >
                        <template #list-items>
                            <VnLv
                                :label="t('shelving.list.parking')"
                                :title-label="t('shelving.list.parking')"
                                :value="row.parking?.code"
                            />
                            <VnLv
                                :label="t('shelving.list.priority')"
                                :value="row?.priority"
                            />
                        </template>
                        <template #actions>
                            <QBtn
                                :label="t('components.smartCard.openSummary')"
                                @click.stop="viewSummary(row.id, ShelvingSummary)"
                                color="primary"
                            />
                        </template>
                    </CardList>
                </template>
            </VnPaginate>
        </div>
        <QPageSticky :offset="[20, 20]">
            <RouterLink :to="{ name: 'ShelvingCreate' }">
                <QBtn fab icon="add" color="primary" shortcut="+" />
                <QTooltip>
                    {{ t('shelving.list.newShelving') }}
                </QTooltip>
            </RouterLink>
        </QPageSticky>
    </QPage>
</template>
