// material-ui
import * as React from 'react';
import {
    DataGrid,
    GridActionsCellItem,
} from "@mui/x-data-grid";
import EditIcon from '@mui/icons-material/Edit';
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";

// ==============================|| CLIENT TABLE ||============================== //

export default function ClientTable({recordList, pageSize}) {
    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:pageSize});
        setRows(recordList);
    }, [recordList]);

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

    const handlePdfClick = (id) => () => {
        navigate(`/pdf/${id}`);
    };

    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={<PictureAsPdfIcon sx={{fontSize: 25}}/>}
                            label="Edit PDF"
                            className="textPrimary"
                            onClick={handlePdfClick(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.clientFrom,false)}
        //         // </div>
        //     ),
        // },
        {
            id: 'clientCode',
            field: 'clientCode',
            headerName: 'Client Code',
            flex: 1.5,
        },
        {
            id: 'fullname',
            field: 'fullname',
            headerName: 'Client Name',
            flex: 1.5,
            renderCell: (params) => {
                return (
                    <div style={{display: 'flex', alignItems: 'center', justifyContent: 'center', whiteSpace: 'normal', wordBreak: 'break-word'}}>
                        {params.row.firstname ? ` ${params.row.lastname} ${params.row.firstname}` : `Mr/Ms ${params.row.lastname}`}
                    </div>
                );
            }
        },
        // {
        //     id: 'created',
        //     field: 'created',
        //     headerName: 'Create Date',
        //     flex: 1,
        //     sortComparator: dateComparator,
        //     renderCell: (params) => (
        //         <div>
        //             {getDateString(params.row.created, 'dd/MM/yyyy')}
        //         </div>
        //     ),
        // },
        // {
        //     id: 'lastname',
        //     field: 'lastname',
        //     headerName: 'Last Name',
        //     flex: 1.5,
        //     sortComparator: dateComparator,
        //     renderCell: (params) => (
        //         <div style={{display: 'flex', alignItems: 'center', justifyContent: 'center', whiteSpace: 'normal', wordBreak: 'break-word'}}>
        //             {params.value}
        //         </div>
        //         // <div>
        //         //     {getDateString(params.row.startDate,false)}
        //         // </div>
        //     ),
        // },
        // {
        //     id: 'firstname',
        //     field: 'firstname',
        //     headerName: 'First Name',
        //     // sortComparator: dateComparator,
        //     flex: 2,
        //     renderCell: (params) => (
        //         <div style={{display: 'flex', alignItems: 'center', justifyContent: 'center', whiteSpace: 'normal', wordBreak: 'break-word'}}>
        //             {params.value}
        //         </div>
        //         // <div>
        //         //     {getDateString(params.row.applicationDeadline,false)}
        //         // </div>
        //     ),
        // },
        {
            id: 'email',
            field: 'email',
            headerName: 'Email',
            flex: 1.5,
            renderCell: (params) => {
                return (
                    <div style={{display: 'flex', alignItems: 'center', justifyContent: 'center', whiteSpace: 'normal', wordBreak: 'break-word'}}>
                        {params.value}
                    </div>
                );
            }
        },
        {
            id: 'phone1',
            field: 'phone1',
            headerName: 'Phone No.',
            flex: 1,
            renderCell: (params) => {
                return (
                    <div style={{display: 'flex', alignItems: 'center', justifyContent: 'center', whiteSpace: 'normal', wordBreak: 'break-word'}}>
                        {params.value}
                    </div>
                );
            }
        },
        {
            id: 'createdByName',
            field: 'createdByName',
            headerName: 'Created By',
            flex: 1.5,
            renderCell: (params) => {
                return (
                    <div style={{display: 'flex', alignItems: 'center', justifyContent: 'center', whiteSpace: 'normal', wordBreak: 'break-word'}}>
                        {params.value}
                    </div>
                );
            }
        },
        {
            id: 'remarks',
            field: 'remarks',
            headerName: 'Remarks',
            flex: 3,
            renderCell: (params) => {
                return (
                    <div style={{display: 'flex', alignItems: 'center', justifyContent: 'center', whiteSpace: 'normal', wordBreak: 'break-word'}}>
                        {params.value}
                    </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
        />
    );
}
