import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout';
import { Head, Link, router, 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 SuppliersIndex({ suppliers }: any) {
    const { data, setData, get } = useForm({ search: new URLSearchParams(window.location.search).get('search') ?? '' });
    const search = (e: React.FormEvent) => { e.preventDefault(); get('/suppliers'); };
    return (
        <AuthenticatedLayout header="Suppliers">
            <Head title="Suppliers" />
            <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 suppliers..." 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="/suppliers/create"><Plus className="w-4 h-4" />New Supplier</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">POs</th><th className="text-left px-4 py-3 font-medium">Status</th><th className="px-4 py-3"></th></tr></thead>
                        <tbody className="divide-y divide-border">
                            {suppliers.data.map((s: any) => (
                                <tr key={s.id} className="hover:bg-muted/30">
                                    <td className="px-4 py-3 font-medium"><Link href={`/suppliers/${s.id}`} className="hover:underline">{s.name}</Link></td>
                                    <td className="px-4 py-3 text-muted-foreground hidden md:table-cell">{s.contact_name ?? '—'}</td>
                                    <td className="px-4 py-3 text-muted-foreground hidden lg:table-cell">{s.email ?? '—'}</td>
                                    <td className="px-4 py-3">{s.purchase_orders_count}</td>
                                    <td className="px-4 py-3"><Badge status={s.status} /></td>
                                    <td className="px-4 py-3"><Link href={`/suppliers/${s.id}/edit`} className="text-xs text-muted-foreground hover:text-primary">Edit</Link></td>
                                </tr>
                            ))}
                        </tbody>
                    </table>
                    {suppliers.data.length === 0 && <p className="text-center py-12 text-muted-foreground">No suppliers found.</p>}
                </div>
            </div>
        </AuthenticatedLayout>
    );
}
