import {
    Button,
    Dialog,
    DialogActions,
    DialogContent,
    DialogTitle,
    IconButton,
    Tooltip,
    Typography,
} from "@mui/material";
import { useState } from "react";
import useRequest from "@/lib/hooks/useRequest";
import DeleteIcon from "@mui/icons-material/Delete";
import { DELETE_AZMAYESH } from "@/core/utils/routes";

const DeleteAzmayesh = ({ rowId, mutate }) => {
    const requestServer = useRequest({ notificationSuccess: true });
    const [openDeleteDialog, setOpenDeleteDialog] = useState(false);
    const [isSubmitting, setIsSubmitting] = useState(false);

    const handleSubmit = () => {
        setIsSubmitting(true);
        requestServer(`${DELETE_AZMAYESH}/${rowId}`, "post")
            .then(() => {
                setOpenDeleteDialog(false);
                mutate();
            })
            .catch(() => {})
            .finally(() => {
                setIsSubmitting(false);
            });
    };

    return (
        <>
            <Tooltip title="حذف آزمایش" arrow placement="right">
                <IconButton
                    color="error"
                    sx={{ textTransform: "unset", alignSelf: "center" }}
                    onClick={() => {
                        setOpenDeleteDialog(true);
                    }}
                >
                    <DeleteIcon />
                </IconButton>
            </Tooltip>
            <Dialog
                maxWidth="sm"
                open={openDeleteDialog}
                PaperProps={{
                    sx: {
                        boxShadow: "rgba(60, 64, 67, 0.3) 0px 1px 16px 0px, rgba(60, 64, 67, 0.15) 0px 1px 10px 0px",
                    },
                }}
            >
                <DialogTitle>حذف آزمایش</DialogTitle>
                <DialogContent>
                    <Typography>آیا از حذف این آزمایش مطمئن هستید؟</Typography>
                </DialogContent>
                <DialogActions>
                    <Button
                        onClick={() => setOpenDeleteDialog(false)}
                        variant="outlined"
                        color="primary"
                        disabled={isSubmitting}
                        autoFocus
                    >
                        بستن
                    </Button>
                    <Button onClick={handleSubmit} variant="contained" color="error" disabled={isSubmitting}>
                        حذف
                    </Button>
                </DialogActions>
            </Dialog>
        </>
    );
};
export default DeleteAzmayesh;
