// material-ui
import * as React from 'react';
import {
    DataGrid,
    GridActionsCellItem,
} from "@mui/x-data-grid";
import EditIcon from '@mui/icons-material/Edit';
import {useContext, useEffect, useRef} from "react";
import {useNavigate} from "react-router-dom";
import {CustomNoRowsOverlay} from "../../utils/CommonFunction";
import AbilityContext from "../../components/AbilityProvider";

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

export default function ApplicationTable({recordList}) {
    const [rows, setRows] = React.useState(recordList);
    const [rowModesModel] = React.useState({});
    const ability = useContext(AbilityContext);

    const navigate = useNavigate()

    const gridRef = useRef(null);

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

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

    const columns = [
        {
            field: 'actions',
            type: 'actions',
            headerName: 'Actions',
            width: 100,
            cellClassName: 'actions',
            getActions: ({id}) => {
                return [
                    <GridActionsCellItem
                        key="OutSave"
                        icon={<EditIcon sx={{fontSize: 25}}/>}
                        label="Edit"
                        className="textPrimary"
                        onClick={handleEditClick(id)}
                        color="warning"
                        disabled={!ability.can('VIEW','APPLICATION')}
                    />]
            },
        },
        {
            id: 'applicationName',
            field: 'applicationName',
            headerName: 'Application',
            flex: 1,
            renderCell: (params) => {
                return (
                    <div style={{display: 'flex', alignItems: 'center', justifyContent: 'center', whiteSpace: 'normal', wordBreak: 'break-word'}}>
                        {params.value}
                    </div>
                );
            }
        },
        {
            id: 'subDivisions',
            field: 'subDivisions',
            //type: 'date',
            //sortable: false,
            headerName: 'Sub-Divisions',
            flex: 1,
            renderCell: (params) => {
                return (
                    <div style={{display: 'flex', alignItems: 'center', justifyContent: 'center', whiteSpace: 'normal', wordBreak: 'break-word'}}>
                        {params.value}
                    </div>
                );
            }
        },
        {
            id: 'tags',
            field: 'tags',
            //type: 'date',
            //sortable: false,
            headerName: 'Tags',
            flex: 0.8,
        },
        {
            id: 'responsibleOfficer',
            field: 'responsibleOfficer',
            headerName: 'Responsible Officer',
            flex: 0.7,
        },
    ];

    return (
        <div style={{/*height: 400,*/ width: '100%', }}>
            <DataGrid
                disableColumnMenu
                ref={gridRef}
                rows={rows}
                columns={columns}
                columnHeaderHeight={45}
                editMode="row"
                rowModesModel={rowModesModel}
                getRowHeight={() => 'auto'}
                initialState={{
                    pagination: {
                        paginationModel: {page: 0, pageSize: 5},
                    },
                }}
                slots={{
                    noRowsOverlay: () => (
                        CustomNoRowsOverlay()
                    )
                }}
                pageSizeOptions={[5]}
                autoHeight
            />
        </div>
    );
}
