// 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 ProofStatus from "utils/statusUtils/ProofStatus";
import { LIST_PROOF } from "utils/ApiPathConst";
import { useNavigate } from "react-router-dom";
import { FiDataGrid } from "components/FiDataGrid";
import {
    isORGLoggedIn,
    isDummyLoggedIn,
} from "utils/Utils";
import { useTheme } from "@emotion/react";
import { useIntl } from "react-intl";
import { clickableLink } from 'utils/CommonFunction';
// ==============================|| 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 [_searchCriteria, set_searchCriteria] = React.useState(searchCriteria);

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

    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"
        },
        "& .MuiDataGrid-columnHeaderTitle": {
            whiteSpace: "normal",
            lineHeight: "normal"
        },
        "& .MuiDataGrid-columnHeader": {
            // Forced to use important since overriding inline styles
            height: "unset !important"
        },
    }


    /*eslint no-irregular-whitespace: ["error", { "skipComments": true }]*/
    /*const getGroupTitle = (title) => {
        switch (title) {
            case 'Private Bill':
                return "私人帳單"; 
            case 'Companies Ordinance':
                return "公司條例"; 
            case 'High Court':
                return "高等法院"; 
            case 'Notices':
                return "通知"; 
            case 'Miscellaneous (Companies)':
                return "其他"; 
            default:
                return title;
        }
    }*/

    const getIssueLabel = (data) => {
        let issueYear = data.row.issueYear
        let volume = data.row.issueVolume;
        let issueNo = data.row.issueNo;
        let issueDate = data.row.issueDate;
        if (locale === 'zh-HK') {
            return issueYear
                + " 第" + volume + "卷,"
                + " 第" + issueNo + "期,"
                + " " + DateUtils.dateFormat(issueDate, "YYYY年MM月DD日")
                + " (" + DateUtils.getWeekdayStr_ZH(issueDate) + ")";
        } else if (locale === 'zh-CN') {
            return issueYear
                + " 第" + volume + "卷,"
                + " 第" + issueNo + "期,"
                + " " + DateUtils.dateFormat(issueDate, "YYYY年MM月DD日")
                + " (" + DateUtils.getWeekdayStr_CN(issueDate) + ")";
        }
        return issueYear
            + " Vol. " + FormatUtils.zeroPad(volume, 3)
            + ", No. " + FormatUtils.zeroPad(issueNo, 2)
            + ", " + DateUtils.dateFormat(issueDate, "D MMM YYYY (ddd)");
    }


    const renderHeaderWithAria = (params) => (
        <span aria-label={params.colDef.headerName}>{params.colDef.headerName}</span>
    );

    const columns = [
        {
            field: 'actions',
            headerName: intl.formatMessage({ id: 'proofId' }),
            width: isMdOrLg ? 'auto' : 200,
            flex: isMdOrLg ? 1.5 : undefined,
            cellClassName: 'actions',
            renderHeader: renderHeaderWithAria,
            renderCell: (params) => {
                return clickableLink('/proof/reply/' + params.row.id, params.row.refNo);
            },
        },
        {
            id: 'appId',
            field: 'appId',
            headerName: isORGLoggedIn() ? intl.formatMessage({ id: 'gazetteCount3' }) : intl.formatMessage({ id: 'gazetteCount2' }),
            width: isMdOrLg ? 'auto' : 330,
            flex: isMdOrLg ? 2 : undefined,
            renderHeader: renderHeaderWithAria,
            renderCell: (params) => {
                // let appNo = params.row.appNo;
                // let code = params.row.groupNo;
                let isssue = getIssueLabel(params);

                // return <div style={{ margin: 4 }}>{appNo}<br />{isssue}<br />{params.row.appRemarks}</div>
                return <div style={{ margin: 4 }}>{isssue}<br />{isORGLoggedIn() ? isDummyLoggedIn()?<><>GLD: {params.row.applicant}<br /></><>{params.row.appCareOf}<br /></></>:<>{params.row.appCareOf}<br /></> : null}{params.row.appRemarks}</div>
            },
        },
        {
            id: 'created',
            field: 'reviseDeadline',
            headerName: intl.formatMessage({ id: 'proofDate' }),
            width: isMdOrLg ? 'auto' : 200,
            flex: isMdOrLg ? 1.5 : undefined,
            renderHeader: renderHeaderWithAria,
            valueGetter: (params) => {
                return DateUtils.datetimeStr(params?.value);
            }
        },
        {
            id: 'proofPaymentDeadline',
            field: 'proofPaymentDeadline',
            headerName: intl.formatMessage({ id: 'replyBefore' }),
            width: isMdOrLg ? 'auto' : 200,
            flex: isMdOrLg ? 1.5 : undefined,
            renderHeader: renderHeaderWithAria,
            valueGetter: (params) => {
                const proofPaymentDeadline = DateUtils.convertToDate(params?.value);
                return DateUtils.datetimeStr(
                    proofPaymentDeadline,
                    'dd/MM/yyyy HH:mm:ss'
                );
            }
        },
        {
            id: 'replyDate',
            field: 'replyDate',
            headerName: intl.formatMessage({ id: 'replyDate' }),
            width: isMdOrLg ? 'auto' : 200,
            flex: isMdOrLg ? 1.5 : undefined,
            renderHeader: renderHeaderWithAria,
            valueGetter: (params) => {
                return params?.value ? DateUtils.datetimeStr(params?.value) : "";
            }
        },
        {
            field: 'proofStatus',
            headerName: intl.formatMessage({ id: 'status' }),
            width: isMdOrLg ? 'auto' : 160,
            flex: isMdOrLg ? 1 : undefined,
            renderHeader: renderHeaderWithAria,
            renderCell: (params) => {
                return locale === 'en' ? ProofStatus.getStatus_Eng(params) : locale === 'zh-HK' ? ProofStatus.getStatus_Cht(params) : ProofStatus.getStatus_Cn(params);
            },
        },
        {
            id: 'fee',
            field: 'fee',
            headerName: intl.formatMessage({ id: 'fee' }),
            width: isMdOrLg ? 'auto' : 160,
            flex: isMdOrLg ? 1 : undefined,
            renderHeader: renderHeaderWithAria,
            valueGetter: (params) => {
                return (params?.value) ? "$ " + FormatUtils.currencyFormat(params?.value) : "";
            }
        },
    ];

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

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

            <FiDataGrid
                sx={_sx}
                columns={columns}
                customPageSize={10}
                getRowHeight={() => "auto"}
                onRowDoubleClick={handleRowDoubleClick}
                applyGridOnReady={applyGridOnReady}
                applySearch={applySearch}
                doLoad={React.useMemo(() => ({
                    url: LIST_PROOF,
                    params: _searchCriteria,
                }), [_searchCriteria])}
            />
        </div>
    );
}