'use client';
import { useActionState, useEffect, useRef } from 'react';
import { createStudent, type FormState } from './actions';
import GoalFields from '@/components/GoalFields';
import FormMessage from '@/components/FormMessage';

export default function NewStudentForm() {
  const [state, action, pending] = useActionState<FormState, FormData>(createStudent, null);
  const ref = useRef<HTMLFormElement>(null);
  useEffect(() => {
    if (state?.ok) ref.current?.reset();
  }, [state]);
  return (
    <form ref={ref} action={action} className="stack">
      <div className="fields">
        <label>
          Ad soyad
          <input name="full_name" required autoComplete="off" />
        </label>
        <label>
          E-posta
          <input type="email" name="email" required autoComplete="off" />
        </label>
      </div>
      <GoalFields />
      <FormMessage state={state} />
      <div>
        <button className="btn" disabled={pending}>
          {pending ? 'Ekleniyor…' : 'Öğrenciyi ekle ve davet gönder'}
        </button>
      </div>
    </form>
  );
}
