<template>
    <div class="flex items-center justify-between">
        <h1 class="text-3xl font-bold uppercase text-primary-500">Liste des utilisateurs</h1>
        <NuxtLink
            class="inline-flex items-center justify-center gap-2 text-xl uppercase bg-primary-500 text-white h-[50px] px-8 rounded-md"
            @click="router.push('/admin/user/')"
        >
            <Icon name="mdi:plus" size="28"  />
            Ajouter
        </NuxtLink>

    </div>

    <UiDataTable
        :columns="columns"
        url="admin/users"
        @row-click="onUserRowClick"
    />
</template>


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

import {ROLE} from "~/utils/constants";
import type {ColumnConfig, Row} from "~/services/datatable";
import {formatRoleLabels} from "~/utils/datatable-formatters";

const router = useRouter()
const roleLabelByValue = new Map(ROLE.map((role) => [role.value, role.label]))

const columns: ColumnConfig[] = [
    { key: "username", label: "Username" },
    { key: "roles", label: "Role", format: (value) => formatRoleLabels(value, roleLabelByValue) },
]

const onUserRowClick = (row: Row) => {
    const id = Number(row.id)
    if (!Number.isFinite(id)) return
    router.push(`/admin/user/${id}`)
}
</script>
