// material-ui
import * as React from 'react';
import {
    GridActionsCellItem,
} from "@mui/x-data-grid";

import {FiDataGrid} from "components/FiDataGrid";
import VisibilityIcon from '@mui/icons-material/Visibility';
import {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';

// ==============================|| EVENT TABLE ||============================== //

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

    const navigate = useNavigate()

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

    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: 'User Name',
            flex: 1,
        },
        {
            id: 'contactPerson',
            field: 'contactPerson',
            headerName: 'Name',
            flex: 1,
        },
        {
            id: 'enCompanyName',
            field: 'enCompanyName',
            headerName: 'Company(Eng)',
            flex: 1,
        },
        {
            id: 'chCompanyName',
            field: 'chCompanyName',
            headerName: 'Company(Ch)',
            flex: 1,
        },
        {
            id: 'brNo',
            field: 'brNo',
            headerName: 'Br No',
            flex: 1,
        },
        {
            id: 'lastLogin',
            field: 'lastLogin',
            headerName: 'Last Login',
            flex: 1,
            valueGetter:(params)=>{
                if(params.value){
                    return DateUtils.datetimeStr(params.value);
                }
                
            }
        },
        {
            id: 'locked',
            field: 'locked',
            headerName: 'Status',
            flex: 1,
            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=""
                        icon={<CheckCircleOutline/>}
                        color="success"
                    />];
                return [
                    <GridActionsCellItem 
                        key=""
                        icon={<HighlightOff/>}
                        color="error"
                    />];
            },
        },
    ];

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

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