// 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 {CustomNoRowsOverlay, removeObjectWithId} from "../../utils/CommonFunction";
import {LIONER_BUTTON_THEME} from "../../themes/colorConst";
import {ThemeProvider} from "@emotion/react";

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

export default function UserAddTable({setGroupUserData, userList,setDeletedList}) {
    const [groupData, setGroupData] = useState([]);
    const [onReady, setOnReady] = useState(false);
    const [localDeletedList, setLocalDeletedList] = React.useState([]);

    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];
            setLocalDeletedList(temp);
        }
        const newList =removeObjectWithId(groupData,id)
        setGroupUserData(newList);
        setGroupData(newList);
    };

    const columns = [
        {
            field: 'actions',
            type: 'actions',
            headerName: 'Actions',
            width: 100,
            cellClassName: 'actions',
            getActions: ({id}) => {
                return [
                    <ThemeProvider theme={LIONER_BUTTON_THEME} key="OutDelete">
                    <GridActionsCellItem
                        icon={<DeleteIcon sx={{fontSize: 25}}/>}
                        label="Delete"
                        onClick={handleDeleteClick(id)}
                        color="delete"
                    />
                    </ThemeProvider>,
                    ]
            },
        },
        {
            id: 'name',
            field: 'name',
            headerName: 'User',
            flex: 0.7,
        },
        {
            id: 'subDiv',
            field: 'subDivisionName',
            headerName: 'Sub-Division',
            flex: 1.3,
            renderCell: (params) => {
                return (
                    <div style={{display: 'flex', alignItems: 'center', justifyContent: 'center', whiteSpace: 'normal', wordBreak: 'break-word'}}>
                        {params.value}
                    </div>
                );
            }
        },
    ];

    return (
        !onReady ?
            <LoadingComponent/>
            :
            <div style={{height: '48vh', width: '100%' }}>
                <DataGrid
                    rows={groupData}
                    columns={columns}
                    columnHeaderHeight={45}
                    editMode="row"
                    initialState={{
                        pagination: {
                            paginationModel: {page: 0, pageSize: 10},
                        },
                    }}
                    slots={{
                        noRowsOverlay: () => (
                            CustomNoRowsOverlay()
                        )
                    }}
                    getRowHeight={() => 'auto'}
                    pageSizeOptions={10}
                    pageSize={20}
                    //autoHeight={true}
                />
            </div>
    );
}
