// material-ui
import {
    Box,
    Grid, Typography
} from '@mui/material';
import MainCard from "../../components/MainCard";
import SearchForm from "./UserSearchForm";
import UserTable 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";

import {LIONER_FORM_THEME, CARD_MAX_WIDTH} from "../../themes/themeConst";
import {ThemeProvider} from "@emotion/react";

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

const UserSettingPage = () => {

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

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

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

    function getUserList(){
        axios.get(`${apiPath}${GET_USER_PATH}`,
            {params: searchCriteria}
        )
            .then((response) => {
                if (response.status === 200) {
                    const suId = response.data.find(item => item.groupId == 3)?.id;
                    const res = response.data.filter(item => item?.id !== suId);

                    setRecord(res);
                    setOnReady(true);
                }
            })
            .catch(error => {
                console.log(error);
                return false;
            });
    }

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

    return (
        <Grid container rowSpacing={3} columnSpacing={2.75}>
            <ThemeProvider theme={LIONER_FORM_THEME}>
                <Grid item xs={12} md={12} lg={12} sx={{mt:-1}}>
                    <Grid container justifyContent="flex-start" alignItems="center">
                        <Grid item xs={12} sx={{mb: -2.25}} >
                            <Box sx={{ display: 'flex', alignItems: 'center'}}>
                                <Typography align="center" variant="h4">View User</Typography>
                            </Box>
                        </Grid>
                    </Grid>
                </Grid>

            {/*row 1*/}
            <Grid item xs={12} md={12} lg={12}>
                <SearchForm applySearch={applySearch}/>
            </Grid>
            {/*row 2*/}
            {!onReady ?
            <LoadingComponent/>
            :
            <Grid item xs={12} md={12} lg={12} sx={{mt:{lg:-1.5}}}>
                <MainCard elevation={0}
                          content={false}
                          sx={{width:CARD_MAX_WIDTH}}
                >
                    <UserTable
                        recordList={record}
                    />
                </MainCard>
            </Grid>}
            </ThemeProvider>
        </Grid>

    );
};

export default UserSettingPage;
