// material-ui
import * as React from 'react';
import {
    DataGrid,
    GridActionsCellItem,
} from "@mui/x-data-grid";
import EditIcon from '@mui/icons-material/Edit';
import {useEffect} from "react";
import {useNavigate} from "react-router-dom";
import { useTheme } from '@mui/material/styles';
import Checkbox from '@mui/material/Checkbox';
import {CustomNoRowsOverlay} from "../../utils/CommonFunction";
import {LIONER_BUTTON_THEME} from "../../themes/colorConst";
import {USER_GROUP_COMBO} from "../../utils/ComboConst";
import {ThemeProvider} from "@emotion/react";

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

export default function UserTable({recordList}) {
    const [rows, setRows] = React.useState(recordList);
    const [rowModesModel] = React.useState({});
    const theme = useTheme();

    const navigate = useNavigate()

    const [paginationModel, setPaginationModel] = React.useState({
        page: 0,
        pageSize:10
    });
    useEffect(() => {
        setPaginationModel({page:0,pageSize:10});
        setRows(recordList);
    }, [recordList]);

    const handleEditClick = (id) => () => {
        navigate('/user/'+ id);
    };

    const columns = [
        {
            field: 'actions',
            type: 'actions',
            headerName: 'Actions',
            width: 100,
            cellClassName: 'actions',
            getActions: ({id}) => {
                return [
                    <ThemeProvider key="OutSave" theme={LIONER_BUTTON_THEME}>
                    <GridActionsCellItem
                        key="OutSave"
                        icon={
                            <EditIcon sx={{fontSize:25}}/>
                        }
                        label="Edit"
                        className="textPrimary"
                        onClick={handleEditClick(id)}
                        color="edit"
                    />
                    </ThemeProvider>]
            },
        },
        {
            id: 'username',
            field: 'username',
            headerName: 'User Name',
            flex: 0.8,
        },
        {
            id: 'name',
            field: 'fullname',
            headerName: 'Full Name',
            flex: 0.75,
        },
        {
            id: 'groupId',
            field: 'groupId',
            headerName: 'User Group',
            flex: 0.5,
            renderCell: (params) => {
                const userGroup = USER_GROUP_COMBO.find(item => item.id === params.value)?? {label: "N/A"};
                return (
                    <div style={{display: 'flex', alignItems: 'center', justifyContent: 'center', whiteSpace: 'normal', wordBreak: 'break-word'}}>
                        {userGroup.label}
                    </div>
                );
            }
        },
        {
            id: 'email',
            field: 'email',
            headerName: 'Email',
            flex: 1.45,
            renderCell: (params) => {
                return (
                    <div style={{display: 'flex', alignItems: 'center', justifyContent: 'center', whiteSpace: 'normal', wordBreak: 'break-word'}}>
                        {params.value}
                    </div>
                );
            }
        },
        {
            id: 'twoFactorEnabled',
            field: 'twoFactorEnabled',
            headerName: '2FA Enabled',
            flex: 0.5,
            renderCell: (params) => {
                return (
                    <input
                        type="checkbox"
                        checked={!!params.row.twoFactorEnabled}
                        disabled
                    />
                );
            },
        },
        {
            id: 'locked',
            field: 'locked',
            type: 'bool',
            headerName: 'Locked',
            flex: 0.5,
            renderCell: (params) => {
                return (
                    <input
                    type="checkbox"
                    checked={params.row.locked}
                    // onClick={handleClick} // Prevent click effect
                    />
                    // <Checkbox
                    //     theme={theme}
                    //     key="locked"
                    //     checked={params.row.locked}
                    //     color="primary"
                    //     size="medium"
                    //     disabled
                    //     //onChange={handleChange}
                    // />
                );
            },
        },
    ];

    return (
        <div style={{/*height: '51vh',*/ width: '100%'}}>
            <DataGrid
                rows={rows}
                columns={columns}
                columnHeaderHeight={45}
                editMode="row"
                rowModesModel={rowModesModel}
            paginationModel={paginationModel}
            onPaginationModelChange={setPaginationModel}
                getRowHeight={() => 'auto'}
                slots={{
                    noRowsOverlay: () => (
                        CustomNoRowsOverlay()
                    )
                }}
                pageSizeOptions={[10]}
                autoHeight
            />
        </div>
    );
}
