// material-ui
import {
    Grid,
    Dialog, DialogTitle, DialogContent, DialogActions,
    Typography,
    Button,
    Stack,
    TextField
} from '@mui/material';

import { useFormik } from 'formik';
import * as React from "react";
import * as yup from 'yup';
import * as HttpUtils from "utils/HttpUtils";
import { apiPath } from "auth/utils";
import { useIntl } from "react-intl";


// ==============================|| DASHBOARD - DEFAULT ||============================== //



const Mail = () => {

    const [isResponsPopUp, setIsResponsPopUp] = React.useState(false);
    const [responsText, setResponsText] = React.useState("");
    const intl = useIntl();

    function getMaxErrStr(num, fieldname) {
        return intl.formatMessage({ id: 'noMoreThenNWords' }, { num: num, fieldname: fieldname ? intl.formatMessage({ id: fieldname }) + ": " : "" });
    }

    const formik = useFormik({
        enableReinitialize: true,
        initialValues: { email: "" },
        validationSchema: yup.object().shape({
            email: yup.string().max(128, getMaxErrStr(128)).required(intl.formatMessage({ id: 'require' }) + 'e-Mail')
        }),
        onSubmit: values => {
            console.log(values);
            HttpUtils.post({
                url: apiPath + "/test/send-mail",
                params: {
                    email: values.email,
                    tempKey: values.tempKey
                },
                onSuccess: function () {
                    setResponsText("Success");
                    setIsResponsPopUp(true);
                },
                onFail: (response) => {
                    setResponsText("Fail: " + response);
                    setIsResponsPopUp(true);
                },
                onError: (error) => {
                    setResponsText("Error: " + error);
                    setIsResponsPopUp(true);
                }
            });
        }
    });

    const setReminderDate=()=>{
        HttpUtils.get({
            url: apiPath + "/demandNote/set-expect-reminder",
            onSuccess: function () {
                setResponsText("Success");
                setIsResponsPopUp(true);
            },
            onFail: (response) => {
                setResponsText("Fail: " + response);
                setIsResponsPopUp(true);
            },
            onError: (error) => {
                setResponsText("Error: " + error);
                setIsResponsPopUp(true);
            }
        });
    }

    const testDaily_checkDNStatus=()=>{
        HttpUtils.post({
            url: apiPath + "/test/daily_checkDNStatus",
            onSuccess: function () {
                setResponsText("Success");
                setIsResponsPopUp(true);
            },
            onFail: (response) => {
                setResponsText("Fail: " + response);
                setIsResponsPopUp(true);
            },
            onError: (error) => {
                setResponsText("Error: " + error);
                setIsResponsPopUp(true);
            }
        });
    }
    

    return (
        !JSON.parse(localStorage.getItem('userData')).fullenName == "2fi" ?
            <Grid container >
                <Typography variant="h1">Ooops! Seem go wrong page...</Typography>
            </Grid>
            :
            <Grid container spacing={1} >
                <Grid item xs={12}>
                    <Typography variant="h3" style={{ padding: 8 }}>Test View</Typography>
                </Grid>

                {/*col 2*/}
                <form onSubmit={formik.handleSubmit} style={{ width: '100%', paddingLeft: 64 , paddingBottom: 60}}>
                    <Grid item xs={12} mt={1} >
                        <Typography variant="h4" style={{ padding: 8 }}>Test Send Mail</Typography>
                        <Stack direction="row" justifyContent="flex-end" alignItems="center">
                            <TextField
                                fullWidth
                                id="email"
                                name="email"
                                type="email"
                                placeholder="email"
                                label="eMail"
                                onChange={formik.handleChange}
                            />
                        </Stack>
                        <Stack direction="row" justifyContent="flex-end" alignItems="center">
                            <TextField
                                fullWidth
                                id="tempKey"
                                name="tempKey"
                                type="tempKey"
                                placeholder="tempKey"
                                label="tempKey"
                                onChange={formik.handleChange}
                            />
                        </Stack>
                    </Grid>
                    <Button
                        variant="contained"
                        type="submit"
                        sx={{
                            textTransform: 'capitalize',
                            alignItems: 'end',
                            width: 200
                        }}>
                       Send
                    </Button>
                </form>
                <Grid item xs={12} mt={1} style={{ width: '100%', paddingLeft: 64, paddingBottom: 60 }}>
                    <Typography variant="h4" style={{ padding: 8 }}>Set DN Reminder Expect send Date</Typography>
                    <Button
                        variant="contained"
                        onClick={()=>{
                            setReminderDate();
                        }}
                        sx={{
                            textTransform: 'capitalize',
                            alignItems: 'end',
                            width: 200
                        }}>
                        Set Expect Date
                    </Button>
                </Grid>

                <Grid item xs={12} mt={1} style={{ width: '100%', paddingLeft: 64, paddingBottom: 60 }}>
                    <Typography variant="h4" style={{ padding: 8 }}>Set DN Reminder Expect send Date</Typography>
                    <Button
                        variant="contained"
                        onClick={()=>{
                            testDaily_checkDNStatus();
                        }}
                        sx={{
                            textTransform: 'capitalize',
                            alignItems: 'end',
                            width: 250
                        }}>
                        Test daily_checkDNStatus
                    </Button>
                </Grid>

                <div>
                    <Dialog
                        open={isResponsPopUp}
                        onClose={() => setIsResponsPopUp(false)}
                        PaperProps={{
                            sx: {
                                minWidth: '40vw',
                                maxWidth: { xs: '90vw', s: '90vw', m: '70vw', lg: '70vw' },
                                maxHeight: { xs: '90vh', s: '70vh', m: '70vh', lg: '60vh' }
                            }
                        }}
                    >
                        <DialogTitle>Respons</DialogTitle>
                        <DialogContent style={{ display: 'flex', }}>
                            <Typography variant="h5" style={{ padding: '16px' }}>{responsText}</Typography>
                        </DialogContent>
                        <DialogActions>
                            <Button onClick={() => setIsResponsPopUp(false)}>OK</Button>
                        </DialogActions>
                    </Dialog>
                </div>


            </Grid>
    );
};


export default Mail;
