
import {
    GridActionsCellItem,
} from "@mui/x-data-grid";

import {FiDataGrid} from "components/FiDataGrid";
//import VisibilityIcon from '@mui/icons-material/Visibility';
import {useState, useEffect} from "react";
import {useNavigate} from "react-router-dom";
import HighlightOff from '@mui/icons-material/HighlightOff';
import CheckCircleOutline from '@mui/icons-material/CheckCircleOutline';
import * as DateUtils from 'utils/DateUtils';
import { GET_ORG_USER_PATH } from "utils/ApiPathConst";
import { clickableLink} from 'utils/CommonFunction';
import * as React from 'react';
// ==============================|| EVENT TABLE ||============================== //

export default function UserTable_Organization({searchCriteria, applyGridOnReady, applySearch}) {
    const [_searchCriteria, set_searchCriteria] = useState(searchCriteria);

    const navigate = useNavigate()

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

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

    const columns = [
        // {
        //     field: 'actions',
        //     type: 'actions',
        //     headerName: 'Actions',
        //     width: 100,
        //     cellClassName: 'actions',
        //     getActions: ({id}) => {
        //         return [
        //             <GridActionsCellItem
        //                 key="OutSave"
        //                 icon={<VisibilityIcon/>}
        //                 label="View"
        //                 className="textPrimary"
        //                 onClick={handleActionClick(id)}
        //                 color="primary"
        //             />]
        //     },
        // },
        {
            id: 'username',
            field: 'username',
            headerName: 'Username',
            flex: 1,
            minWidth: 150,
            renderCell: (params) => {
                return clickableLink('/orgUser/'+ params.row.id, params.row.username);
            },
        },
        {
            id: 'contactPerson',
            field: 'contactPerson',
            headerName: 'Name',
            flex: 1,
            minWidth: 150,
        },
        {
            id: 'enCompanyName',
            field: 'enCompanyName',
            headerName: 'Organisation (Eng)',
            flex: 1,
            minWidth: 200,
        },
        {
            id: 'chCompanyName',
            field: 'chCompanyName',
            headerName: 'Organisation (Ch)',
            flex: 1,
            minWidth: 150,
        },
        {
            id: 'brNo',
            field: 'brNo',
            headerName: 'BR No.',
            flex: 1,
            minWidth: 150,
        },
        {
            id: 'lastLogin',
            field: 'lastLogin',
            headerName: 'Last Login',
            flex: 1,
            minWidth: 200,
            valueGetter:(params)=>{
                if(params.value){
                    return DateUtils.datetimeStr(params.value);
                }
                
            }
        },
        {
            id: 'locked',
            field: 'locked',
            headerName: 'Status',
            flex: 1,
            minWidth: 100,
            valueGetter:(params)=>{
                if(params.value){
                    return "Locked";
                }else{
                    return "Active";
                }
                
            }
        },
        {
            field: 'verifiedDate',
            type: 'actions',
            headerName: 'Verified',
            width: 100,
            cellClassName: 'actions',
            getActions: (params) => {
                if(params.row.verifiedDate)
                return [
                    <GridActionsCellItem
                        key="verified"
                        icon={<CheckCircleOutline/>}
                        label="Verified"
                        color="success"
                    />];
                return [
                    <GridActionsCellItem 
                        key="not-verified"
                        icon={<HighlightOff/>}
                        label="Not verified"
                        color="error"
                    />];
            },
        },
    ];

    function handleRowDoubleClick(params) {
        navigate('/orgUser/'+ 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_USER_PATH,
                //     params: _searchCriteria,
                // }}
                doLoad={React.useMemo(() => ({
                    url: GET_ORG_USER_PATH,
                    params: _searchCriteria,
                }), [_searchCriteria])}
            />
        </div>
    );
}
