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

export default function SearchPublicNoticeTable({ searchCriteria, setPaymentCount, setPublishCount  }) {
    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 handleEditClick = (params) => () => {
        window.open('/application/' + params.row.id);
    };

    const columns = [
        {
            id: 'appNo',
            field: 'appNo',
            headerName: 'App No.',
            flex: 1,
            minWidth: 150,
            renderCell: (params) => {
                return clickableLink('/application/' + params.row.id, params.row.appNo)
            },
        },
        {
            field: 'status',
            headerName: 'Status',
            flex: 1,
            minWidth: 200,
            renderCell: (params) => {
                return PublicNoteStatusUtils.getStatusByTextEng(params.row.status, params.row.creditor);
            }
        },
        {
            id: 'contactPerson',
            field: 'contactPerson',
            headerName: 'Client',
            flex: 3,
            minWidth: 300,
            renderCell: (params) => {
                let company = params.row.enCompanyName != null ? params.row.enCompanyName : params.row.chCompanyName;
                company = company != null ? company : "";
                return <div>
                    {params?.value}<br />{company}<br />Client Remark: {params.row.careOf}
                </div>;
            }
        },
        {
            id: 'groupNo',
            field: 'groupNo',
            headerName: 'Gazette',
            flex: 2,
            minWidth: 300,
            renderCell: (params) => {
                return <>{params.row.issueYear
                    + " Vol. " + FormatUtils.zeroPad(params.row.issueVolume, 3)
                    + ", No. " + FormatUtils.zeroPad(params.row.issueNo, 2)
                    + ", " + DateUtils.dateFormat(params.row.issueDate, "D MMM YYYY (ddd)")}
                    <br />{(params?.value)}</>;
            }
        },
        {
            id: 'fee',
            field: 'fee',
            headerName: 'Amount($)',
            flex: 1,
            minWidth: 100,
            valueGetter: (params) => {
                return FormatUtils.currencyFormat(params?.value);
            }
        },
    ];

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

            <FiDataGrid
                sx={_sx}
                getRowHeight={() => 'auto'}
                columns={columns}
                customPageSize={10}
                onRowDoubleClick={handleEditClick}
                doLoad={{
                    url: _searchCriteria.issueId>0?UrlUtils.DEMAND_NOTE_PREVIEW + "/" + _searchCriteria.issueId:null,
                    params: {},
                    callback: (responseData) => {
                        setPaymentCount(responseData.paymentCount);
                        setPublishCount(responseData.publishCount);
                    }
                }}
            />
        </div>
    );
}
