// material-ui
import {
    Button,
    Grid, 
    TextField,
    Autocomplete,
    Typography
} from '@mui/material';
import MainCard from "components/MainCard";
import { useForm } from "react-hook-form";
import * as React from "react";
// import * as DateUtils from "utils/DateUtils";
import {PNSPS_BUTTON_THEME} from "themes/buttonConst";
import {ThemeProvider} from "@emotion/react";
// import * as ComboData from "utils/ComboData";
// ==============================|| DASHBOARD - DEFAULT ||============================== //


const SearchGazetteIssueForm = ({ applyExport, comboData, waitDownload}) => {
    const [selectedYear, setSelectedYear] = React.useState([]);
    // const [defaultYear, setDefaultYear] = React.useState(searchCriteria.year);
    const [comboList, setComboList] = React.useState([]);
    // const [onReady, setOnReady] = React.useState(false);

    const { 
        // register, 
        handleSubmit } = useForm()

    const onSubmit = () => {
        // console.log(selectedYear)
        if (selectedYear !=null && Object.keys(selectedYear).length>0){
            // console.log("okkkkkkkkkkkkkkkk")
            const temp = {
                year: selectedYear.label,
            };
            applyExport(temp);
        }
    };

    React.useEffect(() => {
        if (comboData && comboData.length > 0) {
            // console.log(comboData)
            // const labelValue = comboData.find(obj => obj.label === searchCriteria.year);
            // console.log(labelValue)
            // if(selectedYear.length == 0){
            //     setSelectedYear(comboData[0])
            // }
            setComboList(comboData)
            // setSelectedYear(searchCriteria.dateFrom)
        }
    }, [comboData]);


    return (
        <MainCard xs={12} md={12} lg={12}
            border={false}
            content={false}
            >

            <form onSubmit={handleSubmit(onSubmit)} >
                <Grid container sx={{ backgroundColor: '#ffffff', ml: 2, mt: 1}} width="98%">
                    {/*row 1*/}
                    <Grid item justifyContent="space-between" alignItems="center" sx={{mt:1,ml:3,mb:1}}>
                        <Typography variant="h5" >
                            Export Preliminary Gazette Issue From Holiday Year
                        </Typography>
                    </Grid>
                    {/*row 2*/}

                    <Grid container display="flex" alignItems={"center"}>
                    <Grid item xs={9} s={6} md={4} lg={4} sx={{ ml: 3, mr: 3, mb: 3 }}>
                       <Autocomplete
                            disablePortal
                            id="year-combo"
                            value={selectedYear}
                            // defaultValue={selectedYear}
                            options={comboList}
                            // disabled={checkCountry}
                            getOptionLabel={(option) =>
                                option != null && typeof option === "object" && option.label != null
                                    ? String(option.label)
                                    : ""
                            }
                            onChange={(event, newValue) => {
                                setSelectedYear(newValue);
                            }}
                            sx={{
                                '& .MuiInputBase-root': { alignItems: 'center' },
                                '& .MuiAutocomplete-endAdornment': { top: '50%', transform: 'translateY(-50%)' },
                                '& .MuiOutlinedInput-root': { height: 40 }
                            }}
                            renderInput={(params) => <TextField {...params} placeholder={""}/>}
                        />
                    </Grid>

                    <Grid item xs={9} s={6} md={4} lg={4} sx={{ ml: 3, mr: 3, mb: 3 }}>
                        {/* <TextField
                            fullWidth
                            InputLabelProps={{
                                shrink: true
                            }}
                            {...register("dateTo")}
                            InputProps={{ inputProps: { min: minDate } }}
                            onChange={(newValue) => {
                                setMaxDate(DateUtils.dateValue(newValue));
                            }}
                            id="dateTo"
                            type="date"
                            label="To"
                            defaultValue={searchCriteria.dateTo}
                        /> */}
                    </Grid>

                    {/* <Grid item xs={9} s={6} md={4} lg={3}>
                    </Grid> */}
                </Grid>
                <Grid container justifyContent="center" direction="row"  alignItems="center" spacing={3}>
                    <ThemeProvider theme={PNSPS_BUTTON_THEME}>
                        <Grid item sx={{ ml: 3, mb: 3, }} >
                            <Button
                                variant="contained"
                                type="submit"
                                size="large"
                                disabled={waitDownload}
                            >
                                Export
                            </Button>
                        </Grid>
                    </ThemeProvider>
                </Grid>
                </Grid>
            </form>
        </MainCard>
    );
};

export default SearchGazetteIssueForm;
