// 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 { useEffect } from "react";
import { useNavigate } from "react-router-dom";
import * as DateUtils from "utils/DateUtils";
// ==============================|| EVENT TABLE ||============================== //

export default function OrganizationTable({ recordList }) {
    const [rows, setRows] = React.useState(recordList);
    const navigate = useNavigate()

    useEffect(() => {
        setRows(recordList);
    }, [recordList]);

    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,
        },
        {
            id: 'enCompanyName',
            field: 'enCompanyName',
            headerName: 'Name (Eng)',
            flex: 1,
        },
        {
            id: 'chCompanyName',
            field: 'chCompanyName',
            headerName: 'Name (Ch)',
            flex: 1,
        },
        {
            id: 'contactTel',
            field: 'contactTel',
            headerName: 'Phone',
            flex: 1,
            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,
            valueGetter: (params) => {
                return DateUtils.dateStr(params?.value);
            }
        },
        {
            id: 'creditor',
            field: 'creditor',
            headerName: 'Creditor',
            width: 100,
            valueGetter: (params) => {
                return params?.value?"Yes":"";
            }
        },
    ];

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

    return (
        <div style={{ height: "fit-content", width: '100%' }}>
            <FiDataGrid
                rows={rows}
                columns={columns}
                customPageSize={5}
                onRowDoubleClick={handleRowDoubleClick}
            />
        </div>
    );
}
