<script setup lang="ts">
import { Head, Link } from '@inertiajs/vue3';
import { Plus, Search } from '@lucide/vue';
import Heading from '@/components/Heading.vue';
import Pagination from '@/components/Pagination.vue';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Badge } from '@/components/ui/badge';
import { dashboard } from '@/routes';
import type { BreadcrumbItem } from '@/types';

interface Bank {
    id: number;
    kode: string;
    keterangan: string;
    status: boolean;
}

interface Paginated {
    data: Bank[];
    from: number;
    to: number;
    total: number;
    links: { url: string | null; label: string; active: boolean }[];
}

const props = defineProps<{
    banks: Paginated;
    filters: { search?: string };
}>();

defineOptions({
    layout: {
        breadcrumbs: [
            { title: 'Dashboard', href: dashboard() },
            { title: 'Bank', href: '/banks' },
        ] as BreadcrumbItem[],
    },
});
</script>

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

    <div class="flex flex-col space-y-6">
        <div class="flex items-center justify-between gap-4">
            <Heading title="Master Bank" description="Rekening tujuan pembayaran deposit" />
            <Button as-child>
                <Link href="/banks/create"><Plus class="size-4" /> Tambah Bank</Link>
            </Button>
        </div>

        <div class="w-full max-w-sm">
            <form method="GET" action="/banks">
                <div class="relative">
                    <Search class="absolute left-3 top-1/2 size-4 -translate-y-1/2 text-muted-foreground" />
                    <Input name="search" :default-value="props.filters.search ?? ''" placeholder="Cari bank…" class="pl-9" />
                </div>
            </form>
        </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">Kode</th>
                        <th class="px-4 py-3 font-medium">Keterangan</th>
                        <th class="px-4 py-3 font-medium text-center">Status</th>
                        <th class="px-4 py-3 font-medium text-right">Aksi</th>
                    </tr>
                </thead>
                <tbody class="divide-y">
                    <tr v-for="b in props.banks.data" :key="b.id" class="hover:bg-muted/30">
                        <td class="px-4 py-3 font-mono font-medium">{{ b.kode }}</td>
                        <td class="px-4 py-3 text-muted-foreground">{{ b.keterangan }}</td>
                        <td class="px-4 py-3 text-center">
                            <Badge :variant="b.status ? 'default' : 'outline'">{{ b.status ? 'Aktif' : 'Nonaktif' }}</Badge>
                        </td>
                        <td class="px-4 py-3 text-right">
                            <Button as-child variant="ghost" size="sm">
                                <Link :href="`/banks/${b.id}/edit`">Edit</Link>
                            </Button>
                        </td>
                    </tr>
                    <tr v-if="props.banks.data.length === 0">
                        <td colspan="4" class="px-4 py-8 text-center text-muted-foreground">Belum ada bank.</td>
                    </tr>
                </tbody>
            </table>
        </div>

        <Pagination :links="props.banks.links" :from="props.banks.from" :to="props.banks.to" :total="props.banks.total" />
    </div>
</template>
