// material-ui
import * as React from 'react';
// import {
//     GridActionsCellItem,
// } from "@mui/x-data-grid";
import { FiDataGrid } from "components/FiDataGrid";
//import EditIcon from '@mui/icons-material/Visibility';
import { useNavigate } from "react-router-dom";
import * as DateUtils from "utils/DateUtils";
import { clickableLink} from 'utils/CommonFunction';
import {GET_ORG_PATH} from "utils/ApiPathConst";
// ==============================|| EVENT TABLE ||============================== //

export default function OrganizationTable({ searchCriteria, applyGridOnReady, applySearch}) {
    const [_searchCriteria, set_searchCriteria] = React.useState(searchCriteria);
    const navigate = useNavigate()

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

    // const handleActionClick = (id) => () => {
    //     navigate('/org/' + id);
    // };

    const columns = [
        // {
        //     field: 'actions',
        //     type: 'actions',
        //     headerName: 'Actions',
        //     width: 100,
        //     cellClassName: 'actions',
        //     getActions: ({ id }) => {
        //         return [
        //             <GridActionsCellItem
        //                 key="OutSave"
        //                 icon={<EditIcon />}
        //                 label="Edit"
        //                 className="textPrimary"
        //                 onClick={handleActionClick(id)}
        //                 color="primary"
        //             />]
        //     },
        // },
        {
            id: 'brNo',
            field: 'brNo',
            headerName: 'BR No.',
            flex: 1,
            minWidth: 150,
            renderCell: (params) => {
                return clickableLink('/org/'+ params.row.id, params.row.brNo);
            },
        },
        {
            id: 'enCompanyName',
            field: 'enCompanyName',
            headerName: 'Name (Eng)',
            flex: 1,
            minWidth: 200,
        },
        {
            id: 'chCompanyName',
            field: 'chCompanyName',
            headerName: 'Name (Ch)',
            flex: 1,
            minWidth: 150,
        },
        {
            id: 'contactTel',
            field: 'contactTel',
            headerName: 'Phone',
            flex: 1,
            minWidth: 150,
            renderCell: (params) => {
                let phone = JSON.parse(params.value);
                let contact = "";
                if (phone && phone.phoneNumber) {
                    contact = phone?.countryCode + " " + phone?.phoneNumber
                }
                return contact;
            }
        },
        {
            id: 'brExpiryDate',
            field: 'brExpiryDate',
            headerName: 'BR Expiry Date',
            flex: 1,
            minWidth: 150,
            valueGetter: (params) => {
                return DateUtils.dateValue(params?.value);
            }
        },
        {
            id: 'creditor',
            field: 'creditor',
            headerName: 'Credit Client',
            width: 150,
            minWidth: 150,
            valueGetter: (params) => {
                return params?.value?"Yes":"";
            }
        },
    ];

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

    return (
        <div style={{ height: "fit-content", width: '100%' }}>
            <FiDataGrid
                columns={columns}
                customPageSize={10}
                onRowDoubleClick={handleRowDoubleClick}
                applyGridOnReady={applyGridOnReady}
                applySearch={applySearch}
                // doLoad={{
                //     url: GET_ORG_PATH,
                //     params: _searchCriteria,
                // }}
                doLoad={React.useMemo(() => ({
                    url: GET_ORG_PATH,
                    params: _searchCriteria,
                }), [_searchCriteria])}
            />
        </div>
    );
}
