<template>
    <div class="flex items-center justify-between">
        <h1 class="text-3xl font-bold uppercase">Liste des utilisateurs</h1>
        <NuxtLink
            class="flex items-center justify-center text-xl uppercase bg-primary-500 text-white h-[50px] w-[272px]"
            @click="router.push('/admin/user/')"
        >
            Ajouter
        </NuxtLink>

    </div>

    <div>
        <div class="mt-6 border border-slate-200 mb-16 ">
            <div class="grid grid-cols-3 gap-4 bg-slate-100 px-4 py-3 text-sm font-semibold uppercase tracking-wide">
                <div>Username</div>
                <div>Role</div>
            </div>
            <div
                v-for="user in userList"
                :key="user.id"
                class="grid grid-cols-3 gap-4 px-4 py-3 text-sm hover:bg-slate-50 cursor-pointer border-t items-center"
                role="button"
                tabindex="0"
                @click="goToUser(user.id)"
            >
                <div>
                    {{ user.username }}
                </div>
                <div>
                    {{ user.roles?.join(', ') || ' ---' }}
                </div>
            </div>
        </div>
    </div>
</template>


<script setup lang="ts">
definePageMeta({
    layout: 'admin'
})

import type {UserData} from "~/services/dto/user-data";
import {getAdminUsers, getUsers} from "~/services/auth";

const userList = ref<UserData[]>([])
const router = useRouter()

const goToUser = (id: number) => {
    router.push(`/admin/user/${id}`)
}

onMounted(async () => {
    userList.value = await getAdminUsers()
})
</script>
