import { IKid, IUser, PageProps } from "@/types";
import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout";
import { Head, useForm } from "@inertiajs/react";
import InputLabel from "@/Components/InputLabel";
import TextInput from "@/Components/TextInput";
import InputError from "@/Components/InputError";
import { FormEventHandler } from "react";
import PrimaryButton from "@/Components/PrimaryButton";
import SelectInput from "@/Components/SelectInput";

export interface IPriceCategory {
    id: number;
    name: string;
}

export default function Edit({
    auth,
    kid,
    users,
    priceCategories,
}: PageProps<{
    kid: IKid;
    users: IUser[];
    priceCategories: IPriceCategory[];
}>) {
    const { data, setData, patch, errors, processing } = useForm({
        full_name: kid.full_name,
        active_from: kid.active_from,
        active_to: kid.active_to,
        user_id: kid.user?.id || 0,
        price_category_id: kid.price_category?.id,
    });

    const userOptions = users.map((user) => ({
        value: user.id,
        label: user.full_name,
    }));

    const pirceCategoriesOpions = priceCategories.map((priceCat) => ({
        value: priceCat.id,
        label: priceCat.name,
    }));

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

        patch(route("admin.kids.update", kid.id));
    };
    return (
        <AuthenticatedLayout user={auth.user} header={"Gyerek szerkesztése"} processing={processing}>
            <Head title="Gyerek szerkesztése" />

            <div className="p-4 sm:p-8 bg-babolygo-ocean shadow sm:rounded-lg">
                <section>
                    <form onSubmit={submit} className="mt-6 space-y-6">
                        <div>
                            <InputLabel
                                htmlFor="full_name"
                                value="Teljes név"
                            />
                            <TextInput
                                id="full_name"
                                type="text"
                                className="mt-1 block w-full"
                                value={data.full_name}
                                onChange={(e) =>
                                    setData("full_name", e.target.value)
                                }
                            />
                            <InputError
                                className="mt-2"
                                message={errors.full_name}
                            />
                        </div>

                        <div>
                            <InputLabel
                                htmlFor="user"
                                value="Szülő kiválasztása"
                            />
                            <SelectInput
                                id="user"
                                name="user_id"
                                value={data.user_id}
                                className="mt-1 block w-full"
                                onChange={(e) =>
                                    setData("user_id", Number(e.target.value))
                                }
                                options={userOptions}
                            />
                            <InputError
                                className="mt-2"
                                message={errors.user_id}
                            />
                        </div>

                        <div>
                            <InputLabel
                                htmlFor="active_from"
                                value="Aktív ettől"
                            />
                            <TextInput
                                id="active_from"
                                type="date"
                                className="mt-1 block w-full"
                                value={data.active_from}
                                onChange={(e) =>
                                    setData("active_from", e.target.value)
                                }
                            />
                            <InputError
                                className="mt-2"
                                message={errors.active_from}
                            />
                        </div>

                        <div>
                            <InputLabel
                                htmlFor="active_to"
                                value="Aktív eddig"
                            />
                            <TextInput
                                id="active_to"
                                type="date"
                                className="mt-1 block w-full"
                                value={data.active_to}
                                onChange={(e) =>
                                    setData("active_to", e.target.value)
                                }
                            />
                            <InputError
                                className="mt-2"
                                message={errors.active_to}
                            />
                        </div>

                        <div>
                            <InputLabel
                                htmlFor="price_category"
                                value="Árkategória"
                            />
                            <SelectInput
                                id="price_category"
                                name="price_category"
                                value={data.price_category_id}
                                className="mt-1 block w-full"
                                onChange={(e) =>
                                    setData(
                                        "price_category_id",
                                        Number(e.target.value)
                                    )
                                }
                                options={pirceCategoriesOpions}
                            />
                            <InputError
                                className="mt-2"
                                message={errors.price_category_id}
                            />
                        </div>

                        <div className="flex items-center justify-end mt-6">
                            <PrimaryButton
                                className="w-full sm:w-auto"
                                disabled={processing}
                            >
                                Mentés
                            </PrimaryButton>
                        </div>
                    </form>
                </section>
            </div>
        </AuthenticatedLayout>
    );
}
