<script setup lang="ts">
definePageMeta({
    layout: 'admin',
    middleware: ['is-admin'],
});

const session = useAuthSession();
const adminStore = useAdminStore();
</script>

<template>
    <div class="grid grid-cols-2 gap-4">
        <Card>
            <CardHeader>
                <CardTitle
                    ><NuxtLink to="/admin/users">Users</NuxtLink></CardTitle
                >
                <CardDescription
                    >Add users or modify existing users' permissions or
                    warrens</CardDescription
                >
            </CardHeader>
            <CardContent class="max-h-64">
                <ScrollArea class="h-full">
                    <div class="flex flex-col gap-4">
                        <AdminUserListing
                            v-for="user in adminStore.users"
                            :key="user.id"
                            :user
                            class="group/user flex flex-row items-center justify-between gap-4"
                        >
                            <template #actions>
                                <Button
                                    class="m-1"
                                    variant="destructive"
                                    size="icon"
                                    :disabled="session?.user.id === user.id"
                                    @click="
                                        () =>
                                            adminStore.openDeleteUserDialog(
                                                user
                                            )
                                    "
                                >
                                    <Icon name="lucide:trash-2" />
                                </Button>
                            </template>
                        </AdminUserListing>
                    </div>
                </ScrollArea>
            </CardContent>
            <CardFooter>
                <div class="mt-4 flex flex-row">
                    <Button @click="adminStore.openCreateUserDialog"
                        >Create user</Button
                    >
                </div>
            </CardFooter>
        </Card>
    </div>
</template>
