<script lang="ts">
	import ResourceCard from "$lib/components/resources/ResourceCard.svelte";
	import ResourceListPage from "$lib/components/resources/ResourceListPage.svelte";
	import Button from "$lib/components/ui/Button.svelte";
	import IconEdit from "~icons/tabler/edit";
	import IconPlus from "~icons/tabler/plus";

    let { data } = $props();
</script>

<ResourceListPage heading="Groups">
    {#snippet actionSnippet()}
        <Button Icon={IconPlus} a href="/dashboard/groups/new">Add Group</Button>
    {/snippet}

    {#snippet contentSnippet()}
        {#if data.groups.length === 0}
            <p>No groups found</p>
        {:else}
            <div class="flex gap-4 flex-wrap">
                {#each data.groups as group}
                    <ResourceCard title={group.name} subtitle="{data.userCounts[group.id]} users, {group.devices.length} devices">
                        {#snippet actionsSnippet()}
                            {#if data.user.admin}
                                <Button Icon={IconEdit} a href="/dashboard/groups/{group.id}" extra="!p-2"/>
                            {/if}
                        {/snippet}
                    </ResourceCard>
                {/each}
            </div>
        {/if}
    {/snippet}
</ResourceListPage>