// material-ui
import {
    DataGrid, GridActionsCellItem,
} from "@mui/x-data-grid";
import {useEffect, useState} from "react";
//import LoadingComponent from "../extra-pages/LoadingComponent";
import * as React from "react";
import DeleteIcon from "@mui/icons-material/DeleteOutlined";
import {removeObjectWithId} from "../../utils/CommonFunction";

import Loadable from 'components/Loadable';
import { lazy } from 'react';
const LoadingComponent = Loadable(lazy(() => import('../extra-pages/LoadingComponent')));

// ==============================|| EVENT TABLE ||============================== //

export default function UserAddTable({setGroupUserData, userList,setDeletedList, editMode}) {
    const [groupData, setGroupData] = useState([]);
    const [onReady, setOnReady] = useState(false);
    const [localDeletedList, setLocalDeletedList] = React.useState([]);
    const _sx = {
        ml: 3,
        mb: 3,
        mr: 3,
        padding: "4 2 4 2",
        boxShadow: 1,
        border: 1,
        borderColor: '#DDD',
        '& .MuiDataGrid-cell': {
            borderTop: 1,
            borderBottom: 1,
            borderColor: "#EEE"
        },
        '& .MuiDataGrid-footerContainer': {
            border: 1,
            borderColor: "#EEE"
        }
    }

    const [_editMode, setEditMode] = useState(editMode);

    useEffect(()=>{
        setEditMode(editMode);
    },[editMode])

    useEffect(() => {
        setGroupData(userList);
    }, []);

    useEffect(() => {
        setGroupData(userList);
    }, [userList]);

    useEffect(() => {
        setDeletedList(localDeletedList);
    }, [localDeletedList]);

    useEffect(() => {
        //if state data are ready and assign to different field
        if (groupData !== undefined) {
            setOnReady(true);
        }
    }, [groupData]);

    const handleDeleteClick = (id) => () => {
        if(id >0){
            const temp = [...localDeletedList, id];
            // console.log("temp here");
            // console.log(temp);
            setLocalDeletedList(temp);
        }
        const newList =removeObjectWithId(groupData,id)
        setGroupUserData(newList);
        setGroupData(newList);
    };


    const columns = [
        _editMode?
        {
            field: 'actions',
            type: 'actions',
            headerName: 'Actions',
            width: 100,
            cellClassName: 'actions',
            getActions: ({id}) => {
                return [
                    <GridActionsCellItem
                        key="OutDelete"
                        icon={<DeleteIcon/>}
                        label="Delete"
                        onClick={handleDeleteClick(id)}
                        color="inherit"
                    />,
                    ]
            },
        }:{},
        {
            id: 'name',
            field: 'name',
            headerName: 'User',
            flex: 1,
        },
    ];

    return (
        !onReady ?
            <LoadingComponent/>
            :
            <div style={{height: '100%', width: '100%', overflow: "auto" }}>
                <DataGrid
                    rows={groupData}
                    columns={columns}
                    editMode="row"
                    initialState={{
                        pagination: {
                            paginationModel: {page: 0, pageSize: 3},
                        },
                    }}
                    pageSizeOptions={[3, 5]}
                    pageSize={20}
                    autoHeight={true}
                    sx={_sx}
                />
            </div>
    );
}
