// 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_AUTH_LIST} 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 GroupAuthTable({setSelectedRow, userAuth,isNewRecord, editMode}) {
    const [authData, setAuthData] = useState([]);
    const [onReady, setOnReady] = useState(false);
    const [currentSelectedRow, setCurrentSelectedRow] = useState(userAuth);
    const [_editMode, setEditMode] = useState(editMode);

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

    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"
        }
    }

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

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

    const columns = [
        {
            id: 'module',
            field: 'module',
            headerName: 'Module',
            flex: 1,
        },
        {
            id: 'authority',
            field: 'name',
            headerName: 'Authority',
            flex: 2,
        },
    ];

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