import {useTranslations} from "next-intl";
import {useState} from "react";
import {
    Button,
    Dialog,
    DialogActions,
    DialogContent,
    DialogTitle,
    IconButton,
    Tooltip,
    Typography
} from "@mui/material";
import RefreshIcon from '@mui/icons-material/Refresh';
import useRequest from "@/lib/app/hooks/useRequest";
import useNotification from "@/lib/app/hooks/useNotification";
import {RESEND_BANK_STATEMENT} from "@/core/data/apiRoutes";

const UpdateForm = ({rowId, mutate}) => {
    const t = useTranslations();
    const [openConfirmDialog, setOpenConfirmDialog] = useState(false);
    const [isSubmitting, setIsSubmitting] = useState(false)
    const requestServer = useRequest({auth: true})
    const {update_notification} = useNotification()
    const handleSubmit = () => {
        setIsSubmitting(true)
        requestServer(`${RESEND_BANK_STATEMENT}/${rowId}`, 'post').then((response) => {
            setOpenConfirmDialog(false)
            mutate()
            update_notification()
        }).catch(() => {
        }).finally(() => {
            setIsSubmitting(false)
        });
    }

    return (
        <>
            <Tooltip title={t("UpdateDialog.resend")}>
                <IconButton
                    color="primary"
                    onClick={() => {
                        setOpenConfirmDialog(true)
                    }}
                >
                    <RefreshIcon/>
                </IconButton>
            </Tooltip>
            <Dialog maxWidth="sm" open={openConfirmDialog}
                    PaperProps={{sx: {boxShadow: 'rgba(60, 64, 67, 0.3) 0px 1px 16px 0px, rgba(60, 64, 67, 0.15) 0px 1px 10px 0px'}}}>
                <DialogTitle>{t("UpdateDialog.resend")}</DialogTitle>
                <DialogContent>
                    <Typography>{t("UpdateDialog.typography")}</Typography>
                </DialogContent>
                <DialogActions>
                    <Button onClick={() => setOpenConfirmDialog(false)} variant="outlined" color="secondary"
                            disabled={isSubmitting} autoFocus>
                        {t("UpdateDialog.button-cancel")}
                    </Button>
                    <Button onClick={handleSubmit} variant="contained" color="primary"
                            disabled={isSubmitting}>
                        {t("UpdateDialog.button-resend")}
                    </Button>
                </DialogActions>
            </Dialog>
        </>
    );
};
export default UpdateForm;