import { DateTimePicker } from '@/Components/DateTimePicker';
import FormInput from '@/Components/FormInput';
import SelectInput from '@/Components/SelectInput';
import SidebarForm from '@/Components/SidebarForm';
import TextInput from '@/Components/TextInput';
import { t } from '@/lib/utils';
import { Exam, SidebarFormBodyProps } from '@/types';
import dayjs, { Dayjs } from 'dayjs';
import React from 'react';

interface ExamEditFormProps {
    exam: Exam;
    children: React.ReactNode;
    availableTypes: string[];
}

type ExamFormData = {
    type: string;
    value: number;
    done_at: Dayjs;
};

export default function ExamEditForm({
    exam,
    children: trigger,
    availableTypes,
}: ExamEditFormProps) {
    const initialData = {
        type: exam.type,
        value: exam.value,
        done_at: dayjs(exam.done_at),
    };

    const availableTypesOptions = availableTypes.map((type) => ({
        value: type,
        label: t(type),
    }));

    const Body = (bodyProps: Partial<SidebarFormBodyProps<ExamFormData>>) => {
        const { data, setData, errors } =
            bodyProps as SidebarFormBodyProps<ExamFormData>;
        return (
            <>
                <FormInput
                    label="Utente"
                    id="user_id"
                    className="mt-8 block w-full"
                >
                    <TextInput
                        value={`${exam.user.name} ${exam.user.surname}`}
                        disabled
                    />
                </FormInput>
                <FormInput
                    label="Tipo"
                    id="type"
                    className="my-4 block w-full"
                    error={errors.type}
                >
                    <SelectInput
                        options={availableTypesOptions}
                        value={data.type}
                        onChange={(value) => setData('type', value)}
                    />
                </FormInput>
                <FormInput
                    label="Data"
                    id="value"
                    className="my-4 block w-full"
                    error={errors.value}
                >
                    <DateTimePicker
                        date={data.done_at.toDate()}
                        setDate={(date) => setData('done_at', dayjs(date))}
                    />
                </FormInput>
                <FormInput
                    label="Valore"
                    id="value"
                    className="my-4 block w-full"
                    error={errors.value}
                >
                    <TextInput
                        type="number"
                        value={data.value}
                        onChange={(e) =>
                            setData('value', parseInt(e.target.value))
                        }
                    />
                </FormInput>
            </>
        );
    };

    return (
        <SidebarForm
            title="Modifica esame"
            trigger={trigger}
            submitMethod={'put'}
            submitRoute={route('admin-exams.update', { exam: exam.id })}
            initialData={initialData}
        >
            <Body />
        </SidebarForm>
    );
}
