// 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 HighlightOff from '@mui/icons-material/HighlightOff';
import CheckCircleOutline from '@mui/icons-material/CheckCircleOutline';
import { useEffect } from "react";
import { useNavigate } from "react-router-dom";
import * as DateUtils from "../../../utils/DateUtils";
// ==============================|| EVENT TABLE ||============================== //

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

    const navigate = useNavigate()

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

    const handleActionClick = (id) => () => {
        navigate('/indUser/' + 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: 'enName',
            field: 'enName',
            headerName: 'Name (Eng)',
            flex: 1,
        },
        {
            id: 'chName',
            field: 'chName',
            headerName: 'Name (Ch)',
            flex: 1,
        },
        {
            id: 'mobileNumber',
            field: 'mobileNumber',
            headerName: 'Phone',
            flex: 1,
            valueGetter: (params) => {
                if (params.value) {
                    let tel = JSON.parse(params.value);
                    if(tel?.phoneNumber)
                        return "+" + tel?.countryCode + " " + tel?.phoneNumber;
                    else return "";
                }

            }
        },
        {
            id: 'emailAddress',
            field: 'emailAddress',
            headerName: 'Email',
            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('/indUser/' + params.id);
    }

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