// material-ui
import {
    Grid,
    Typography,
    Stack,
} from '@mui/material';
import MainCard from "components/MainCard";

import * as React from "react";
import * as UrlUtils from "utils/ApiPathConst";
import * as HttpUtils from "utils/HttpUtils";
import * as DateUtils from "utils/DateUtils";

import Loadable from 'components/Loadable';
const LoadingComponent = Loadable(React.lazy(() => import('pages/extra-pages/LoadingComponent')));
const SearchForm = Loadable(React.lazy(() => import('./SearchForm')));
const EventTable = Loadable(React.lazy(() => import('./DataGrid')));
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 UserSearchPage_Individual = () => {

    const [record, setRecord] = React.useState([]);
    const [orgCombo, setOrgCombo] = React.useState([]);
    const [issueCombo, setIssueCombo] = React.useState([]);
    const [searchCriteria, setSearchCriteria] = React.useState({
        dateTo: DateUtils.dateStr(new Date()),
        dateFrom: DateUtils.dateStr(new Date().setDate(new Date().getDate() - 14)),
    });
    const [onReady, setOnReady] = React.useState(false);

    React.useEffect(() => {
        getUserList();
        getOrgCombo();
        getIssueCombo();
    }, []);

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

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

    function getUserList() {
        HttpUtils.get({
            url: UrlUtils.DEMAND_NOTE_LIST,
            params: searchCriteria,
            onSuccess: function (responseData) {
                setRecord(responseData);
            }
        });
    }

    function getOrgCombo() {
        HttpUtils.get({
            url: UrlUtils.GET_ORG_COMBO,
            onSuccess: function (responseData) {
                let combo = responseData;
                setOrgCombo(combo);
            }
        });
    }

    function getIssueCombo() {
        HttpUtils.get({
            url: UrlUtils.GET_ISSUE_COMBO,
            onSuccess: function (responseData) {
                let combo = responseData;
                setIssueCombo(combo);
            }
        });
    }


    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: '95vh', 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">
                                Demand Note
                            </Typography>
                        </Stack>
                    </div>
                </Grid>
                {/*row 1*/}
                <Grid item xs={12} md={12} lg={12} sx={{mb: -1}}>
                    <SearchForm
                        applySearch={applySearch}
                        orgComboData={orgCombo}
                        issueComboData={issueCombo}
                        searchCriteria={searchCriteria}
                        
                    />
                </Grid>
                {/*row 2*/}
                <Grid item xs={12} md={12} lg={12}>
                    <MainCard elevation={0}
                        border={false}
                        content={false}
                        sx={{ backgroundColor: '#fff' }}
                    >
                        <EventTable
                            recordList={record}
                            reloadFun={getUserList}
                            applySearch={applySearch}
                        />
                    </MainCard>
                </Grid>
               
            </Grid>
    );
}
export default UserSearchPage_Individual;
