import { Col, Row } from "reactstrap";
import ValidationField from "../../../../../../Components/ValidationField/ValidationField";
import { useFormikContext } from "formik";
import { useModalState } from "../../../../../../zustand/Modal";
import { useEffect } from "react";
import { useParams } from "react-router-dom";
import { notes } from "../../../../../../types/App";
import { useGetAllRegistrationRecord } from "../../../../../../api/registrationRecord";
import registration_recordSelect from "../../../../../../utils/registration_recordSelect";
import useSearchQuery from "../../../../../../api/utils/useSearchQuery";

const Form = () => {
  const formik = useFormikContext();
  const { isOpen } = useModalState((state) => state);
  const [searchQuery] = useSearchQuery("student_name");

  useEffect(() => {
    if (isOpen === "") {
      formik.setErrors({});
      // formik.resetForm();
    }
    if (isOpen === "isSuccess") {
      formik.setErrors({});
      formik.resetForm();
    }
  }, [isOpen]);
  const { edu_class_id } = useParams();
  const { data: registration_record,isLoading } = useGetAllRegistrationRecord({
    edu_class_id: edu_class_id,
    status: "active",
    name: searchQuery,
  });
  // console.log(registration_record,"registration_record");

  const recordSelect = registration_recordSelect(registration_record?.data);

  return (
    <Row className="w-100">
      <Col>
        <ValidationField placeholder="content" label="content" name="content" />

        <ValidationField
          placeholder="date"
          label="date"
          type="Date"
          name="date"
        />
      </Col>

      <Col>
        <ValidationField
          placeholder="type"
          label="type"
          type="Select"
          option={notes}
          name="type"
        />

        <ValidationField
          placeholder="student_name"
          label="student_name"
          type="Search"
          option={recordSelect}
          name="registration_record_id"
          searchBy="student_name"
          isLoading={isLoading}
        />

        <ValidationField
          placeholder="attachments"
          label="attachments"
          type="MaltyFile"
          name="attachments"
        />
      </Col>
    </Row>
  );
};

export default Form;
