<script setup>
import { useRoute, useRouter } from 'vue-router';
import { computed, ref, watch } from 'vue';

import VnPaginate from 'components/ui/VnPaginate.vue';

import { useArrayData } from 'composables/useArrayData';

const props = defineProps({
    dataKey: { type: String, required: true },
});

const route = useRoute();
const router = useRouter();

const paginateRef = ref(null);

const arrayData = useArrayData(props.dataKey);
const store = arrayData.store;

const data = computed(() => {
    const dataCopy = store.data;
    return dataCopy.sort((a, b) => a.inherits?.name.localeCompare(b.inherits?.name));
});

const filter = computed(() => ({
    where: { role: route.params.id },
    include: {
        relation: 'inherits',
        scope: {
            fields: ['id', 'name', 'description'],
        },
    },
}));

const urlPath = computed(() => 'RoleRoles');

const columns = computed(() => [
    {
        name: 'name',
    },
]);

watch(
    () => route.params.id,
    () => {
        store.url = urlPath.value;
        store.filter = filter.value;
        fetchSubRoles();
    }
);

const fetchSubRoles = () => paginateRef.value.fetch();

const redirectToRoleSummary = (id) =>
    router.push({ name: 'RoleSummary', params: { id } });
</script>

<template>
    <QPage class="column items-center q-pa-md">
        <div class="full-width" style="max-width: 400px">
            <VnPaginate
                ref="paginateRef"
                :data-key="dataKey"
                :filter="filter"
                :url="urlPath"
                auto-load
            >
                <template #body>
                    <QTable :rows="data" :columns="columns" hide-header>
                        <template #body="{ row }">
                            <QTr
                                @click="redirectToRoleSummary(row.inherits?.id)"
                                class="cursor-pointer"
                            >
                                <QTd>
                                    <div class="column">
                                        <span>{{ row.inherits?.name }}</span>
                                        <span class="color-vn-label">{{
                                            row.inherits?.description
                                        }}</span>
                                    </div>
                                </QTd>
                            </QTr>
                        </template>
                    </QTable>
                </template>
            </VnPaginate>
        </div>
    </QPage>
</template>

<i18n>
es:
    Role removed. Changes will take a while to fully propagate.: Rol eliminado. Los cambios tardaran un tiempo en propagarse completamente.
    Role added! Changes will take a while to fully propagate.: ¡Rol añadido! Los cambios tardaran un tiempo en propagarse completamente.
    El rol va a ser eliminado: Role will be removed
    ¿Seguro que quieres continuar?: Are you sure you want to continue?
</i18n>
