// material-ui
import * as React from 'react';
import {
    useMediaQuery
} from '@mui/material';
import * as DateUtils from "utils/DateUtils";
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 {useTheme} from "@emotion/react";
import {useIntl} from "react-intl";
import { clickableLink } from 'utils/CommonFunction';
import { getPaymentMethodByCode } from "auth/utils";
import {PAYMENT_LIST} from "utils/ApiPathConst";
// ==============================|| EVENT TABLE ||============================== //

export default function SearchPublicNoticeTable({ searchCriteria, applyGridOnReady, applySearch }) {
    const navigate = useNavigate()
    const theme = useTheme();
    const isMdOrLg = useMediaQuery(theme.breakpoints.up('md'));
    const intl = useIntl();
    const { locale } = intl;
    
    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 [_searchCriteria, set_searchCriteria] = React.useState(searchCriteria);

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

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

    const columns = [
        {
            field: 'actions',
            headerName: intl.formatMessage({id: 'payId'}),
            width: isMdOrLg ? 'auto' : 160,
            flex: isMdOrLg ? 1 : undefined,
            cellClassName: 'actions',
            renderCell: (params) => {
                return clickableLink('/paymentPage/details/' + params.row.id, params.row.transNo);
            },
        },
        {
            field: 'egisRefNo',
            headerName: intl.formatMessage({id: 'paymentRefCode'}),
            width: isMdOrLg ? 'auto' : 160,
            flex: isMdOrLg ? 1 : undefined,
        },
        {
            id: 'appNos',
            field: 'appNos',
            headerName: intl.formatMessage({id: 'applicationId'}),
            width: isMdOrLg ? 'auto' : 160,
            flex: isMdOrLg ? 1 : undefined,
            renderCell: (params) => {
                let appNo = params.row.appNos;
                return <div style={{ margin: 4 }}>{appNo}</div>
            },
        },
        {
            field: 'payMethod',
            headerName: intl.formatMessage({id: 'paymentMethod'}),
            width: isMdOrLg ? 'auto' : 160,
            flex: isMdOrLg ? 1 : undefined,
            renderCell: (params) => getPaymentMethodByCode(params?.value),
        },
        {
            id: 'transDateTime',
            field: 'transDateTime',
            headerName: intl.formatMessage({id: 'payDate'}),
            width: isMdOrLg ? 'auto' : 160,
            flex: isMdOrLg ? 1 : undefined,
            valueGetter: (params) => {
                return params?.value ? DateUtils.dateStr(params.value) : "";
            }
        },
        {
            field: 'action',
            headerName: intl.formatMessage({id: 'status'}),
            width: 150,
            renderCell: (params) => {
                return locale === 'en' ?
                    PaymentStatus.getStatus_Eng(params)
                        :
                    PaymentStatus.getStatus_Cht(params);
            }
        },
        {
            id: 'payAmount',
            field: 'payAmount',
            headerName: intl.formatMessage({id: 'currencyAmount'}),
            width: 150,
            valueGetter: (params) => {
                return (params?.value) ? "$ " + FormatUtils.currencyFormat(params?.value) : "";
            }
        },
    ];

    return (
        <div style={{ minHeight: 400, width: '100%' }}>
            <FiDataGrid
                sx={_sx}
                rowHeight={80}
                columns={columns}
                customPageSize={10}
                onRowDoubleClick={handleEditDoubleClick}
                applyGridOnReady={applyGridOnReady}
                applySearch={applySearch}
                // doLoad={{
                //     url: PAYMENT_LIST,
                //     params: _searchCriteria,
                // }}
                doLoad={React.useMemo(() => ({
                    url: PAYMENT_LIST,
                    params: _searchCriteria,
                }), [_searchCriteria])}
            />
        </div>
    );
}
