// material-ui
import {useState, useEffect} from 'react';
import {
    Button,
    Typography, useMediaQuery
} from '@mui/material';
import * as DateUtils from "utils/DateUtils";
import * as StatusUtils from "utils/statusUtils/PublicNoteStatusUtils";
import { useNavigate } from "react-router-dom";
import { FiDataGrid } from "components/FiDataGrid";
import {
    isORGLoggedIn,
    isDummyLoggedIn,
} from "utils/Utils";
import {GET_PUBLIC_NOTICE_LIST} from "utils/ApiPathConst";
import {getModeIntl } from "utils/statusUtils/PublicNoteStatusUtils";
import { useTheme } from "@emotion/react";
import { FormattedMessage, useIntl } from "react-intl";
import * as React from 'react';
// ==============================|| 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 [_searchCriteria, set_searchCriteria] = useState(searchCriteria);
    useEffect(() => {
        set_searchCriteria(searchCriteria);
    }, [searchCriteria]);


    const handleDetailClick = (params) => () => {
        navigate('/publicNotice/' + params.id);
    };

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

    const columns = [
        {
            id: 'appNo',
            field: 'appNo',
            headerName: intl.formatMessage({ id: 'applicationId' }),
            width: isMdOrLg ? 'auto' : 160,
            flex: isMdOrLg ? 1 : undefined,
            renderHeader: renderHeaderWithAria,
            renderCell: (params) => {
                return params.row.appNo + getModeIntl(params, intl);
            },
        },
        {
            id: 'created',
            field: 'created',
            headerName: intl.formatMessage({ id: 'submitDate' }),
            width: isMdOrLg ? 'auto' : 160,
            flex: isMdOrLg ? 1 : undefined,
            renderHeader: renderHeaderWithAria,
            valueGetter: (params) => {
                return DateUtils.datetimeStr(params?.value);
            }
        },
        // {
        //     id: 'contactPerson',
        //     field: 'contactPerson',
        //     headerName: '聯絡人',
        //     flex: 2,
        //     renderCell: (params) => {
        //         let phone = JSON.parse(params.row.contactTelNo);
        //         let faxNo = JSON.parse(params.row.contactFaxNo);

        //         let contact = "";
        //         if (phone) {
        //             contact = "電話: " + phone?.countryCode + " " + phone?.phoneNumber
        //         }

        //         if (faxNo && faxNo?.faxNumber) {
        //             if (contact != "")
        //                 contact = contact + ", "
        //             contact = contact + "傳真:" + faxNo?.countryCode + " " + faxNo?.faxNumber
        //         }

        //         return (<>
        //             {params?.value}<br />
        //             {contact}
        //         </>);
        //     }
        // },
        {
            id: 'remarks',
            field: 'remarks',
            headerName: isORGLoggedIn() ? intl.formatMessage({ id: 'gazetteCount2_1' }) : intl.formatMessage({ id: 'myRemarks' }),
            width: isMdOrLg ? 'auto' : 400,
            flex: isMdOrLg ? 3 : undefined,
            renderHeader: renderHeaderWithAria,
            renderCell: (params) => (
                isORGLoggedIn() ?
                    isDummyLoggedIn()?
                        <div>
                            <FormattedMessage id="gazetteCount" />: {params.row.issueVolume + "/" + params.row.issueYear
                                + " No. " + params.row.issueNo}<br />
                            GLD: {params.row.custName} <br />
                            <FormattedMessage id="careOf" />: {params.row.careOf} <br />
                            <FormattedMessage id="myRemarks" />: {params.row.remarks}
                        </div>:
                        <div>
                            <FormattedMessage id="gazetteCount" />: {params.row.issueVolume + "/" + params.row.issueYear
                                + " No. " + params.row.issueNo}<br />
                            <FormattedMessage id="careOf" />: {params.row.careOf} <br />
                            <FormattedMessage id="myRemarks" />: {params.row.remarks}
                        </div> 
                    :
                    <div>
                        <FormattedMessage id="gazetteCount" />: {params.row.issueVolume + "/" + params.row.issueYear
                            + " No. " + params.row.issueNo}<br />
                        <Typography variant="pnspsNormalText">{params.row.remarks}</Typography>
                    </div>
            )
        },
        {
            id: 'status',
            field: 'status',
            headerName: intl.formatMessage({ id: 'status' }),
            width: 200,
            renderHeader: renderHeaderWithAria,
            renderCell: (params) => {
                return StatusUtils.getStatusIntl(params, intl);
            },
        },
        {
            field: 'actions',
            type: 'actions',
            headerName: '',
            width: 150,
            renderHeader: renderHeaderWithAria,
            cellClassName: 'actions',
            renderCell: (params) => {
                return <Button onClick={handleDetailClick(params)}
                    aria-label={intl.formatMessage({ id: 'viewDetail' })}
                >
                    <FormattedMessage id="viewDetail" />
                </Button>;
            },
        }
    ];

    function handleRowDoubleClick(params) {
        navigate('/publicNotice/' + params.id);
    }

    return (
        <div style={{ minHeight: 400, height: "100%", width: '100%' }}>
            <FiDataGrid
                columns={columns}
                customPageSize={10}
                getRowHeight={() => 'auto'}
                onRowDoubleClick={handleRowDoubleClick}
                applyGridOnReady = {applyGridOnReady}
                applySearch={applySearch}
                tab={"application"}
                doLoad={React.useMemo(() => ({
                    url: GET_PUBLIC_NOTICE_LIST,
                    params: _searchCriteria,
                    applyGridOnReady: applyGridOnReady,
                    // callback: function (responseData) {
                    //     setRows(responseData?.records);
                    //     // applyGridOnReady(false)
                    // }
                }), [_searchCriteria])}
            />
        </div>
    );
}
