// material-ui
import {
    DataGrid,
} from "@mui/x-data-grid";
import {useEffect, useState} from "react";
import axios from "axios";
import {apiPath} from "../../auth/utils";
import LoadingComponent from "../extra-pages/LoadingComponent";
import * as React from "react";
import {GET_GROUP_COMBO_PATH} from "../../utils/ApiPathConst";
import {CustomNoRowsOverlay} from "../../utils/CommonFunction";

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

export default function UserGroupTable({setSelectedRow, userGroup}) {
    const [groupData, setGroupData] = useState([]);
    const [onReady, setOnReady] = useState(false);
    const [currentSelectedRow, setCurrentSelectedRow] = useState(userGroup);

    useEffect(() => {
        axios.get(`${apiPath}${GET_GROUP_COMBO_PATH}`)
            .then((response) => {
                if (response.status === 200) {
                    setGroupData(response.data.records);
                }
            })
            .catch(error => {
                console.log(error);
                return false;
            });
    }, []);

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

    const columns = [
        {
            id: 'group',
            field: 'name',
            headerName: 'Select All',
            flex: 1,
        },
    ];

    return (
        !onReady ?
            <LoadingComponent/>
            :
            <div style={{height: '100%', width: '100%'}}>
                <DataGrid
                    rows={groupData}
                    columns={columns}
                    columnHeaderHeight={45}
                    editMode="row"
                    initialState={{
                        pagination: {
                            paginationModel: {page: 0, pageSize: 5},
                        },
                    }}
                    slots={{
                        noRowsOverlay: () => (
                            CustomNoRowsOverlay()
                        )
                    }}
                    // getRowHeight={(params) => Math.max(
                    //     getRowHeight(params, 'name', 30),
                    // )}
                    getRowHeight={() => 'auto'}
                    pageSizeOptions={[3, 5]}
                    checkboxSelection
                    rowSelectionModel={currentSelectedRow}
                    onRowSelectionModelChange={(ids) => {
                        setSelectedRow(ids);
                        setCurrentSelectedRow(ids);
                    }}
                />
            </div>
    );
}
