import { Head, Link } from '@inertiajs/react';
import { ArrowLeft, MapPin, Pencil, Phone, ShoppingCart, UserRound } from 'lucide-react';
import AppLayout from '@/layouts/app-layout';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';

import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
type Props = { customer: any };
const money = (v: number) => new Intl.NumberFormat('id-ID', { style: 'currency', currency: 'IDR', maximumFractionDigits: 0 }).format(v);

export default function CustomerShow({ customer }: Props) {
    return <><Head title={customer.name} /><div className="flex flex-1 flex-col gap-6 p-4 md:p-6"><div className="flex flex-col justify-between gap-3 md:flex-row md:items-center"><div><Button variant="ghost" asChild><Link href="/customers"><ArrowLeft className="mr-2 size-4" /> Customers</Link></Button><h1 className="mt-2 text-2xl font-semibold">{customer.name}</h1><p className="text-muted-foreground text-sm">{customer.code || 'Tanpa kode'} · {customer.contact_person || 'Tidak ada contact person'}</p></div><Button asChild><Link href={`/customers/${customer.id}/edit`}><Pencil className="mr-2 size-4" /> Edit</Link></Button></div>
        <div className="grid gap-4 lg:grid-cols-3"><Card className="shadow-none lg:col-span-2"><CardHeader><CardTitle>Profil Customer</CardTitle></CardHeader><CardContent className="grid gap-5 sm:grid-cols-2"><div><p className="text-muted-foreground text-xs">Status</p><Badge className="mt-1" variant={customer.status === 'active' ? 'default' : 'outline'}>{customer.status}</Badge></div><div><p className="text-muted-foreground text-xs">Kategori</p><p className="mt-1 text-sm">{customer.category?.name ?? '-'}</p></div><div><p className="text-muted-foreground text-xs">Sales</p><p className="mt-1 flex items-center gap-2 text-sm"><UserRound className="size-4" />{customer.sales?.name ?? '-'}</p></div><div><p className="text-muted-foreground text-xs">Team</p><p className="mt-1 text-sm">{customer.team?.name ?? '-'}</p></div><div><p className="text-muted-foreground text-xs">Telepon</p><p className="mt-1 flex items-center gap-2 text-sm"><Phone className="size-4" />{customer.phone ?? '-'}</p></div><div><p className="text-muted-foreground text-xs">Credit Limit</p><p className="mt-1 text-sm">{money(Number(customer.credit_limit ?? 0))}</p></div><div className="sm:col-span-2"><p className="text-muted-foreground text-xs">Alamat</p><p className="mt-1 flex gap-2 text-sm"><MapPin className="mt-0.5 size-4 shrink-0" />{customer.address ?? '-'}</p></div></CardContent></Card>
        <Card className="shadow-none"><CardHeader><CardTitle>Ringkasan</CardTitle></CardHeader><CardContent className="space-y-3"><div className="bg-muted/50 flex justify-between rounded-lg p-3 text-sm"><span>Order</span><b>{customer.orders?.length ?? 0}</b></div><div className="bg-muted/50 flex justify-between rounded-lg p-3 text-sm"><span>Invoice</span><b>{customer.invoices?.length ?? 0}</b></div><div className="bg-muted/50 flex justify-between rounded-lg p-3 text-sm"><span>Kunjungan</span><b>{customer.visits?.length ?? 0}</b></div></CardContent></Card></div>
        <Card className="shadow-none"><CardHeader><CardTitle className="flex items-center gap-2"><ShoppingCart className="size-5" /> Order Terakhir</CardTitle></CardHeader><CardContent><div className="overflow-x-auto"><Table className="w-full text-sm"><TableHeader><TableRow className="border-b text-left"><TableHead className="pb-3">Nomor</TableHead><TableHead className="pb-3">Tanggal</TableHead><TableHead className="pb-3">Status</TableHead><TableHead className="pb-3 text-right">Total</TableHead></TableRow></TableHeader><TableBody>{(customer.orders ?? []).map((o: any) => <TableRow key={o.id} className="border-b last:border-0"><TableCell className="py-3">{o.order_number || `ORD-${o.id}`}</TableCell><TableCell className="py-3">{o.order_date}</TableCell><TableCell className="py-3"><Badge variant="outline">{o.status}</Badge></TableCell><TableCell className="py-3 text-right">{money(Number(o.grand_total))}</TableCell></TableRow>)}</TableBody></Table></div></CardContent></Card>
    </div></>;
}
CustomerShow.layout = (page: React.ReactNode) => <AppLayout breadcrumbs={[{ title: 'Customers', href: '/customers' }, { title: 'Detail', href: '#' }]}>{page}</AppLayout>;
