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

const CreateContent = ({mutate, setOpenConfirmDialog}) => {
    const t = useTranslations();
    const requestServer = useRequest({auth: true})
    const validationSchema = Yup.object().shape({
        name: Yup.string("AddDialog.name_string")
            .required(t("AddDialog.setting_name_error")),
        name_fa: Yup.string("AddDialog.name_fa_string")
            .required(t("AddDialog.setting_name_fa_error")),
        value: Yup.string("AddDialog.value_string")
            .required(t("AddDialog.value_error")),
    });

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

            requestServer(ADD_ADMIN_SETTING, 'post', {
                data: formData,
            }).then((response) => {
                setOpenConfirmDialog(false)
                mutate()
                update_notification()
            }).catch(() => {
            }).finally(() => {
                setSubmitting(false);
            });
        },
    });

    return (
        <>
            <DialogContent>
                <Stack spacing={2}>
                    <Stack>
                        <TextField
                            name="name_fa"
                            rows={8}
                            label={t("AddDialog.setting_name_fa")}
                            value={formik.values.name_fa}
                            onChange={formik.handleChange}
                            fullWidth
                            variant="outlined"
                            onBlur={formik.handleBlur("name_fa")}
                            error={formik.touched.name_fa && Boolean(formik.errors.name_fa)}
                            helperText={formik.touched.name_fa && formik.errors.name_fa}
                            sx={{mt: 1}}
                        />
                    </Stack>
                    <Stack>
                        <TextField
                            name="name"
                            rows={8}
                            label={t("AddDialog.setting_name")}
                            value={formik.values.name}
                            onChange={formik.handleChange}
                            fullWidth
                            variant="outlined"
                            onBlur={formik.handleBlur("name")}
                            error={formik.touched.name && Boolean(formik.errors.name)}
                            helperText={formik.touched.name && formik.errors.name}
                            sx={{mt: 1}}
                        />
                    </Stack>
                    <Stack>
                        <TextField
                            name="value"
                            label={t("AddDialog.value")}
                            value={formik.values.value}
                            onChange={formik.handleChange}
                            fullWidth
                            variant="outlined"
                            onBlur={formik.handleBlur("value")}
                            error={formik.touched.value && Boolean(formik.errors.value)}
                            helperText={formik.touched.value && formik.errors.value}
                            sx={{mt: 1}}
                        />
                    </Stack>
                </Stack>
            </DialogContent>
            <DialogActions>
                <Button onClick={() => setOpenConfirmDialog(false)} variant="outlined" color="secondary"
                        disabled={formik.isSubmitting} autoFocus>
                    {t("AddDialog.button-cancel")}
                </Button>
                <Button onClick={formik.handleSubmit} variant="contained" color="primary"
                        disabled={formik.isSubmitting || !formik.dirty || !formik.isValid}>
                    {t("AddDialog.button-add")}
                </Button>
            </DialogActions>
        </>
    )
}
export default CreateContent