import React,{useRef} from 'react';
import { useForm, SubmitHandler } from "react-hook-form";


interface IFormInputs {
  firstName: string
  lastName: string
  coach_telefon_number: string
}

const onSubmit: SubmitHandler<IFormInputs> = data => console.log(data);

const alerError = (props) => {
  return <p className="mt-2 text-sm text-red-600 dark:text-red-500 font-medium">{props}</p>
}

export const RegistrationForm = (props): JSX.Element => {
  const form = useRef(null);
  const { register, handleSubmit, formState: { errors } } = useForm();
  const submit = e => {
    const data =[];
    e.preventDefault();
    const form = e.target;
    // eslint-disable-next-line no-console
    for (let i = 0; i < e.target.length-1; i++) {
      data.push(form[i].value);
    }
    JSON.stringify(data);
    //console.log(data);
    fetch('/api/registration', { method: 'POST', body: data })
      .then(props.updateData(2), e.target.reset())
  }
  return (
    <>
      <div className="mt-10 sm:mt-0">
        <div className="md:grid md:grid-cols-3 md:gap-6">
          <div className="md:col-span-1">
            <div className="px-4 sm:px-0">
              <h3 className="text-lg font-medium leading-6">Регистрация команды</h3>
              <p className="mt-1 text-sm">Введите актуальные данные команды</p>
              <p className="mt-1 text-sm">От каждого учебного заведения может быть зарегистрированно неограниченое количеставо команд</p>
            </div>
          </div>
          <div className="mt-5 md:mt-0 md:col-span-2">
            <form ref={form} onSubmit={handleSubmit(onSubmit)}>
              <div className="shadow overflow-hidden sm:rounded-md">
                <div className="px-4 py-5 bg-white sm:p-6">
                  <div className="grid grid-cols-6 gap-6">
                    <div className="col-span-6 sm:col-span-3">
                      <label htmlFor="first-name" className="block text-sm font-medium text-gray-700">
                        ФИО тренера команды
                      </label>
                      <input
                        {...register("name_team_coach",{ required: true, maxLength: 80 })}
                        name="name_team_coach"
                        placeholder="Иванов Иван Иванович"
                        className="mt-1 block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
                      />
                      {errors.name_team_coach && alerError('Введите ФИО тренера команды') }
                    </div>

                    <div className="col-span-6 sm:col-span-3">
                      <label htmlFor="last-name" className="block text-sm font-medium text-gray-700">
                        Номер теленона тренера
                      </label>
                      <input
                        {...register("coach_telefon_number",{ required: true, valueAsNumber: true })}
                        name="coach_telefon_number"
                        placeholder="+79181234567"
                        className="mt-1 block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
                      />
                      {errors.coach_telefon_number && alerError('Введите номер телефона')}
                    </div>

                    <div className="col-span-6 sm:col-span-3">
                      <label htmlFor="email-address" className="block text-sm font-medium text-gray-700">
                        Email тренера
                      </label>
                      <input
                        {...register("trainer_mail",{ required: true, maxLength: 80, pattern: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ })}
                        placeholder="you@example.com"
                        className="mt-1 block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
                      />
                      {errors.trainer_mail && alerError('Введите коректный Email адресс')
                      }
                    </div>

                    <div className="col-span-6 sm:col-span-3">
                      <label htmlFor="email-address" className="block text-sm font-medium text-gray-700">
                        Город команда
                      </label>
                      <input
                       {...register("city_team",{ required: true, maxLength: 80 })}
                        name="city_team"
                        placeholder="Краснодар"
                        className="mt-1 block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
                      />
                      { errors.city_team && alerError('Введите город команды') }
                    </div>

                    <div className="col-span-3">
                      <label htmlFor="street-address" className="block text-sm font-medium text-gray-700">
                        Учебное заведение команды
                      </label>
                      <input
                        {...register("training_institution_team",{ required: true, maxLength: 150 })}
                        name="training_institution_team"
                        placeholder="МАОУ СОШ 103"
                        className="mt-1 block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
                      />
                      {errors.training_institution_team && alerError('Введите название учебного заведения') }
                    </div>

                    <div className="col-span-3">
                      <label htmlFor="region" className="block text-sm font-medium text-gray-700">
                        Название команды
                      </label>
                      <input
                        {...register("team_name",{ required: true, maxLength: 150 })}
                        name="team_name"
                        placeholder="Фиксики"
                        className="mt-1 block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
                      />
                      {errors.team_name && alerError('Введите название команды') }
                    </div>

                    <div className="col-span-6 sm:col-span-3">
                      <label htmlFor="city" className="block text-sm font-medium text-gray-700">
                        ФИО первого участника
                      </label>
                      <input
                        {...register("name_first_participant",{ required: true, maxLength: 150 })}
                        name="name_first_participant"
                        placeholder="Иванов Иван Иванович / нет"
                        className="mt-1 block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
                      />
                      {errors.name_first_participant && alerError('Введите ФИО участника') }
                    </div>

                    <div className="col-span-6 sm:col-span-3">
                      <label htmlFor="country" className="block text-sm font-medium text-gray-700">
                        Класс участника
                      </label>
                      <select
                        id="country"
                        name="first_partial_class"
                        autoComplete="country-name"
                        className="mt-1 block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
                      >
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option>4</option>
                        <option>5</option>
                        <option>6</option>
                        <option>7</option>
                        <option>8</option>
                        <option>9</option>
                        <option>10</option>
                        <option>11</option>
                      </select>
                    </div>

                    <div className="col-span-6 sm:col-span-3">
                      <label htmlFor="city" className="block text-sm font-medium text-gray-700">
                        ФИО второго участника
                      </label>
                      <input
                        {...register("name_second_participant",{ maxLength: 150 })}
                        name="name_second_participant"
                        placeholder="Иванов Петр Иванович / нет"
                        className="mt-1 block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
                      />
                    </div>

                    <div className="col-span-6 sm:col-span-3">
                      <label htmlFor="country" className="block text-sm font-medium text-gray-700">
                        Класс участника
                      </label>
                      <select
                        id="country"
                        name="second_class"
                        autoComplete="country-name"
                        className="mt-1 block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
                      >
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option>4</option>
                        <option>5</option>
                        <option>6</option>
                        <option>7</option>
                        <option>8</option>
                        <option>9</option>
                        <option>10</option>
                        <option>11</option>
                      </select>
                    </div>

                    <div className="col-span-6 sm:col-span-3">
                      <label htmlFor="city" className="block text-sm font-medium text-gray-700">
                        ФИО третьего участника
                      </label>
                      <input
                        {...register("name_third_party",{ maxLength: 150 })}
                        name="name_third_party"
                        id="city"
                        placeholder="Иванов Дмитрий Иванович / нет"
                        className="mt-1 block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
                      />
                    </div>

                    <div className="col-span-6 sm:col-span-3">
                      <label htmlFor="country" className="block text-sm font-medium text-gray-700">
                        Класс участника
                      </label>
                      <select
                        id="country"
                        name="third_part_class"
                        autoComplete="country-name"
                        className="mt-1 block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
                      >
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option>4</option>
                        <option>5</option>
                        <option>6</option>
                        <option>7</option>
                        <option>8</option>
                        <option>9</option>
                        <option>10</option>
                        <option>11</option>
                      </select>
                    </div>

                  </div>
                </div>
                <div className="px-4 py-3 bg-gray-50 text-right sm:px-6">
                  <button
                    type="submit"
                    className="inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
                  >
                    Зарегистрировать команду
                  </button>
                </div>
              </div>
            </form>
          </div>
        </div>
      </div>
</>
  );
};

export default RegistrationForm;
