<script setup lang="ts">
import { Head, Link, usePage } from '@inertiajs/vue3';
import { ArrowBigLeft, 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 Campaign {
    id: number;
    judul: string;
    url: string;
    budget_harian: number | null;
    klik: number;
    status: number;
    kategoris?: { id: number; nama_kategori: string }[];
    user?: { name: string } | null;
    created_at: string;
}

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

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

defineOptions({
    layout: {
        breadcrumbs: [
            { title: 'Dashboard', href: dashboard() },
            { title: 'Campaign', href: '/campaigns' },
            { title: 'Draft', href: '/campaigns/drafts' },
        ] as BreadcrumbItem[],
    },
});

const page = usePage();
const isAdmin = page.props.auth.user?.is_admin ?? false;
</script>

<template>
    <Head title="Draft Campaign" />

    <div class="flex flex-col space-y-6">
        <div class="flex items-center justify-between gap-4">
            <Heading title="Draft Campaign" description="Campaign tersimpan — bisa diaktifkan kembali" />
            <Button as-child>
                <Link href="/campaigns">
                    <ArrowBigLeft class="size-4" />
                    Kembali
                </Link>
            </Button>
        </div>

        <div class="w-full max-w-sm">
            <form method="GET" action="/campaigns/drafts">
                <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 judul campaign…" 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 text-right">ID</th>
                        <th class="px-4 py-3 font-medium">Judul</th>
                        <th v-if="isAdmin" class="px-4 py-3 font-medium">Kategori</th>
                        <th class="px-4 py-3 font-medium text-right">Budget Harian</th>
                        <th class="px-4 py-3 font-medium">Dibuat</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="c in props.campaigns.data" :key="c.id" class="hover:bg-muted/30">
                        <td class="px-4 py-3 text-right tabular-nums text-muted-foreground">{{ c.id }}</td>
                        <td class="px-4 py-3 font-medium">
                            {{ c.judul }}
                            <div class="text-xs text-muted-foreground">{{ c.user?.name }}</div>
                        </td>
                        <td v-if="isAdmin" class="px-4 py-3 text-muted-foreground">{{ c.kategoris?.map(k => k.nama_kategori).join(', ') || '—' }}</td>
                        <td class="px-4 py-3 text-right tabular-nums">Rp {{ (c.budget_harian ?? 0).toLocaleString('id-ID') }}</td>
                        <td class="px-4 py-3 text-muted-foreground">
                            <div class="tabular-nums">{{ new Date(c.created_at).toLocaleDateString('id-ID', { day: '2-digit', month: 'short', year: 'numeric' }) }}</div>
                            <div class="text-xs tabular-nums">{{ new Date(c.created_at).toLocaleTimeString('id-ID', { hour: '2-digit', minute: '2-digit' }) }}</div>
                        </td>
                        <td class="px-4 py-3 text-center">
                            <Badge variant="outline">Draft</Badge>
                        </td>
                        <td class="px-4 py-3 text-right">
                            <div class="flex flex-wrap justify-end gap-1">
                                <Button as-child size="sm">
                                    <Link :href="`/campaigns/${c.id}/restore`">Aktifkan</Link>
                                </Button>
                                <Button as-child variant="ghost" size="sm">
                                    <Link :href="`/campaigns/${c.id}/edit`">Edit</Link>
                                </Button>
                                <Button as-child variant="destructive" size="sm">
                                    <Link :href="`/campaigns/${c.id}`" method="delete" as="delete">Hapus</Link>
                                </Button>
                            </div>
                        </td>
                    </tr>
                    <tr v-if="props.campaigns.data.length === 0">
                        <td :colspan="isAdmin ? 7 : 6" class="px-4 py-8 text-center text-muted-foreground">Belum ada draft.</td>
                    </tr>
                </tbody>
            </table>
        </div>

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