// material-ui
import {
    Button,
    Grid, Typography
} from '@mui/material';
import {useEffect, useState} from "react";
import * as React from "react";
import axios from "axios";
import {apiPath} from "../../auth/utils";
import {useParams} from "react-router-dom";
import LoadingComponent from "../extra-pages/LoadingComponent";
import {GET_GROUP_LIST_PATH} from "../../utils/ApiPathConst";
import GroupAuthCard from "./GroupAuthCard";
import UserGroupInfoCard from "./UserGroupInfoCard";
import UserAddCard from "./UserAddCard";
import {getIdList} from "../../utils/CommonFunction";

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


const UserMaintainPage = () => {
    const params = useParams();
    const [onReady, setOnReady] = useState(false);
    const [isCollectData, setIsCollectData] = useState(false);
    const [editedGroupData, setEditedGroupData] = useState({});
    const [userGroupData,setUserGroupData] = useState([]);
    const [userAuthData,setUserAuthData] = useState([]);
    const [userConfirm, setUserConfirm] = useState(false);
    const [groupMember, setGroupMember] = useState([]);

    function updateGroupObject(groupData) {
        setEditedGroupData(groupData);
    }

    function updateGroupMember(groupMember){
        setGroupMember(groupMember);
    }

    function updateUserAuthList(userAuthData){
        setUserAuthData(userAuthData);
    }

    const submitData = () => {
        setUserConfirm(true);
        setIsCollectData(!isCollectData);
    }

    useEffect(() => {
        axios.get(`${apiPath}${GET_GROUP_LIST_PATH}/${params.id}`)
            .then((response) => {
                if (response.status === 200) {
                    setUserGroupData(response.data);
                }
            })
            .catch(error => {
                console.log(error);
                return false;
            });
    }, []);

    useEffect(() => {
        console.log(userGroupData);
        setOnReady(true);
    }, [userGroupData]);

    useEffect(() => {
        if(userConfirm && onReady){
            console.log("update in parents");
            console.log(editedGroupData);
            console.log(groupMember);
            console.log(getIdList(groupMember));
            console.log(userAuthData);
        }
        setUserConfirm(false);
    }, [editedGroupData,userGroupData,userAuthData]);

    return (
        !onReady ?
            <LoadingComponent/>
            :
            <Grid container rowSpacing={4.5} columnSpacing={2.75}>
                <Grid item xs={12} sx={{mb: -2.25}}>
                    <Typography variant="h5">Maintain User Group</Typography>
                </Grid>
                {/*col 1*/}
                <Grid item xs={12} md={5} lg={5}>
                    <Grid container>
                        <Grid item xs={12} md={12} lg={12}>
                            <UserGroupInfoCard
                                updateGroupObject={updateGroupObject}
                                userGroupData={userGroupData}
                                isCollectData={isCollectData}
                            />
                        </Grid>

                        <Grid item xs={12} md={12} lg={12} sx={{mt:3}}>
                            <UserAddCard
                                updateGroupMember={updateGroupMember}
                                userGroupData={userGroupData}
                                isCollectData={isCollectData}
                            />
                        </Grid>
                    </Grid>
                </Grid>
                {/*col 2*/}
                <Grid item xs={12} md={7} lg={7}>
                    <GroupAuthCard
                        updateUserAuthList={updateUserAuthList}
                        userGroupData={userGroupData}
                        isCollectData={isCollectData}
                    />
                </Grid>

                {/*bottom button*/}
                <Grid item s={12} md={12} lg={12} sx={{mb: 3}} alignItems={"end"} justifyContent="center">
                    <Grid container maxWidth justifyContent="flex-end">
                        <Grid item sx={{ml: 3, mr: 3}}>
                            <Button
                                size="large"
                                variant="contained"
                                type="submit"
                                sx={{
                                    textTransform: 'capitalize',
                                    alignItems: 'end'
                                }}
                                onClick={submitData}
                            >
                                Save User
                            </Button>
                        </Grid>
                    </Grid>
                </Grid>
            </Grid>
    );
};

export default UserMaintainPage;
