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

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

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

const SBUDivisionIndex = () => {

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

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

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

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

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

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

    return (
        !onReady ?
            <LoadingComponent/>
            :
            <Grid container rowSpacing={3} columnSpacing={2.75} sx={{marginTop: '0px'}}>
                <ThemeProvider theme={LIONER_FORM_THEME}>
                    <Grid item xs={12} md={12} lg={12}>
                        <Grid container justifyContent="flex-start" alignItems="center" >
                            <Grid item xs={3} sx={{mt:-2.25, mb: -2.25}} >
                                <Box sx={{ display: 'flex', alignItems: 'center'}}>
                                    <Typography align="center" variant="h5" >View SBU / Division</Typography>
                                </Box>
                            </Grid>
                        </Grid>
                    </Grid>

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

    );
};

export default SBUDivisionIndex;
