import {
  Box,
  Button,
  FormControl,
  FormLabel,
  Input,
  Modal,
  ModalBody,
  ModalCloseButton,
  ModalContent,
  ModalFooter,
  ModalHeader,
  ModalOverlay,
  Text,
  useDisclosure,
} from "@chakra-ui/react";
import React from "react";
import * as yup from "yup";
import { yupResolver } from "@hookform/resolvers/yup";
import { useForm } from "react-hook-form";

export const conformModalSchema = yup.object().shape({
  fees: yup.string().required("File name is required"),
  totalAmount: yup.string().required("File name is required"),
});

const ConfirmModal = ({ isOpen, onClose, firstField }) => {
  const {
    register,
    handleSubmit,
    formState: { errors },
  } = useForm({
    resolver: yupResolver(conformModalSchema),
  });

  const onSubmit = (data) => {
    setFile(data.document[0]);

    const newDocument = {
      ...data,
      document: data.document[0].name, // Store the document name
      status: true,
      id: uuidv4(),
      createdAt: new Date().toISOString(),
      Type: getFileIcon(file.type),
    };

    setCreate((prevCreate) => [...prevCreate, newDocument]);
    onClose();
  };

  const handleFileChange = (event) => {
    const selectedFile = event.target.files[0];
    setFile(selectedFile);
  };  

  return (
    <Modal isOpen={isOpen} onClose={onClose} initialFocusRef={firstField}>
      <ModalOverlay />
      <ModalContent pb={4}>
        <ModalHeader fontSize={"md"}>Confirm</ModalHeader>
        <ModalCloseButton />
        <Box as="form" onSubmit={handleSubmit(onSubmit)}>
          <ModalBody>
            <FormControl mb={4}>
              <FormLabel fontSize="sm">Deposit Amount</FormLabel>
              <Input
              focusBorderColor='green.400'
                name="fileName"
                {...register("fileName")}
                fontSize="sm"
                type="text"
                size="sm"
                placeholder={"$100,000"}
                readOnly
              />
            </FormControl>
            <FormControl mb={4}>
              <FormLabel fontSize="sm">Fees</FormLabel>
              <Input 
              focusBorderColor='green.400'
                name="fileName"
                {...register("fileName")}
                fontSize="sm"
                type="text"
                size="sm"
                placeholder={"$100,000"}
              />
              {errors.fees && (
                <Text fontSize="xs" color="red">
                  {errors.fees.message}
                </Text>
              )}
            </FormControl>
            <FormControl mb={4}>
              <FormLabel fontSize="sm">Total Amount</FormLabel>
              <Input 
              focusBorderColor='green.400'
                name="fileName"
                {...register("fileName")}
                fontSize="sm"
                type="text"
                size="sm"
                placeholder={"$100,000"}
              />
              {errors.totalAmount && (
                <Text fontSize="xs" color="red">
                  {errors.totalAmount.message}
                </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'}>
              Confirm
            </Button>
          </ModalFooter>
        </Box>
      </ModalContent>
    </Modal>
  );
};

export default ConfirmModal;
