<script setup lang="ts">
import { Head, Link, useForm } from '@inertiajs/vue3';
import { Plus } from '@lucide/vue';
import Heading from '@/components/Heading.vue';
import InputError from '@/components/InputError.vue';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Badge } from '@/components/ui/badge';
import {
    Dialog,
    DialogContent,
    DialogDescription,
    DialogFooter,
    DialogHeader,
    DialogTitle,
    DialogTrigger,
} from '@/components/ui/dialog';
import { dashboard } from '@/routes';
import type { BreadcrumbItem } from '@/types';

interface Group {
    id: number;
    name: string;
    description: string | null;
    users_count: number;
}

defineProps<{
    groups: Group[];
}>();

defineOptions({
    layout: {
        breadcrumbs: [
            { title: 'Dashboard', href: dashboard() },
            { title: 'Grup', href: '/groups' },
        ] as BreadcrumbItem[],
    },
});

const coreGroups = ['admin', 'members', 'developer'];

const form = useForm({
    name: '',
    description: '',
});

const submit = () => {
    form.post('/groups', {
        onSuccess: () => form.reset(),
    });
};
</script>

<template>
    <Head title="Grup" />

    <div class="flex flex-col space-y-6">
        <div class="flex items-center justify-between gap-4">
            <Heading title="Grup & Hak Akses" description="Kelola grup pengguna dan jumlah anggota" />
            <Dialog>
                <DialogTrigger as-child>
                    <Button>
                        <Plus class="size-4" />
                        Tambah Grup
                    </Button>
                </DialogTrigger>
                <DialogContent>
                    <DialogHeader>
                        <DialogTitle>Grup Baru</DialogTitle>
                        <DialogDescription>Buat grup untuk mengatur hak akses menu.</DialogDescription>
                    </DialogHeader>
                    <form @submit.prevent="submit" class="space-y-4">
                        <div class="grid gap-2">
                            <Label for="name">Nama Grup</Label>
                            <Input id="name" v-model="form.name" required />
                            <InputError :message="form.errors.name" />
                        </div>
                        <div class="grid gap-2">
                            <Label for="description">Keterangan</Label>
                            <Input id="description" v-model="form.description" />
                            <InputError :message="form.errors.description" />
                        </div>
                        <DialogFooter>
                            <Button type="submit" :disabled="form.processing">Simpan</Button>
                        </DialogFooter>
                    </form>
                </DialogContent>
            </Dialog>
        </div>

        <div class="overflow-hidden rounded-xl border">
            <table class="w-full text-sm">
                <thead class="bg-muted/50 text-left text-xs uppercase text-muted-foreground">
                    <tr>
                        <th class="px-4 py-3 font-medium">Nama</th>
                        <th class="px-4 py-3 font-medium">Keterangan</th>
                        <th class="px-4 py-3 font-medium text-center">Anggota</th>
                        <th class="px-4 py-3 font-medium text-center">Tipe</th>
                    </tr>
                </thead>
                <tbody class="divide-y">
                    <tr v-for="group in groups" :key="group.id" class="hover:bg-muted/30">
                        <td class="px-4 py-3 font-medium capitalize">{{ group.name }}</td>
                        <td class="px-4 py-3 text-muted-foreground">{{ group.description ?? '—' }}</td>
                        <td class="px-4 py-3 text-center tabular-nums">{{ group.users_count }}</td>
                        <td class="px-4 py-3 text-center">
                            <Badge v-if="coreGroups.includes(group.name)" variant="secondary">Inti</Badge>
                            <Badge v-else variant="outline">Kustom</Badge>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</template>
