// 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} from "react";
import {useNavigate} from "react-router-dom";
import {CustomNoRowsOverlay, dateComparator, getDateString} from "../../utils/CommonFunction";
import AbilityContext from "../../components/AbilityProvider";
import {LIONER_BUTTON_THEME} from "../../themes/colorConst";
import {ThemeProvider} from "@emotion/react";

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

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

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

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

    const columns = [
        {
            field: 'actions',
            type: 'actions',
            headerName: 'Actions',
            width: 100,
            cellClassName: 'actions',
            getActions: ({id}) => {
                return [
                    <ThemeProvider key="OutSave" theme={LIONER_BUTTON_THEME}>
                        <GridActionsCellItem
                            icon={<EditIcon sx={{fontSize: 25}}/>}
                            label="Edit"
                            className="textPrimary"
                            onClick={handleEditClick(id)}
                            color="edit"
                            disabled={!ability.can('VIEW','EVENT')}
                        />
                    </ThemeProvider>
                ]
            },
        },
        {
            id: 'name',
            field: 'name',
            headerName: 'Event Name',
            flex: 2,
            renderCell: (params) => {
                return (
                    <div style={{display: 'flex', alignItems: 'center', justifyContent: 'center', whiteSpace: 'normal', wordBreak: 'break-word'}}>
                        {params.value}
                    </div>
                );
            }
        },
        {
            id: 'eventDate',
            field: 'startDate',
            headerName: 'Application Date',
            flex: 0.45,
            sortComparator: dateComparator,
            renderCell: (params) => (
                <div>
                    {getDateString(params.row.startDate,false)}
                </div>
            ),
        },
        {
            id: 'applicationDeadline',
            field: 'applicationDeadline',
            headerName: 'Application Deadline',
            sortComparator: dateComparator,
            flex: 0.5,
            renderCell: (params) => (
                <div>
                    {getDateString(params.row.applicationDeadline,false)}
                </div>
            ),
        },
        {
            id: 'eventFrom',
            field: 'eventFrom',
            headerName: 'Event Start Date',
            sortComparator: dateComparator,
            flex: 0.45,
            renderCell: (params) => (
                <div>
                    {getDateString(params.row.eventFrom,false)}
                </div>
            ),
        },

    ];

    return (
        <DataGrid
            rows={rows}
            columns={columns}
            columnHeaderHeight={45}
            editMode="row"
            //autoPageSize
            rowModesModel={rowModesModel}
            getRowHeight={() => 'auto'}
            paginationModel={paginationModel}
            onPaginationModelChange={setPaginationModel}
            slots={{
                noRowsOverlay: () => (
                    CustomNoRowsOverlay()
                )
            }}
            pageSizeOptions={[10]}
            autoHeight
        />
    );
}
