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

export default function SearchPublicNoticeTable({searchCriteria, applyGridOnReady,applySearch}) {

    const navigate = useNavigate()

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

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

    const columns = [
        {
            field: 'actions',
            headerName: 'Proof No.',
            width: 170,
            cellClassName: 'actions',
            renderCell: (params) => {
                return clickableLink('/proof/reply/' + params.row.id, params.row.refNo);
            },
        },
        {
            id: 'appId',
            field: 'appId',
            headerName: 'Application No./ Gazette Code/ Gazette Issue No.',
            width: 400,
            renderCell: (params) => {
                let appNo = params.row.appNo;
                let code = params.row.groupNo;
                let isssue = 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)");

                return <div style={{ margin: 4 }}>App No: {appNo}<br />Gazette Code: {code}<br />Issue: {isssue}</div>
            },
        },
        {
            id: 'proofStatus',
            field: 'proofStatus',
            headerName: 'Status',
            flex: 1,
            minWidth: 150,
            sortable: false,
            filterable: false,
            valueGetter: () => '',
            renderCell: (params) => {
                return ProofStatus.getStatus_Eng(params);
            },
        },
        {
            id: 'created',
            field: 'created',
            headerName: 'Proof Issue Date',
            flex: 1,
            minWidth: 200,
            // sorting/filtering uses this value
            valueGetter: (params) => DateUtils.toDate(params?.value),

            // display uses this (params.value is the *Date* returned above)
            valueFormatter: (params) => {
                const d = params.value;               // Date or Invalid Date
                return d instanceof Date && !isNaN(d.getTime())
                ? DateUtils.dateStr(d)
                : "";
            },

            // make sorting 100% deterministic
            sortComparator: (v1, v2) => {
                const t1 = v1 instanceof Date && !isNaN(v1.getTime()) ? v1.getTime() : -Infinity;
                const t2 = v2 instanceof Date && !isNaN(v2.getTime()) ? v2.getTime() : -Infinity;
                return t1 - t2;
            }
        },
        {
            id: 'replyDate',
            field: 'replyDate',
            headerName: 'Confirmed/ Return Date',
            flex: 1,
            minWidth: 200,
            // sorting/filtering uses this value
            valueGetter: (params) => DateUtils.toDate(params?.value),

            // display uses this (params.value is the *Date* returned above)
            valueFormatter: (params) => {
                const d = params.value;               // Date or Invalid Date
                return d instanceof Date && !isNaN(d.getTime())
                ? DateUtils.dateStr(d)
                : "";
            },

            // make sorting 100% deterministic
            sortComparator: (v1, v2) => {
                const t1 = v1 instanceof Date && !isNaN(v1.getTime()) ? v1.getTime() : -Infinity;
                const t2 = v2 instanceof Date && !isNaN(v2.getTime()) ? v2.getTime() : -Infinity;
                return t1 - t2;
            }
        },
        {
            id: 'contactPerson',
            field: 'contactPerson',
            headerName: 'Client',
            flex: 1,
            minWidth: 200,
            renderCell: (params) => {
                let company = params.row.enCompanyName != null?params.row.enCompanyName: params.row.chCompanyName;
                company = company != null ? company : "";
                if (company == "GLD"){
                    company = company + ": " + params.row.appCustName
                }
                return (<>
                    {params?.value}<br />{company}
                </>);
            }
        },
        {
            id: 'groupTitle',
            field: 'groupTitle',
            headerName: 'Gazette Group',
            flex: 1,
            minWidth: 200,
            valueGetter: (params) => {
                return (params?.value) ? (params?.value) : "";
            }
        },
        {
            id: 'fee',
            field: 'fee',
            headerName: 'Amount ($)',
            flex: 1,
            minWidth: 200,
            valueGetter: (params) => {
                return (params?.value) ? "$ " + FormatUtils.currencyFormat(params?.value) : "";
            }
        },
    ];

    function handleRowDoubleClick(params) {
        navigate('/proof/reply/' + params.row.id);
    }

    return (
        <div style={{ width: '100%' }}>
            <FiDataGrid
                columns={columns}
                customPageSize={10}
                getRowHeight={() => 'auto'}
                onRowDoubleClick={handleRowDoubleClick}
                applyGridOnReady={applyGridOnReady}
                applySearch = {applySearch}
                // doLoad={{
                //     url: LIST_PROOF,
                //     params: _searchCriteria,
                // }}
                doLoad={React.useMemo(() => ({
                    url: LIST_PROOF,
                    params: _searchCriteria,
                }), [_searchCriteria])}
            />
        </div>
    );
}
