// material-ui
import {
    Grid, Typography
} from '@mui/material';
import MainCard from "../../components/MainCard";
import SearchForm from "./UserSearchForm";
import EventTable from "./UserTable";
import {useEffect, useState} from "react";
import axios from "axios";
import {apiPath} from "../../auth/utils";
import {GET_USER_PATH} from "../../utils/ApiPathConst";
import * as React from "react";
import LoadingComponent from "../extra-pages/LoadingComponent";

// ==============================|| DASHBOARD - DEFAULT ||============================== //

const UserSettingPage = () => {

    const [record,setRecord] = useState([]);
    const [searchCriteria, setSearchCriteria] = useState({});
    const [onReady, setOnReady] = useState(false);

    useEffect(() => {
        getUserList();
    }, []);

    useEffect(() => {
        setOnReady(true);
    }, [record]);

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

    function getUserList(){
        axios.get(`${apiPath}${GET_USER_PATH}`,
            {params: searchCriteria}
        )
            .then((response) => {
                if (response.status === 200) {
                    setRecord(response.data);
                }
            })
            .catch(error => {
                console.log(error);
                return false;
            });
    }

    function applySearch(input) {
        setSearchCriteria(input);
    }

    return (
        !onReady ?
            <LoadingComponent/>
            :
        <Grid container rowSpacing={4.5} columnSpacing={2.75}>
            <Grid item xs={12} sx={{mb: -2.25}}>
                <Typography variant="h5">View User</Typography>
            </Grid>

            {/*row 1*/}
            <Grid item xs={12} md={12} lg={12}>
                <SearchForm applySearch={applySearch}/>
            </Grid>
            {/*row 2*/}
            <Grid item xs={12} md={12} lg={12}>
                <MainCard elevation={0}
                          border={false}
                          content={false}
                >
                    <EventTable
                        recordList={record}
                    />
                </MainCard>
            </Grid>

        </Grid>

    );
};

export default UserSettingPage;
