// material-ui
import * as React from 'react';
import * as DateUtils from "utils/DateUtils";
import { useNavigate } from "react-router-dom";
import { FiDataGrid } from "components/FiDataGrid";
import {useIntl} from "react-intl";
import { clickableLink } from 'utils/CommonFunction';
import {GET_MSG_LIST} from "utils/ApiPathConst";

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

export default function MsgTable({ searchCriteria, applyGridOnReady, applySearch}) {
    
    const navigate = useNavigate()
    const intl = useIntl();

    const [_searchCriteria, set_searchCriteria] = React.useState(searchCriteria);
    
    React.useEffect(() => {
        set_searchCriteria(searchCriteria);
    }, [searchCriteria]);

    const _sx = {
        padding: "4 2 4 2",
        boxShadow: 1,
        border: 1,
        borderColor: '#DDD',
        '& .MuiDataGrid-cell': {
            borderTop: 1,
            borderBottom: 1,
            borderColor: "#EEE"
        },
        '& .MuiDataGrid-footerContainer': {
            border: 1,
            borderColor: "#EEE"
        },
        "& .MuiDataGrid-columnHeaderTitle": {
            whiteSpace: "normal",
            lineHeight: "normal"
        },
        "& .MuiDataGrid-columnHeader": {
            // Forced to use important since overriding inline styles
            height: "unset !important"
        },
    }
    const columns = [
        {
            id: 'sentDate',
            field: 'sentDate',
            headerName: intl.formatMessage({id: 'date'}),
            width: 170,
            valueGetter: (params) => {
                return DateUtils.datetimeStr(params?.value);
            },
        },
        {
            field: 'subject',
            headerName: intl.formatMessage({id: 'subject'}),
            flex: 1 ,
            cellClassName: 'subject',
            renderCell: (params) => {
                return clickableLink('/msg/details/' + params.row.id, params.row.subject);
            },
        },
    ];

    function handleEditClick(params) {
        navigate('/msg/details/' + params.row.id);
    }

    return (
        <div style={{  width: '100%', overflowX: 'auto'}}>

            <FiDataGrid
                sx={_sx}
                columns={columns}
                customPageSize={10}
                getRowHeight={() => 'auto'}
                onRowDoubleClick={handleEditClick}
                applyGridOnReady={applyGridOnReady}
                applySearch={applySearch}
                doLoad={React.useMemo(() => ({
                    url: GET_MSG_LIST,
                    params: _searchCriteria,
                }), [_searchCriteria])}
            />
        </div>
    );
}
