// 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, 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 AwardTable({recordList}) {
    const [rows, setRows] = React.useState(recordList);
    const [rowModesModel] = React.useState({});
    const ability = useContext(AbilityContext);

    const navigate = useNavigate()

    const gridRef = useRef(null);
    /*useEffect(() => {
        const handleResize = () => {
            if (gridRef.current) {
                gridRef.current.updateSize(); // Manually trigger layout recalculation
            }
        };

        window.addEventListener('resize', handleResize);

        return () => {
            window.removeEventListener('resize', handleResize);
        };
    }, []);*/

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

    const handleEditClick = (id) => () => {
        navigate('/award/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
                        key="OutSave"
                        icon={<EditIcon sx={{fontSize: 25}}/>}
                        label="Edit"
                        className="textPrimary"
                        onClick={handleEditClick(id)}
                        color="edit"
                        disabled={!ability.can('VIEW','AWARD')}
                    />
                    </ThemeProvider>
                ]
            },
        },
        {
            id: 'awardName',
            field: 'name',
            headerName: 'Award',
            flex: 1.3,
            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,
        },
        {
            id: 'receiveDate',
            field: 'receiveDate',
            headerName: 'Received Date',
            flex: 0.7,
            renderCell: (params) => (
                <div>
                    {getDateString(params.row.receiveDate)}
                </div>
            ),
            sortComparator: (v1, v2) => getDateString(v1,true).localeCompare(getDateString(v2,true))
        },
        {
            id: 'storageLocation',
            field: 'storageLocation',
            headerName: 'Storage Location',
            flex: 1,
        },
        {
            id: 'responsibleOfficer',
            field: 'responsibleOfficer',
            headerName: 'Responsible Officer',
            flex: 1,
        },
    ];

    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>
    );
}
