import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout';
import { Head, Link, useForm } from '@inertiajs/react';
import { Badge } from '@/Components/ui/badge';
import { LinkButton } from '@/Components/ui/button';
import { Plus, Search } from 'lucide-react';

export default function CustomersIndex({ customers }: any) {
    const { data, setData, get } = useForm({ search: new URLSearchParams(window.location.search).get('search')??'' });
    const search = (e: React.FormEvent) => { e.preventDefault(); get('/customers'); };
    return (
        <AuthenticatedLayout header="Customers">
            <Head title="Customers" />
            <div className="space-y-4">
                <div className="flex gap-3 items-center">
                    <form onSubmit={search} className="flex gap-2 flex-1"><input value={data.search} onChange={e=>setData('search',e.target.value)} placeholder="Search customers..." className="border rounded-md px-3 py-2 text-sm flex-1 focus:outline-none focus:ring-2 focus:ring-primary/30" /><button type="submit" className="p-2 border rounded-md hover:bg-muted"><Search className="w-4 h-4" /></button></form>
                    <LinkButton href="/customers/create"><Plus className="w-4 h-4"/>New Customer</LinkButton>
                </div>
                <div className="bg-white rounded-lg border border-border shadow-sm overflow-hidden">
                    <table className="w-full text-sm">
                        <thead className="bg-muted/50 border-b"><tr><th className="text-left px-4 py-3 font-medium">Name</th><th className="text-left px-4 py-3 font-medium hidden md:table-cell">Contact</th><th className="text-left px-4 py-3 font-medium hidden lg:table-cell">Email</th><th className="text-left px-4 py-3 font-medium">DOs</th><th className="text-left px-4 py-3 font-medium">Status</th></tr></thead>
                        <tbody className="divide-y">{customers.data.map((c:any)=><tr key={c.id} className="hover:bg-muted/30"><td className="px-4 py-3 font-medium"><Link href={`/customers/${c.id}`} className="hover:underline">{c.name}</Link></td><td className="px-4 py-3 text-muted-foreground hidden md:table-cell">{c.contact_name??'—'}</td><td className="px-4 py-3 text-muted-foreground hidden lg:table-cell">{c.email??'—'}</td><td className="px-4 py-3">{c.delivery_orders_count}</td><td className="px-4 py-3"><Badge status={c.status}/></td></tr>)}</tbody>
                    </table>
                    {customers.data.length===0&&<p className="text-center py-12 text-muted-foreground">No customers found.</p>}
                </div>
            </div>
        </AuthenticatedLayout>
    );
}
