import { useState } from "react";
import Form from "./Form";
import { styleDefault } from "@/mui";
import { Box, Button, Modal } from "@mui/material";

// Icons
import PlusB from "@/components/icons/PlusB";
import CloseModalD from "@/components/icons/CloseModalD";

function ModalAddCard() {
  const [open, setOpen] = useState(false);
  const [data, setData] = useState({
    card: { value: "", is_edit: true },
    shaba: { value: "", is_edit: true },
    bank: { value: "", is_edit: true },
  });

  const handleOpen = () => setOpen(true);
  const handleClose = () => setOpen(false);

  const changeData = (value, name) =>
    setData({ ...data, [name]: { ...data[name], value } });

  const updateNum = (value, type, name) => {
    const parseValue = +value;

    changeData(parseValue, name);
  };

  const isValidData = data.card && data.shaba && data.value && data.value.value;

  return (
    <>
      {/* Button Modal */}
      <Button
        className="!bg-[#5559CE] !w-fit !shadow-none !rounded-[4px] !gap-[4px] !text-[#EFEFEF] !text-[14px] md:!text-[16px] !font-medium"
        onClick={handleOpen}
        variant="contained"
      >
        <PlusB />
        اضافه کردن کارت
      </Button>
      {/* Content Modal */}
      <Modal open={open} onClose={handleClose}>
        <Box
          sx={styleDefault}
          className="!w-full dark:!bg-[#222433] dark:!shadow-none !h-full md:!h-fit sm:!w-fit !rounded-none sm:!rounded-[8px] sm:!min-w-[524px] !py-[20px] !pt-[24px] md:!pt-[22px] lg:!pt-[20px] !pb-[36px]"
        >
          <div className="flex items-center justify-between w-full">
            <p className="text-[#3B3B3B] dark:text-[#D7D8ED] text-[16px] md:text-[18px] lg:text-[20px] font-bold">
              اضافه کردن شماره کارت
            </p>
            <Button onClick={handleClose} className="!p-1" variant="text">
              <CloseModalD />
            </Button>
          </div>
          <Form data={data} changeData={changeData} updateNum={updateNum} />
          <div className="flex items-center justify-center gap-[12px] md:gap-[14px] lg:gap-[16px] mt-[32px] md:mt-[36px] lg:mt-[40px]">
            <Button
              variant="outlined"
              onClick={handleClose}
              className="!min-w-[134px] !rounded-[4px] !h-[40px] md:!h-[44px] lg:!h-[48px] !border-[#828DE0] dark:!bg-[#C7CEF4] 
              dark:!border-transparent dark:!text-[#5559CE] !text-[#828DE0] !text-[16px] !font-medium"
            >
              انصراف
            </Button>
            <Button
              variant="contained"
              onClick={handleClose}
              disabled={!isValidData}
              className="!min-w-[134px] !rounded-[4px] !h-[40px] md:!h-[44px] lg:!h-[48px] !text-[#EFEFEF] !text-[16px] !font-medium"
            >
              ذخیره
            </Button>
          </div>
        </Box>
      </Modal>
    </>
  );
}

export default ModalAddCard;
