import AppLayout from '@/layouts/app-layout';
import { type BreadcrumbItem } from '@/types';
import { Head, Link, useForm } from '@inertiajs/react';
import {
    Card,
    CardContent,
    CardFooter,
    CardHeader,
    CardTitle,
} from "@/components/ui/card"
import { Input } from "@/components/ui/input"
import InputError from '@/components/input-error';
import { Button, buttonVariants } from '@/components/ui/button';
import { Label } from '@/components/ui/label';
import { FormEventHandler, useRef } from 'react';
// import ImageUpload from '@/components/ImageUpload';

type CreateTaskForm = {
    name: string;
};

const breadcrumbs: BreadcrumbItem[] = [
    {
        title: 'Residency Program Create',
        href: '/residency_program/create',
    },
];

export default function Create() {
    const taskName = useRef<HTMLInputElement>(null);
    const taskemail = useRef<HTMLInputElement>(null);

    const { data, setData, errors, post, reset, processing } = useForm<CreateTaskForm>({
        name: '',
    });

    const createTask: FormEventHandler = (e) => {
        e.preventDefault();
        post(route('residency_program.store'), {
            forceFormData: true,
            preserveScroll: true,
            onSuccess: () => {
                reset();
            },
            onError: (errors) => {
                if (errors.name) {
                    reset('name');
                    reset('email');
                    taskName.current?.focus();
                }
            }
        });
    };

    return (
        <AppLayout breadcrumbs={breadcrumbs}>
            <Head title="Residency Program Create" />
            <div className="m-5">
                <div className="flex">
                    <Link className={(buttonVariants({ variant: 'default', className: 'mb-2 ml-auto' }))} href={route('residency_program.index')}>
                        Residency Program List
                    </Link>
                </div>

                <Card>
                    <CardHeader>
                        <CardTitle>Create Residency Program</CardTitle>
                    </CardHeader>
                    <form onSubmit={createTask} encType="multipart/form-data">
                        <CardContent>
                            <div className="grid w-full items-center gap-4">
                                <div className="flex flex-col space-y-1.5">
                                    <Label htmlFor="name">Name</Label>
                                    <Input
                                        id="name"
                                        ref={taskName}
                                        value={data.name}
                                        onChange={(e) => setData('name', e.target.value)}
                                        placeholder="Name of residency program"
                                        disabled={processing}
                                    />
                                    <InputError message={errors.name} />
                                </div>
                                <div className="flex flex-col space-y-1.5">
                                    <Label htmlFor="email">Email</Label>
                                    <Input
                                        id="email"
                                        ref={taskemail}
                                        value={data.email}
                                        onChange={(e) => setData('email', e.target.value)}
                                        placeholder="email of residency program"
                                        disabled={processing}
                                    />
                                    <InputError message={errors.email} />
                                </div>
                            </div>
                        </CardContent>
                        <CardFooter className="flex justify-between mt-5">
                            <div className="space-x-2">
                                <Link className={(buttonVariants({ variant: 'default', className: 'ml-auto' }))} href={route('residency_program.index')}>
                                    Cancel
                                </Link>
                                <Button type="button" variant="outline" onClick={() => reset()}>
                                    Reset
                                </Button>
                            </div>
                            <Button type="submit" disabled={processing}>
                                Create
                            </Button>
                        </CardFooter>
                    </form>
                </Card>
            </div>
        </AppLayout>
    );
}
