// material-ui
import * as React from 'react';
import {
    DataGrid,
    GridActionsCellItem,
} from "@mui/x-data-grid";
import EditIcon from '@mui/icons-material/Edit';
import VisibilityIcon from '@mui/icons-material/Visibility';
import PictureAsPdfIcon from '@mui/icons-material/PictureAsPdf';
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";
import {UploadTemplateWindow} from "../TemplateMaintainPage/UploadTemplateWindow";
import { color } from '../../../../node_modules/@mui/system/index';

// ==============================|| Template TABLE ||============================== //

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

    const [isWindowOpen, setIsWindowOpen] = React.useState(false);
    const [recordId, setRecordId] = React.useState(null);

    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) => () => {
        setIsWindowOpen(true);
        setRecordId(id);
    };

    const handleCloseClick = () => {
        setIsWindowOpen(false);
        setRecordId(null);
    };

    const handleCloseRefresh = () => {
        setIsWindowOpen(false);
        setRecordId(null);
        applySearch();
    };

    const columns = [
        {
            field: 'actions',
            type: 'actions',
            headerName: 'Actions',
            // flex: 0.5,
            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={'true'}
                            // disabled={!ability.can('VIEW','DASHBOARD')}
                        />
                        <GridActionsCellItem
                            icon={<VisibilityIcon sx={{fontSize: 25}}/>}
                            label="View"
                            className="textPrimary"
                            // onClick={handleViewClick(id)}
                            color="edit"
                            // disabled={'true'}
                            // disabled={!ability.can('VIEW','DASHBOARD')}
                        />
                    </ThemeProvider>
                ]
            },
        },
        // {
        //     id: 'title',
        //     field: 'title',
        //     headerName: 'Title',
        //     // sortComparator: dateComparator,
        //     flex: 0.75,
        //     renderCell: (params) => (
        //         <div style={{display: 'flex', alignItems: 'center', justifyContent: 'center', whiteSpace: 'normal', wordBreak: 'break-word'}}>
        //             {params.value}
        //         </div>
        //         // <div>
        //         //     {getDateString(params.row.pdfFrom,false)}
        //         // </div>
        //     ),
        // },
        {
            id: 'name',
            field: 'name',
            headerName: 'Form Name',
            flex: 2,
            renderCell: (params) => {
                return (
                    <div style={{display: 'flex', alignItems: 'center', justifyContent: 'center', whiteSpace: 'normal', wordBreak: 'break-word'}}>
                        {params.value}
                    </div>
                );
            }
        },
        {
            id: 'createDate',
            field: 'createDate',
            headerName: 'Create Datetime',
            flex: 1,
            sortComparator: dateComparator,
            renderCell: (params) => (
                <div>
                    {getDateString(params.row.created, 'dd/MM/yyyy HH:mm:ss')}
                </div>
            ),
        },
        {
            id: 'modified',
            field: 'modified',
            headerName: 'Modified Datetime',
            flex: 1,
            sortComparator: dateComparator,
            renderCell: (params) => (
                <div>
                    {getDateString(params.row.modified, 'dd/MM/yyyy HH:mm:ss')}
                </div>
            ),
        },
        {
            id: 'remarks',
            field: 'remarks',
            headerName: 'Form Code',
            flex: 2,
            renderCell: (params) => {
                return (
                    // params.value == null? <div style={{"color": "red"}}><b>Missing Form Code!</b></div> :
                    <div style={{display: 'flex', alignItems: 'center', justifyContent: 'center', whiteSpace: 'normal', wordBreak: 'break-word'}}>
                        {params.value}
                    </div>
                );
            }
        },

    ];

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