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

const EditContent = ({rowId, row, mutate, setOpenEditDialog}) => {
    const t = useTranslations();
    const requestServer = useRequest({auth: true, notification: false});
    const {update_notification} = useNotification();

    const validationSchema = Yup.object().shape({
        approved_amount: Yup.string().required(`${t("EditDialog.edit_approved_amount_required")}`)
    })

    const formik = useFormik({
        initialValues: {
            approved_amount: String(row.original.approved_amount / 1000000),
        }, validationSchema,
        onSubmit: (values, {setSubmitting}) => {
            requestServer(`${CONFIRM_TRANSPORTATION_ASSISTANCE}/${rowId}`, 'post', {
                notification: true
            }).then(() => {
                setOpenEditDialog(false)
                mutate()
                update_notification()
            }).catch(() => {
            }).finally(() => {
                setSubmitting(false);
            });
        },
    });
    const handleAmountChange = (event) => {
        if (!isNaN(event.target.value)) {
            formik.setFieldValue("approved_amount", event.target.value)
        } else {
            formik.setFieldValue("approved_amount", formik.values.approved_amount)
        }
    };
    return (
        <>
            <DialogContent>
                <Stack spacing={2}>
                    <Stack>
                        <PriceField
                            name="approved_amount"
                            label={<>
                                <span>{t("EditDialog.edit_approved_amount")}</span><small>{t("EditDialog.unit")}</small></>}
                            type="text"
                            inputProps={{
                                inputMode: "number",
                                min: 0,
                                pattern: "[0-9]*",
                            }}
                            variant="outlined"
                            value={formik.values.approved_amount}
                            onChange={handleAmountChange}
                            onBlur={formik.handleBlur("approved_amount")}
                            error={
                                formik.touched.approved_amount &&
                                Boolean(formik.errors.approved_amount)
                            }
                            helperText={
                                formik.touched.approved_amount && formik.errors.approved_amount
                            }
                            sx={{mt: 1}}
                            fullWidth
                        />
                    </Stack>
                </Stack>
            </DialogContent>
            <DialogActions>
                <Button onClick={() => setOpenEditDialog(false)} variant="outlined" color="secondary"
                        disabled={formik.isSubmitting} autoFocus>
                    {t("EditDialog.button-cancel")}
                </Button>
                <Button onClick={formik.handleSubmit} variant="contained" color="primary"
                        disabled={formik.isSubmitting || !formik.isValid || !formik.dirty}
                >
                    {t("EditDialog.button-edit")}
                </Button>
            </DialogActions>
        </>
    )

}
export default EditContent