// material-ui
import * as React from 'react';
import * as DateUtils from "utils/DateUtils";
import {PAYMENT_LIST} from "utils/ApiPathConst";
import * as FormatUtils from "utils/FormatUtils"
import * as PaymentStatus from "utils/statusUtils/PaymentStatus"
import { useNavigate } from "react-router-dom";
import { FiDataGrid } from "components/FiDataGrid";
import { clickableLink } from 'utils/CommonFunction';
// ==============================|| EVENT TABLE ||============================== //

export default function SearchPaymentTable({ searchCriteria }) {
    const [_searchCriteria, set_searchCriteria] = React.useState(searchCriteria);
    const navigate = useNavigate()

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

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

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

    const columns = [
        {
            id: 'appNos',
            field: 'appNos',
            headerName: 'Application No.',
            flex: 1,
            minWidth: 150,
            renderCell: (params) => {
                let appNo = params.row.appNos;
                return <div style={{ marginTop: 2, marginBottom: 2 }}>{appNo}</div>
            },
        },
        {
            field: 'actions',
            headerName: 'Transaction No.',
            flex: 1,
            minWidth: 200,
            cellClassName: 'actions',
            renderCell: (params) => {
                return clickableLink('/paymentPage/details/' + params.row.id, params.row.transNo);
            },
        },
        {
            id: 'transDateTime',
            field: 'transDateTime',
            headerName: 'Transaction Date',
            flex: 1,
            minWidth: 150,
            valueGetter: (params) => {
                return DateUtils.dateStr(params?.value);
            }
        },
        {
            field: 'action',
            headerName: 'Status',
            width: 150,
            renderCell: (params) => {
                return PaymentStatus.getStatus_Eng(params);
            }
        },
        {
            id: 'payAmount',
            field: 'payAmount',
            headerName: 'Amount ($)',
            width: 150,
            valueGetter: (params) => {
                return (params?.value) ? "$ " + FormatUtils.currencyFormat(params?.value) : "";
            }
        },
    ];

    return (
        <div style={{ width: '100%' }}>
            <FiDataGrid
                sx={_sx}
                rowHeight={80}
                columns={columns}
                customPageSize={10}
                onRowDoubleClick={handleEditClick}
                doLoad={{
                    url:PAYMENT_LIST,
                    params:_searchCriteria,
                }}
            />
        </div>
    );
}
