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

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 RoleSummary from './Card/RoleSummary.vue';
import RoleForm from './Card/RoleForm.vue';
import VnSearchbar from 'components/ui/VnSearchbar.vue';
import AccountRolesFilter from './AccountRolesFilter.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 roleCreateDialogRef = ref(null);

const exprBuilder = (param, value) => {
    switch (param) {
        case 'search':
            return /^\d+$/.test(value)
                ? { id: value }
                : {
                      or: [
                          { name: { like: `%${value}%` } },
                          { nickname: { like: `%${value}%` } },
                      ],
                  };
        case 'name':
        case 'description':
            return { [param]: { like: `%${value}%` } };
    }
};

const openCreateModal = () => roleCreateDialogRef.value.show();

const getApiUrl = () => new URL(window.location).origin;

const navigate = (event, id) => {
    if (event.ctrlKey || event.metaKey)
        return window.open(`${getApiUrl()}/#/account/role/${id}/summary`);
    router.push({ name: 'RoleSummary', params: { id } });
};
</script>

<template>
    <template v-if="stateStore.isHeaderMounted()">
        <Teleport to="#searchbar">
            <VnSearchbar
                data-key="RolesList"
                url="VnRoles"
                :label="t('role.searchRoles')"
                :info="t('role.searchInfo')"
            />
        </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">
            <AccountRolesFilter data-key="RolesList" :expr-builder="exprBuilder" />
        </QScrollArea>
    </QDrawer>
    <QPage class="column items-center q-pa-md">
        <div class="vn-card-list">
            <VnPaginate data-key="RolesList" url="VnRoles">
                <template #body="{ rows }">
                    <CardList
                        :id="row.id"
                        :key="row.id"
                        :title="row.name"
                        @click="navigate($event, row.id)"
                        v-for="row of rows"
                    >
                        <template #list-items>
                            <div style="flex-direction: column; width: 100%">
                                <VnLv :label="t('role.card.name')" :value="row.name">
                                </VnLv>
                                <VnLv
                                    :label="t('role.card.description')"
                                    :value="row.description"
                                >
                                </VnLv>
                            </div>
                        </template>
                        <template #actions>
                            <QBtn
                                :label="t('components.smartCard.openSummary')"
                                @click.stop="viewSummary(row.id, RoleSummary)"
                                color="primary"
                                style="margin-top: 15px"
                            />
                        </template>
                    </CardList>
                </template>
            </VnPaginate>
        </div>
        <QDialog
            ref="roleCreateDialogRef"
            transition-show="scale"
            transition-hide="scale"
        >
            <RoleForm />
        </QDialog>
        <QPageSticky :offset="[20, 20]">
            <QBtn fab icon="add" color="primary" @click="openCreateModal()" />
            <QTooltip>
                {{ t('role.newRole') }}
            </QTooltip>
        </QPageSticky>
    </QPage>
</template>
