<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 { dashboard } from '@/routes';
import type { BreadcrumbItem } from '@/types';

interface Placement {
    id: number;
    kode: string;
    keterangan: string | null;
    url: string | null;
}

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

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

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

<template>
    <Head title="Ads Placement" />

    <div class="flex flex-col space-y-6">
        <div class="flex items-center justify-between gap-4">
            <Heading title="Ads Placement" description="Sumber lalu lintas iklan (mis. fb, gl)" />
            <Button as-child>
                <Link href="/placements/create"><Plus class="size-4" /> Tambah Placement</Link>
            </Button>
        </div>

        <div class="w-full max-w-sm">
            <form method="GET" action="/placements">
                <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 placement…" 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">URL</th>
                        <th class="px-4 py-3 font-medium text-right">Aksi</th>
                    </tr>
                </thead>
                <tbody class="divide-y">
                    <tr v-for="p in props.placements.data" :key="p.id" class="hover:bg-muted/30">
                        <td class="px-4 py-3 font-mono font-medium">{{ p.kode }}</td>
                        <td class="px-4 py-3 text-muted-foreground">{{ p.keterangan ?? '—' }}</td>
                        <td class="px-4 py-3 text-xs text-muted-foreground">{{ p.url ?? '—' }}</td>
                        <td class="px-4 py-3 text-right">
                            <Button as-child variant="ghost" size="sm">
                                <Link :href="`/placements/${p.id}/edit`">Edit</Link>
                            </Button>
                        </td>
                    </tr>
                    <tr v-if="props.placements.data.length === 0">
                        <td colspan="4" class="px-4 py-8 text-center text-muted-foreground">Belum ada placement.</td>
                    </tr>
                </tbody>
            </table>
        </div>

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