import { Head, Link, useForm } from '@inertiajs/react';
import { ArrowLeft, Save } from 'lucide-react';
import AppLayout from '@/layouts/app-layout';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import { Textarea } from '@/components/ui/textarea';

type Option = { id: number; name: string };
type Customer = { id: number; customer_category_id: number | null; team_id: number | null; sales_id: number | null; code: string | null; name: string; address: string | null; latitude: string | number | null; longitude: string | number | null; phone: string | null; email: string | null; contact_person: string | null; type: string; credit_limit: string | number; status: string; notes: string | null };
type Props = { customer: Customer; categories: Option[]; teams: Option[]; salesUsers: Option[] };
type FormData = { customer_category_id: string; team_id: string; sales_id: string; code: string; name: string; address: string; latitude: string; longitude: string; phone: string; email: string; contact_person: string; type: string; credit_limit: string; status: string; notes: string };

export default function CustomerEdit({ customer, categories, teams, salesUsers }: Props) {
    const { data, setData, put, processing, errors } = useForm<FormData>({ customer_category_id: customer.customer_category_id ? String(customer.customer_category_id) : '', team_id: customer.team_id ? String(customer.team_id) : '', sales_id: customer.sales_id ? String(customer.sales_id) : '', code: customer.code ?? '', name: customer.name, address: customer.address ?? '', latitude: customer.latitude ? String(customer.latitude) : '', longitude: customer.longitude ? String(customer.longitude) : '', phone: customer.phone ?? '', email: customer.email ?? '', contact_person: customer.contact_person ?? '', type: customer.type, credit_limit: String(customer.credit_limit ?? 0), status: customer.status, notes: customer.notes ?? '' });
    const submit = (e: React.FormEvent) => { e.preventDefault(); put(`/customers/${customer.id}`); };
    const input = (key: keyof FormData, label: string, type = 'text') => <div className="space-y-2"><Label>{label}</Label><Input type={type} value={data[key]} onChange={e => setData(key, e.target.value)} />{errors[key] && <p className="text-destructive text-xs">{errors[key]}</p>}</div>;
    return <><Head title={`Edit ${customer.name}`} /><div className="flex flex-1 flex-col gap-6 p-4 md:p-6"><div><Button variant="ghost" asChild><Link href={`/customers/${customer.id}`}><ArrowLeft className="mr-2 size-4" /> Kembali</Link></Button><h1 className="mt-3 text-2xl font-semibold">Edit Customer</h1></div><form onSubmit={submit} className="space-y-6"><Card className="shadow-none"><CardHeader><CardTitle>Informasi Customer</CardTitle></CardHeader><CardContent className="grid gap-5 md:grid-cols-2">{input('name','Nama Customer')}{input('code','Kode Customer')}{input('contact_person','Contact Person')}{input('phone','Telepon')}{input('email','Email','email')}
        <div className="space-y-2"><Label>Tipe</Label><Select value={data.type} onValueChange={v => setData('type', v)}><SelectTrigger className="w-full"><SelectValue /></SelectTrigger><SelectContent><SelectItem value="individual">Individu</SelectItem><SelectItem value="company">Perusahaan</SelectItem></SelectContent></Select></div><div className="space-y-2"><Label>Status</Label><Select value={data.status} onValueChange={v => setData('status', v)}><SelectTrigger className="w-full"><SelectValue /></SelectTrigger><SelectContent><SelectItem value="active">Active</SelectItem><SelectItem value="inactive">Inactive</SelectItem></SelectContent></Select></div>
        <div className="space-y-2"><Label>Kategori</Label><Select value={data.customer_category_id || undefined} onValueChange={v => setData('customer_category_id', v)}><SelectTrigger className="w-full"><SelectValue placeholder="Pilih kategori" /></SelectTrigger><SelectContent>{categories.map(o => <SelectItem key={o.id} value={String(o.id)}>{o.name}</SelectItem>)}</SelectContent></Select></div><div className="space-y-2"><Label>Team</Label><Select value={data.team_id || undefined} onValueChange={v => setData('team_id', v)}><SelectTrigger className="w-full"><SelectValue placeholder="Pilih team" /></SelectTrigger><SelectContent>{teams.map(o => <SelectItem key={o.id} value={String(o.id)}>{o.name}</SelectItem>)}</SelectContent></Select></div><div className="space-y-2"><Label>Sales</Label><Select value={data.sales_id || undefined} onValueChange={v => setData('sales_id', v)}><SelectTrigger className="w-full"><SelectValue placeholder="Pilih sales" /></SelectTrigger><SelectContent>{salesUsers.map(o => <SelectItem key={o.id} value={String(o.id)}>{o.name}</SelectItem>)}</SelectContent></Select></div>
        {input('credit_limit','Credit Limit','number')}{input('latitude','Latitude','number')}{input('longitude','Longitude','number')}<div className="space-y-2 md:col-span-2"><Label>Alamat</Label><Textarea value={data.address} onChange={e => setData('address', e.target.value)} /></div><div className="space-y-2 md:col-span-2"><Label>Catatan</Label><Textarea value={data.notes} onChange={e => setData('notes', e.target.value)} /></div>
        </CardContent></Card><div className="flex justify-end"><Button disabled={processing}><Save className="mr-2 size-4" /> Simpan Perubahan</Button></div></form></div></>;
}
CustomerEdit.layout = (page: React.ReactNode) => <AppLayout breadcrumbs={[{ title: 'Customers', href: '/customers' }, { title: 'Edit', href: '/customers' }]}>{page}</AppLayout>;
