<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 { useQuasar } from 'quasar';
import { useRouter } from 'vue-router';
import ShelvingFilter from 'pages/Shelving/Card/ShelvingFilter.vue';
import ShelvingSummaryDialog from 'pages/Shelving/Card/ShelvingSummaryDialog.vue';
import ShelvingSearchbar from 'pages/Shelving/Card/ShelvingSearchbar.vue';

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

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

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

function viewSummary(id) {
    quasar.dialog({
        component: ShelvingSummaryDialog,
        componentProps: {
            id,
        },
    });
}

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

<template>
    <template v-if="stateStore.isHeaderMounted()">
        <Teleport to="#searchbar">
            <ShelvingSearchbar />
        </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">
            <ShelvingFilter data-key="ShelvingList" />
        </QScrollArea>
    </QDrawer>
    <QPage class="column items-center q-pa-md">
        <div class="card-list">
            <VnPaginate
                data-key="ShelvingList"
                url="Shelvings"
                :filter="filter"
                :expr-builder="exprBuilder"
                auto-load
            >
                <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.openCard')"
                                @click.stop="navigate(row.id)"
                                class="bg-vn-dark"
                                outline
                            />
                            <QBtn
                                :label="t('components.smartCard.openSummary')"
                                @click.stop="viewSummary(row.id)"
                                color="primary"
                                style="margin-top: 15px"
                            />
                        </template>
                    </CardList>
                </template>
            </VnPaginate>
        </div>
        <QPageSticky :offset="[20, 20]">
            <RouterLink :to="{ name: 'ShelvingCreate' }">
                <QBtn fab icon="add" color="primary" />
                <QTooltip>
                    {{ t('shelving.list.newShelving') }}
                </QTooltip>
            </RouterLink>
        </QPageSticky>
    </QPage>
</template>

<style lang="scss" scoped>
.card-list {
    width: 100%;
    max-width: 60em;
}
</style>
