
import * as React from "react";
import * as HttpUtils from "utils/HttpUtils";
import * as UrlUtils from "utils/ApiPathConst";
import { useNavigate } from "react-router-dom";
import { useDispatch } from "react-redux";
import { handleLogoutFunction, handleLogin } from 'auth/index';

import Loadable from 'components/Loadable';
const LoadingComponent = Loadable(React.lazy(() => import('pages/extra-pages/LoadingComponent')));
import {
    Grid,
} from '@mui/material';

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

const Index = () => {

    const dispatch = useDispatch()
    const navigate = useNavigate()

    React.useEffect(() => {
        goLogin();
    }, []);

    function goLogin(){
        dispatch(handleLogoutFunction());

        let params = new URLSearchParams(window.location.search)
        if(params.get("code")){
            HttpUtils.post({
                url: UrlUtils.GET_SMART_DIRECT_LOGIN,
                params:{
                    code: params.get("code")
                },
                onSuccess: (responseData) => {
                    // console.log("responseData");
                    // console.log(responseData);
                        const userData = {
                          id: responseData.id,
                          fullenName: responseData.name,
                          fullchName: responseData.chName,
                          email: responseData.email,
                          type: responseData.type,
                          role: responseData.role,
                          abilities: responseData.abilities,
                          creditor: responseData.creditor,
                          passwordExpiryDate: responseData.passwordExpiryDate,
                          orgPaymentRecord: responseData.orgPaymentRecord,
                          orgDnRecord: responseData.orgDnRecord,
                          //avatar: require('src/assets/images/users/avatar-3.png').default,
                        }
                        const data = { ...userData, accessToken: responseData.accessToken, refreshToken: responseData.refreshToken }
                        dispatch(handleLogin(data))
                        navigate('/dashboard');
                },
                onFail: (response)=>{
                    console.log("Fail");
                    console.log(response);
                    window.location.assign("/iamsmart/loginFail");
                },
                onError:(error)=>{
                    console.log("onError");
                    console.log(error);
                    if(error?.response?.data?.exception == "msg: please verify email."){
                        window.location.assign("/iamsmart/notverify");
                    }else{
                        window.location.assign("/iamsmart/loginFail");
                    }
                }
            });
        }else{
            window.location.assign("/iamsmart/loginFail");
        }
    }

    return (
        <Grid container sx={{ minHeight: '87vh', mb: 3 }} direction="column" justifyContent="center" alignItems="center">
            <Grid item>
                <LoadingComponent />
            </Grid>
        </Grid>
    );
};

export default Index;