// material-ui
import {
    // Box,
    Stack,
    // Button,
    Grid, Typography
} from '@mui/material';
import MainCard from "../../components/MainCard";
import { useEffect, useState } from "react";
import axios from "axios";
import { apiPath } from "../../auth/utils";
import { GET_GROUP_LIST_PATH } from "../../utils/ApiPathConst";
import * as React from "react";

//import LoadingComponent from "../extra-pages/LoadingComponent";
// import UserGroupSearchForm from "./UserGroupSearchForm";
// import UserGroupTable from "./UserGroupTable";
import Loadable from 'components/Loadable';
import { lazy } from 'react';
const LoadingComponent = Loadable(lazy(() => import('../extra-pages/LoadingComponent')));
const UserGroupSearchForm = Loadable(lazy(() => import('./UserGroupSearchForm')));
const UserGroupTable = Loadable(lazy(() => import('./UserGroupTable')));
// import {useNavigate} from "react-router";
import titleBackgroundImg from 'assets/images/dashboard/gazette-bar.png'

const BackgroundHead = {
    backgroundImage: `url(${titleBackgroundImg})`,
    width: '100%',
    height: '100%',
    backgroundSize: 'contain',
    backgroundRepeat: 'no-repeat',
    backgroundColor: '#0C489E',
    backgroundPosition: 'right'
}
// ==============================|| DASHBOARD - DEFAULT ||============================== //

const UserGroupSearchPanel = () => {
    const [record, setRecord] = useState([]);
    const [searchCriteria, setSearchCriteria] = useState({});
    const [onReady, setOnReady] = useState(false);
    // const navigate = useNavigate();

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

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

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

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

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

    return (
        !onReady ?
            <Grid container sx={{ minHeight: '95vh', mb: 3 }} direction="column" justifyContent="center" alignItems="center">
                <Grid item>
                    <LoadingComponent />
                </Grid>
            </Grid>
            :
            <Grid container sx={{ minHeight: '90vh', backgroundColor: "backgroundColor.default"}} direction="column">
                <Grid item xs={12}>
                    <div style={BackgroundHead}>
                        <Stack direction="row" height='70px' justifyContent="flex-start" alignItems="center">
                            <Typography ml={15} color='#FFF' variant="h4">
                                View User Group
                            </Typography>
                        </Stack>
                    </div>
                </Grid>

                {/*row 1*/}
                <Grid item xs={12} md={12} lg={12} sx={{mb:-1}}>
                    <UserGroupSearchForm applySearch={applySearch} />
                </Grid>
                {/*row 2*/}
                <Grid item xs={12} md={12} lg={12}>
                    <MainCard elevation={0}
                        border={false}
                        content={false}
                    >
                        <UserGroupTable
                            recordList={record}
                        />
                    </MainCard>
                </Grid>

            </Grid>

    );
};

export default UserGroupSearchPanel;
