<script setup>
import { useI18n } from 'vue-i18n';
import { ref } from 'vue';
import FetchData from 'components/FetchData.vue';

import VnPaginate from 'components/ui/VnPaginate.vue';
import VnSearchbar from 'components/ui/VnSearchbar.vue';
import CardList from 'src/components/ui/CardList.vue';
import VnLv from 'src/components/ui/VnLv.vue';
import AclFilter from './Acls/AclFilter.vue';

import { useVnConfirm } from 'composables/useVnConfirm';
import { useStateStore } from 'stores/useStateStore';
import axios from 'axios';
import useNotify from 'src/composables/useNotify.js';
import AclFormView from './Acls/AclFormView.vue';

defineProps({
    id: {
        type: Number,
        default: 0,
    },
});

const { notify } = useNotify();
const { t } = useI18n();
const stateStore = useStateStore();
const { openConfirmationModal } = useVnConfirm();

const paginateRef = ref();
const formDialog = ref(false);
const rolesOptions = ref([]);

const exprBuilder = (param, value) => {
    switch (param) {
        case 'search':
            return { model: { like: `%${value}%` } };
        default:
            return { [param]: value };
    }
};

const deleteAcl = async (id) => {
    try {
        await axios.delete(`ACLs/${id}`);
        paginateRef.value.fetch();
        notify('ACL removed', 'positive');
    } catch (error) {
        console.error('Error deleting Acl: ', error);
    }
};
function showFormDialog(data) {
    formDialog.value = {
        show: true,
        formInitialData: { ...data },
    };
}
</script>

<template>
    <FetchData
        url="VnRoles"
        :filter="{ fields: ['name'], order: 'name ASC' }"
        @on-fetch="(data) => (rolesOptions = data)"
        auto-load
    />
    <template v-if="stateStore.isHeaderMounted()">
        <Teleport to="#searchbar">
            <VnSearchbar
                data-key="AccountAcls"
                url="ACLs"
                :expr-builder="exprBuilder"
                :label="t('acls.search')"
                :info="t('acls.searchInfo')"
            />
        </Teleport>
    </template>
    <QDrawer v-model="stateStore.rightDrawer" side="right" :width="256" show-if-above>
        <QScrollArea class="fit text-grey-8">
            <AclFilter data-key="AccountAcls" />
        </QScrollArea>
    </QDrawer>

    <QPage class="flex justify-center q-pa-md">
        <div class="vn-card-list">
            <VnPaginate
                ref="paginateRef"
                data-key="AccountAcls"
                url="ACLs"
                :expr-builder="exprBuilder"
            >
                <template #body="{ rows }">
                    <CardList
                        v-for="row of rows"
                        :id="row.id"
                        :key="row.id"
                        :title="`${row.model}.${row.property}`"
                        @click="showFormDialog(row)"
                    >
                        <template #list-items>
                            <VnLv :label="t('acls.role')" :value="row.principalId" />
                            <VnLv :label="t('acls.accessType')" :value="row.accessType" />
                            <VnLv
                                :label="t('acls.permissions')"
                                :value="row.permission"
                            />
                        </template>
                        <template #actions>
                            <QBtn
                                :label="t('globals.delete')"
                                @click.stop="
                                    openConfirmationModal(
                                        t('ACL will be removed'),
                                        t('Are you sure you want to continue?'),
                                        () => deleteAcl(row.id)
                                    )
                                "
                                color="primary"
                                style="margin-top: 15px"
                            />
                        </template>
                    </CardList>
                </template>
            </VnPaginate>
        </div>
        <QDialog
            v-model="formDialog.show"
            transition-show="scale"
            transition-hide="scale"
        >
            <AclFormView
                :form-initial-data="formDialog.formInitialData"
                @on-data-change="paginateRef.fetch()"
                :roles-options="rolesOptions"
            />
        </QDialog>
        <QPageSticky position="bottom-right" :offset="[18, 18]">
            <QBtn fab icon="add" color="primary" @click="showFormDialog()">
                <QTooltip class="text-no-wrap">{{ t('New ACL') }}</QTooltip>
            </QBtn>
        </QPageSticky>
    </QPage>
</template>

<i18n>
es:
    New ACL: Nuevo ACL
    ACL removed: ACL eliminado
    ACL will be removed: El ACL será eliminado
    Are you sure you want to continue?: ¿Seguro que quieres continuar?
</i18n>
