// 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/Edit';
import {useEffect} from "react";
import {useNavigate} from "react-router-dom";
import { useTheme } from '@mui/material/styles';
import Checkbox from '@mui/material/Checkbox';
import * as UrlUtils from "../../../utils/ApiPathConst";
import * as HttpUtils from '../../../utils/HttpUtils';
import { notifyLockSuccess, notifyUnlockSuccess } from 'utils/CommonFunction';

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

export default function UserTable({recordList,setChangeLocked}) {
    const [rows, setRows] = React.useState(recordList);
    const theme = useTheme();

    const navigate = useNavigate()

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

    const handleEditClick = (id) => () => {
        navigate('/user/'+ id);
    };

    const handleLock = (params) => () => {
        setChangeLocked(false)
        if (params.row.locked==true){
            doUnlock(params.id)
        }else{
            doLock(params.id)
            setRows(recordList);
        }
    }

    const doLock = (id) => {
        HttpUtils.get({
            url: UrlUtils.GET_USER_LOCK+"/"+id,
            onSuccess: function(){
                setRows(recordList);
                setChangeLocked(true)
                notifyLockSuccess()
            }
        }); 
    };

    const doUnlock = (id) => {
        HttpUtils.get({
            url: UrlUtils.GET_USER_UNLOCK+"/"+id,
            onSuccess: function(){
                setRows(recordList);
                setChangeLocked(true)
                notifyUnlockSuccess()
            }
        }); 
    };

    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={handleEditClick(id)}
                        color="primary"
                    />]
            },
        },
        {
            id: 'username',
            field: 'username',
            headerName: 'User Name',
            flex: 1,
        },
        {
            id: 'enName',
            field: 'enName',
            headerName: 'Full Name',
            flex: 1,
        },
        {
            id: 'post',
            field: 'post',
            headerName: 'Post',
            flex: 1,
        },
        {
            id: 'emailAddress',
            field: 'emailAddress',
            headerName: 'Email',
            flex: 1,
        },

        {
            id: 'locked',
            field: 'locked',
            type: 'bool',
            headerName: 'Locked',
            flex: 1,
            renderCell: (params) => {
                return (
                    <Checkbox
                        theme={theme}
                        key="locked"
                        checked={params.row.locked}
                        color="primary"
                        size="small"
                        onClick={handleLock(params)}
                    />
                );
            },
        },
    ];

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

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