// material-ui
import {
    DataGrid,
} from "@mui/x-data-grid";
import {useEffect, useState} from "react";
import axios from "axios";
import {apiPath} from "auth/utils";
import * as React from "react";
import {GET_GROUP_COMBO_PATH} from "utils/ApiPathConst";
import Loadable from 'components/Loadable';
import { lazy } from 'react';
const LoadingComponent = Loadable(lazy(() => import('../../extra-pages/LoadingComponent')));

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

export default function UserGroupTable({setSelectedRow, userGroup,isNewRecord,editMode}) {
    const [groupData, setGroupData] = useState([]);
    const [onReady, setOnReady] = useState(false);
    const [currentSelectedRow, setCurrentSelectedRow] = useState(userGroup);
    
    const _sx = {
        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"
        }
    }

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

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

    const columns = [
        {
            id: 'group',
            field: 'name',
            headerName: 'Group',
            flex: 1,
            editable: true,
        },
    ];

    return (
        !onReady ?
            <LoadingComponent/>
            :
            <div style={{height: '100%', width: '100%'}}>
                <DataGrid
                    rows={groupData}
                    columns={columns}
                    editMode="row"
                    initialState={{
                        pagination: {
                            paginationModel: {page: 0, pageSize: 4},
                        },
                    }}
                    pageSizeOptions={[4, 8, 16]}
                    checkboxSelection
                    rowSelectionModel={currentSelectedRow}
                    onRowSelectionModelChange={(ids) => {
                        // console.log(ids);
                        if (editMode){
                            setSelectedRow(ids);
                            setCurrentSelectedRow(ids);
                        }
                    }}
                    autoHeight
                    sx={_sx}
                />
            </div>
    );
}
