import useNotification from "@/lib/app/hooks/useNotification";
import useRequest from "@/lib/app/hooks/useRequest";
import {Button, DialogActions, DialogContent, Stack, TextField} from "@mui/material"
import {useFormik} from "formik";
import {useTranslations} from "next-intl";
import * as Yup from "yup";
import {REVISE_PROVINCE_HEAD_EXPERT} from "@/core/data/apiRoutes";

const ReviseContent = ({rowId, mutate, setOpenReviseDialog}) => {
    const t = useTranslations();
    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_PROVINCE_HEAD_EXPERT}/${rowId}`, 'post', {
                data: formData,
            }).then((response) => {
                setOpenReviseDialog(false)
                mutate()
                update_notification()
            }).catch(() => {
            }).finally(() => {
                setSubmitting(false);
            });
        },
    });

    const handleDescriptionChange = (event) => {
        formik.setFieldValue("description", event.target.value);
        formik.handleChange(event);
    };
    return (
        <>
            <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={() => setOpenReviseDialog(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>
        </>
    )
}
export default ReviseContent