import { useState } from "react";
import EditB from "@/components/icons/EditB";
import { styleDefault } from "@/mui";
import { Box, Button, Modal } from "@mui/material";
import CloseModalD from "@/components/icons/CloseModalD";

function ModalEditItem({
  data,
  resetData,
  name,
  title,
  id,
  changeData,
  children,
}) {
  const [open, setOpen] = useState(false);

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

  return (
    <>
      <Button onClick={handleOpen} className="!min-w-fit !p-1" variant="text">
        <EditB />
      </Button>
      <Modal open={open} onClose={handleClose}>
        <Box
          sx={styleDefault}
          className="!w-fit dark:!bg-[#222433] sm-modal dark:!shadow-[0px_4px_25px_10px_#1F1D2B] !min-w-[328px] md:!w-fit md:!min-w-[552px] !py-[20px] !px-[24px]"
        >
          <div className="flex items-center justify-between w-full">
            <p className="text-[#3B3B3B] text-[14px] dark:text-[#D7D8ED] sm:text-[16px] md:text-[18px] lg:text-[20px] font-medium">
              تغییر {title}
            </p>
            <Button onClick={handleClose} className="!p-1" variant="text">
              <CloseModalD />
            </Button>
          </div>
          {children}
          <div className="flex items-center justify-end gap-[16px] mt-[12px] sm:mt-[16px] md:mt-[20px] lg:mt-[24px]">
            <Button
              variant="outlined"
              onClick={() => {
                resetData();
                handleClose();
              }}
              className="!py-[4px] dark:!bg-[#C7CEF4] dark:!text-[#5559CE] md:!py-[6px] !min-h-[32px] md:!min-h-[39px] lg:!min-h-[46px] lg:!py-[8px] !px-[8px] md:!px-[12px] lg:!px-[16px] !rounded-[4px] !border-[#828DE0] !text-[#828DE0] !text-[12px] md:!text-[14px] lg:!text-[16px] !font-medium"
            >
              لغو
            </Button>
            <Button
              variant="contained"
              onClick={() => {
                // const changedKey = {
                //   name: data.name,
                //   relation: data.relation,
                //   contact: {
                //     phone: data.phone,
                //     address: data.address,
                //   },
                // };
                changeData("update", name, id, data);
                handleClose();
              }}
              className="!py-[4px] md:!py-[6px] !min-h-[32px] md:!min-h-[39px] lg:!min-h-[46px] lg:!py-[8px] !px-[10px] md:!px-[14px] lg:!px-[18px] !rounded-[4px] !text-[#EFEFEF] !text-[12px] md:!text-[14px] lg:!text-[16px] !font-medium"
            >
              ذخیره
            </Button>
          </div>
        </Box>
      </Modal>
    </>
  );
}

export default ModalEditItem;
