// material-ui
import * as React from 'react';
import * as FormatUtils from "utils/FormatUtils"
import {GFIMIS_LIST} from "utils/ApiPathConst";
import { useNavigate } from "react-router-dom";
import { FiDataGrid } from "components/FiDataGrid";

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

export default function SearchTable({ previewSearchCriteria, onPreviewGridOnReady,selectedIds = [], previewToken }) {
    // const [_searchCriteria, set_searchCriteria] = React.useState(previewSearchCriteria);
    const navigate = useNavigate()
    // const [rows, setRows] = React.useState([]);

    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"
        }
    }

    const doLoad = React.useMemo(() => {
        if (!selectedIds?.length) return undefined;
        return {
            url: GFIMIS_LIST,
            params: {
                ...previewSearchCriteria,
                paymentId: selectedIds.join(',')
            }
        };
    }, [previewSearchCriteria, selectedIds, previewToken]);


    // React.useEffect(() => {
    //     set_searchCriteria(previewSearchCriteria);
    // }, [previewSearchCriteria]);

    const handleEditClick = (params) => () => {
        navigate('/paymentPage/details/' + params.row.id);
    };

    const columns = [
        {
            id: 'paymentMethod',
            field: 'paymentMethod',
            headerName: 'GFMIS Payment Method',
            flex: 4,
            renderCell: (params) => {
                let paymentMethod = params.row.paymentMethod;
                return <div style={{ margin: 4 }}>{paymentMethod}</div>
            },
        },
        {
            id: 'payAmount',
            field: 'payAmount',
            headerName: 'Amount ($)',
            flex: 5,            
            valueGetter: (params) => {
                return (params?.value) ? "$ " + FormatUtils.currencyFormat(params?.value) : "";
            }
        },
    ];
    

    return (
        <div style={{ width: '100%' }}>
            <FiDataGrid
                key={previewToken}
                sx={_sx}
                rowHeight={80}
                columns={columns}
                customPageSize={10}
                onRowDoubleClick={handleEditClick}
                applyGridOnReady={onPreviewGridOnReady}
                doLoad={doLoad}
            />
        </div>
    );
}
