import { Head, Link, useForm } from '@inertiajs/react';
import InputError from '@/components/input-error';
import { PageHeader } from '@/components/page-header';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Checkbox } from '@/components/ui/checkbox';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Spinner } from '@/components/ui/spinner';
import { index, store, update } from '@/routes/offices';

type Office = {
    id: string;
    name: string;
    address: string;
    city: string | null;
    state: string | null;
    phone: string | null;
    email: string | null;
    is_head_office: boolean;
    is_active: boolean;
};

export default function OfficeForm({ office }: { office: Office | null }) {
    const editing = office !== null;

    const form = useForm({
        name: office?.name ?? '',
        address: office?.address ?? '',
        city: office?.city ?? '',
        state: office?.state ?? '',
        phone: office?.phone ?? '',
        email: office?.email ?? '',
        is_head_office: office?.is_head_office ?? false,
        is_active: office?.is_active ?? true,
    });

    const submit = (e: React.FormEvent) => {
        e.preventDefault();

        if (editing) {
            form.put(update(office.id).url, { preserveScroll: true });
        } else {
            form.post(store().url, { preserveScroll: true });
        }
    };

    const field = (name: keyof typeof form.data) => ({
        value: String(form.data[name] ?? ''),
        onChange: (e: React.ChangeEvent<HTMLInputElement>) =>
            form.setData(name, e.target.value),
    });

    return (
        <>
            <Head title={editing ? 'Edit office' : 'New office'} />
            <form onSubmit={submit} className="flex flex-col gap-6 p-4">
                <PageHeader
                    title={editing ? 'Edit office' : 'New office'}
                    action={
                        <div className="flex gap-2">
                            <Button asChild variant="outline" type="button">
                                <Link href={index()}>Cancel</Link>
                            </Button>
                            <Button type="submit" disabled={form.processing}>
                                {form.processing && <Spinner />}
                                Save
                            </Button>
                        </div>
                    }
                />

                <Card>
                    <CardHeader>
                        <CardTitle>Details</CardTitle>
                    </CardHeader>
                    <CardContent className="grid gap-4 sm:grid-cols-2">
                        <div className="grid gap-2 sm:col-span-2">
                            <Label htmlFor="name">Name</Label>
                            <Input id="name" {...field('name')} required />
                            <InputError message={form.errors.name} />
                        </div>
                        <div className="grid gap-2 sm:col-span-2">
                            <Label htmlFor="address">Address</Label>
                            <Input
                                id="address"
                                {...field('address')}
                                required
                            />
                            <InputError message={form.errors.address} />
                        </div>
                        <div className="grid gap-2">
                            <Label htmlFor="city">City (optional)</Label>
                            <Input id="city" {...field('city')} />
                            <InputError message={form.errors.city} />
                        </div>
                        <div className="grid gap-2">
                            <Label htmlFor="state">State (optional)</Label>
                            <Input id="state" {...field('state')} />
                            <InputError message={form.errors.state} />
                        </div>
                        <div className="grid gap-2">
                            <Label htmlFor="phone">Phone (optional)</Label>
                            <Input id="phone" {...field('phone')} />
                            <InputError message={form.errors.phone} />
                        </div>
                        <div className="grid gap-2">
                            <Label htmlFor="email">Email (optional)</Label>
                            <Input
                                id="email"
                                type="email"
                                {...field('email')}
                            />
                            <InputError message={form.errors.email} />
                        </div>
                    </CardContent>
                </Card>

                <Card>
                    <CardHeader>
                        <CardTitle>Options</CardTitle>
                    </CardHeader>
                    <CardContent className="flex flex-col gap-3">
                        <label className="flex items-center gap-3">
                            <Checkbox
                                checked={form.data.is_head_office}
                                onCheckedChange={(v) =>
                                    form.setData('is_head_office', v === true)
                                }
                            />
                            <span className="text-sm">Head office</span>
                        </label>
                        <label className="flex items-center gap-3">
                            <Checkbox
                                checked={form.data.is_active}
                                onCheckedChange={(v) =>
                                    form.setData('is_active', v === true)
                                }
                            />
                            <span className="text-sm">Active</span>
                        </label>
                    </CardContent>
                </Card>
            </form>
        </>
    );
}
