import {REVISE_INSPECTOR_EXPERT} from "@/core/data/apiRoutes";
import {
    Button,
    Dialog,
    DialogActions,
    DialogContent,
    DialogTitle,
    IconButton,
    Stack,
    TextField,
    Tooltip,
} from "@mui/material";
import {useTranslations} from "next-intl";
import {useFormik} from "formik";
import * as Yup from "yup";
import {useState} from "react";
import useRequest from "@/lib/app/hooks/useRequest";
import ReplyIcon from '@mui/icons-material/Reply';
import useNotification from "@/lib/app/hooks/useNotification";


const Revise = ({rowId, fetchUrl, mutate}) => {
    const t = useTranslations();
    const [openRejectDialog, setOpenRejectDialog] = useState(false);
    const requestServer = useRequest({auth: true})
    const {update_notification} = useNotification()

    const validationSchema = Yup.object().shape({
        description: Yup.string().required(t("ReviseDialog.description_error")),
    });

    const formik = useFormik({
        initialValues: {
            description: "",
        },
        validationSchema,
        onSubmit: (values, {setSubmitting}) => {
            const formData = new FormData();
            formData.append("description", values.description);

            requestServer(`${REVISE_INSPECTOR_EXPERT}/${rowId}`, 'post', {
                data: formData,
            }).then((response) => {
                mutate(fetchUrl)
                update_notification()
            }).catch(() => {
            }).finally(() => {
                setSubmitting(false);
            });
        },
    });

    const handleDescriptionChange = (event) => {
        formik.setFieldValue("description", event.target.value);
        formik.handleChange(event);
    };

    return (
        <>
            <Tooltip title={t("ReviseDialog.revise")}>
                <IconButton color="primary" onClick={() => setOpenRejectDialog(true)}>
                    <ReplyIcon/>
                </IconButton>
            </Tooltip>
            <Dialog fullWidth open={openRejectDialog}
                    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("ReviseDialog.revise")}</DialogTitle>
                <DialogContent>
                    <Stack spacing={2}>
                        <Stack>
                            <TextField
                                name="description"
                                multiline
                                rows={8}
                                label={t("ReviseDialog.description")}
                                value={formik.values.description}
                                onChange={handleDescriptionChange}
                                onBlur={formik.handleBlur("description")}
                                error={
                                    formik.touched.description && Boolean(formik.errors.description)
                                }
                                helperText={formik.touched.description && formik.errors.description}
                                fullWidth
                                variant="outlined"
                                sx={{mt: 1}}
                            />
                        </Stack>
                    </Stack>
                </DialogContent>
                <DialogActions>
                    <Button onClick={() => setOpenRejectDialog(false)} variant="outlined" color="secondary"
                            disabled={formik.isSubmitting} autoFocus>
                        {t("ReviseDialog.button-cancel")}
                    </Button>
                    <Button onClick={formik.handleSubmit} variant="contained" color="primary"
                            disabled={formik.isSubmitting || !formik.dirty || !formik.isValid}>
                        {t("ReviseDialog.button-revise")}
                    </Button>
                </DialogActions>
            </Dialog>
        </>
    );
};
export default Revise;