// material-ui
import * as React from 'react';
import {
    DataGrid,
    GridActionsCellItem,
} from "@mui/x-data-grid";
import VisibilityIcon from '@mui/icons-material/Visibility';
import {useEffect} from "react";
import {useNavigate} from "react-router-dom";
import { useTheme } from '@mui/material/styles';
import Checkbox from '@mui/material/Checkbox';
// ==============================|| EVENT TABLE ||============================== //

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

    const navigate = useNavigate()

    useEffect(() => {
        setRows(recordList);
    }, [recordList]);

    const handleActionClick = (id) => () => {
        navigate('/orgUser/'+ id);
    };

    const columns = [
        {
            field: 'actions',
            type: 'actions',
            headerName: 'Actions',
            width: 100,
            cellClassName: 'actions',
            getActions: ({id}) => {
                return [
                    <GridActionsCellItem
                        key="OutSave"
                        icon={<VisibilityIcon/>}
                        label="View"
                        className="textPrimary"
                        onClick={handleActionClick(id)}
                        color="primary"
                    />]
            },
        },
        {
            id: 'username',
            field: 'username',
            headerName: 'User Name',
            flex: 1,
        },
        {
            id: 'name',
            field: 'name',
            headerName: 'Full Name',
            flex: 1,
        },
        {
            id: 'email',
            field: 'email',
            headerName: 'Email',
            flex: 1,
        },
        {
            id: 'locked',
            field: 'locked',
            type: 'bool',
            headerName: 'Locked',
            flex: 1,
            renderCell: (params) => {
                return (
                    <Checkbox
                        theme={theme}
                        key="locked"
                        checked={params.row.locked}
                        color="primary"
                        size="small"
                        //onChange={handleChange}
                    />
                );
            },
        },
    ];

    return (
        <div style={{height: 400, width: '100%'}}>
            <DataGrid
                rows={rows}
                columns={columns}
                editMode="row"
                rowModesModel={rowModesModel}
                initialState={{
                    pagination: {
                        paginationModel: {page: 0, pageSize: 5},
                    },
                }}
                pageSizeOptions={[5, 10]}
                autoHeight
            />
        </div>
    );
}
