import {
  Box,
  Button,
  FormControl,
  FormLabel,
  Modal,
  ModalBody,
  ModalCloseButton,
  ModalContent,
  ModalFooter,
  ModalHeader,
  ModalOverlay,
  Text,
  Textarea,
} from "@chakra-ui/react";
import React, { useEffect } from "react";
import PropTypes from "prop-types";

import * as yup from "yup";
import { yupResolver } from "@hookform/resolvers/yup";
import { useForm } from "react-hook-form";

export const conformModalSchema = yup.object().shape({
  comment: yup
    .string()
    .min(2, "Minimum length should be 2 characters.")
    .max(150, "Maximum length should be 150 characters.")
    // .matches(/^[^\d]+$/, "Sponsor Name cannot contain numbers")
    .required("Comment is required"),
}); 

const RejectReversalPopups = ({
  isOpen,
  onClose,
  handelApproved,
  isLoading,
}) => {
  const {
    watch,
    register,
    reset,
    handleSubmit,
    formState: { errors },
  } = useForm({
    resolver: yupResolver(conformModalSchema),
    mode: "all",
  });

   // Reset the form when the modal closes
   useEffect(() => {
    if (!isOpen) {
      reset(); // Clear the form state
    }
  }, [isOpen, reset]);

  return (
    <Modal isOpen={isOpen} onClose={onClose}>
      <ModalOverlay />
      <ModalContent pb={4}>
        <ModalHeader fontSize={"md"}>Reject</ModalHeader>
        <ModalCloseButton />
        <Box
          as="form"
          onSubmit={handleSubmit((data) => {
            handelApproved(data);
            reset();
            onClose();
          })}
        >
          <ModalBody>
            <FormControl mb={4} isRequired>
              <FormLabel fontSize="sm">Comment</FormLabel>
              <Textarea
                rows={6}
                focusBorderColor="green.400"
                name="comment"
                {...register("comment")}
                fontSize="sm"
                type="textarea"
                size="md"
                placeholder={"Enter your comment...."}
                rounded={"md"}
                resize={"none"}
                mb={2}
              />
              {errors.comment ? (
                <Text fontSize="xs" color="red">
                  {errors.comment.message}
                </Text>
              ) : (
                <Text fontSize="xs" color="gray.500">
                  Maximum length should be 150 characters. You have entered{" "}
                  {watch()?.comment?.length || 0} characters.
                </Text>
              )}
            </FormControl>
          </ModalBody>
          <ModalFooter>
            <Button
              colorScheme="gray"
              mr={3}
              onClick={onClose}
              size={"sm"}
              rounded={"sm"}
            >
              Cancel
            </Button>
            <Button
              colorScheme="forestGreen"
              variant="solid"
              size={"sm"}
              rounded={"sm"}
              type="submit"
              fontWeight={400}
            >
              Send
            </Button>
          </ModalFooter>
        </Box>
      </ModalContent>
    </Modal>
  );
};

RejectReversalPopups.propTypes = {
  isOpen: PropTypes.bool.isRequired,
  onClose: PropTypes.func.isRequired,
  handelApproved: PropTypes.func.isRequired,
  isLoading: PropTypes.func.isRequired,
};

export default RejectReversalPopups;
