import * as Yup from 'yup';
import { yupResolver } from '@hookform/resolvers/yup';

import MuiDialog from "@/components/MuiDialog";
import { Box, Button, Card, Checkbox, DialogActions, Grid, Typography } from "@mui/material";
import { Paper } from "@mui/material";
import { Stack } from '@mui/material';
import React, { useEffect, useState } from 'react';
import { DetailFinalLogType } from "../Model/Types";
import { fDateTimesecond, toTitleCase } from "@/utils/formatTime";
import axios from "@/utils/axios";
import { enqueueSnackbar } from "notistack";
import { useNavigate } from "react-router";
import { BenefitConfigurationListType } from "../Model/Types";
import { postEditBenefit } from "../Model/Functions";
import { useForm } from 'react-hook-form';
import { FormProvider, RHFTextField } from "@/components/hook-form";
import RHFTextFieldMoney from "@/components/hook-form/v2/RHFTextFieldMoney";
import { LoadingButton } from "@mui/lab";


type DialogDeleteType = {
    openDialog: boolean;
    setOpenDialog: any;
    onSubmit?: void;
    data: BenefitConfigurationListType|undefined; 
    id: number|undefined;
}

export default function DialogEditBenefit({id, data, setOpenDialog, openDialog,onSubmit} : DialogDeleteType ) { 
    const handleCloseDialog = () => {
        setOpenDialog(false);
    }

     // setup form
    // ====================================
    const defaultValues: BenefitConfigurationListType = {
        request_log_id: 0,
        benefit_name: '',
        amount_incurred: 0,
        amount_approved: 0,
        amount_not_approved: 0,
        excess_paid: 0,
        keterangan: '-',
        description: '-'
    };

    const validationSchema = Yup.object().shape({
        amount_incurred     : Yup.string().typeError('').required(''),
        amount_approved     : Yup.string().typeError('').required(''),
        amount_not_approved : Yup.string().typeError('').required(''),
        excess_paid         : Yup.string().typeError('').required(''),
    });

    const methods = useForm<any>({
        resolver: yupResolver(validationSchema),
        defaultValues
    });

    const { handleSubmit, reset, watch, setValue, formState: { isDirty, isSubmitting, errors } } = methods;

    // Submit Form
    // =====================================
    const submitHandler = async (data: BenefitConfigurationListType) => {
    
        const response = await postEditBenefit(id, data);

        if (response == true) {
            reset();
            // navigate('custormer-service/final-log/detail/'+requestLog?.id);
            window.location.reload()
        }
    }

     // Set Value Form
    // =====================================
    useEffect(() => {
        setValue('amount_incurred', data?.amount_incurred)
        setValue('amount_approved', data?.amount_approved)
        setValue('amount_not_approved', data?.amount_not_approved)
        setValue('excess_paid', data?.excess_paid)
        setValue('keterangan', data?.keterangan)
    }, [data])

    const getContent = () => (
        <FormProvider methods={methods} onSubmit={handleSubmit(submitHandler)}>
            <Stack paddingX={2} paddingY={4}>
            {/* <Card sx={{padding:2}}> */}
                <Box sx={{ marginTop:'10px', marginBottom:'10px', py: '8px', px: '12px', border:'1px solid #919EAB52', borderRadius: '6px'}}>                    
                        <Grid key={id} container spacing={2}>
                            <Grid item xs={12}>
                                <Typography variant="subtitle1" sx={{ fontWeight: 'bold'}}>
                                    {data?.benefit?.description}
                                </Typography>
                                
                            </Grid>
                            <Grid item xs={2.5}>
                                <Grid container spacing={2}>
                                    <Grid item xs={12}>
                                        <Typography variant="subtitle1" component="div">
                                            Amount Incurred*
                                        </Typography>
                                    </Grid>
                                    <Grid item xs={12} sx={{display: 'flex', gap: 1}}>
                                        <RHFTextFieldMoney 
                                            key={id}
                                            id='amount_incurred'
                                            name={`amount_incurred`}
                                            placeholder='Amount Incurred'
                                            required
                                        />
                                    </Grid>
                                </Grid>
                            </Grid>

                            <Grid item xs={2.5}>
                                <Grid container spacing={2}>
                                    <Grid item xs={12}>
                                        <Typography variant="subtitle1" component="div">
                                            Amount Approved*
                                        </Typography>
                                    </Grid>
                                    <Grid item xs={12} sx={{display: 'flex', gap: 1}}>
                                        <RHFTextFieldMoney
                                            // onChange={() => append({amount_approved: ''}) }
                                            id='amount_approved'
                                            key={id}
                                            name={`amount_approved`}
                                            placeholder='Amount Approved'
                                            required
                                        />
                                    </Grid>
                                </Grid>
                            </Grid>

                            <Grid item xs={2.5}>
                                <Grid container spacing={2}>
                                    <Grid item xs={12}>
                                        <Typography variant="subtitle1" component="div">
                                            Amount Not Approved*
                                        </Typography>
                                    </Grid>
                                    <Grid item xs={12} sx={{display: 'flex', gap: 1}}>
                                        <RHFTextFieldMoney
                                            // onChange={() => append({amount_not_approved: ''}) }
                                            id='amount_not_approved'
                                            key={id}
                                            name={`amount_not_approved`}
                                            placeholder='Amount Not Approved'
                                            required
                                        />
                                    </Grid>
                                </Grid>
                            </Grid>

                            <Grid item xs={2.5}>
                                <Grid container spacing={2}>
                                    <Grid item xs={12}>
                                        <Typography variant="subtitle1" component="div">
                                            Excess Paid*
                                        </Typography>
                                    </Grid>
                                    <Grid item xs={12} sx={{display: 'flex', gap: 1}}>
                                        <RHFTextFieldMoney
                                            // onChange={() => append({excess_paid: ''}) }
                                            id='excess_paid'
                                            key={id}
                                            name={`excess_paid`}
                                            placeholder='Excess Paid'
                                            required
                                        />
                                    </Grid>
                                </Grid>
                            </Grid>

                            <Grid item xs={2}>
                                <Grid container spacing={2}>
                                    <Grid item xs={12}>
                                        <Typography variant="subtitle1" component="div">
                                            Keterangan*
                                        </Typography>
                                    </Grid>
                                    <Grid item xs={12} sx={{display: 'flex', gap: 1}}>
                                        <RHFTextField
                                            // onChange={() => append({keterangan: ''}) }
                                            id='keterangan'
                                            key={id}
                                            name={`keterangan`}
                                            placeholder='Keterangan'
                                            required
                                        />
                                    </Grid>
                                </Grid>
                            </Grid>
                    </Grid>
                </Box>
                {/* </Card> */}
                <DialogActions>
                    <Stack direction="row" sx={{marginTop:3}} alignItems="center" justifyContent="space-between" spacing={2}>
                        <Button variant="outlined" onClick={handleCloseDialog}><Typography>Cancel</Typography></Button>
                        <LoadingButton type="submit" variant="contained" loading={isSubmitting}>
                            Save
                        </LoadingButton>
                    </Stack>
                </DialogActions>
            </Stack>
        </FormProvider>
    );


    return (
        <MuiDialog
          title={{name: "Edit Detail Benefit"}}
          openDialog={openDialog}
          setOpenDialog={setOpenDialog}
          content={getContent()}
          maxWidth="xl"
        />
      );
}