// material-ui
import {
    Button,
    CardContent,
    Grid, TextField
} from '@mui/material';
import MainCard from "../../components/MainCard";
import {useForm} from "react-hook-form";

import { useState} from "react";
import * as React from "react";
// ==============================|| DASHBOARD - DEFAULT ||============================== //


const OrganizationSearchForm = ({applySearch}) => {

    const [type, setType] = useState([]);

    const { reset, register, handleSubmit } = useForm()
    const onSubmit = (data) => {

        let typeArray = [];

        for(let i =0; i < type.length; i++){
            typeArray.push(type[i].label);
        }

        const temp = {
            brNo: data.brNo,
            enCompanyName: data.enCompanyName,
            chCompanyName: data.chCompanyName,
        };
        applySearch(temp);
    };

    function resetForm(){
        setType([]);
        reset();
    }

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

            <form onSubmit={handleSubmit(onSubmit)}>
                {/*row 1*/}
                <CardContent sx={{ px: 2.5, pt: 3 }}>
                    <Grid item justifyContent="space-between" alignItems="center">
                        Search Form
                    </Grid>
                </CardContent>

                {/*row 2*/}
                <Grid container alignItems={"center"}>
                    <Grid item xs={9} s={6} md={5} lg={3} sx={{ml:3, mr:3, mb:3}}>
                        <TextField
                            fullWidth
                            {...register("brNo")}
                            id='brNo'
                            label="BR No."
                        />
                    </Grid>

                    <Grid item xs={9} s={6} md={5} lg={3} sx={{ml:3, mr:3, mb:3}}>
                        <TextField
                            fullWidth
                            {...register("enCompanyName")}
                            id="enCompanyName"
                            label="Name (English)"
                        />
                    </Grid>

                    <Grid item xs={9} s={6} md={5} lg={3} sx={{ml:3, mr:3, mb:3}}>
                        <TextField
                            fullWidth
                            {...register("chCompanyName")}
                            id="chCompanyName"
                            label="Name (Chinese)"
                        />
                    </Grid>

                </Grid>


                {/*last row*/}
                <Grid container maxWidth justifyContent="flex-end">

                    <Grid item sx={{ml:3, mr:3, mb:3, mt:3}}>
                        <Button
                            size="large"
                            variant="contained"
                            onClick={resetForm}
                            sx={{
                                textTransform: 'capitalize',
                                alignItems: 'end'
                            }}>
                            Clear
                        </Button>
                    </Grid>

                    <Grid item sx={{ml:3, mr:3, mb:3, mt:3}}>
                        <Button
                            size="large"
                            variant="contained"
                            type="submit"
                            sx={{
                                textTransform: 'capitalize',
                                alignItems: 'end'
                            }}>
                            Submit
                        </Button>
                    </Grid>
                </Grid>
            </form>
        </MainCard>
    );
};

export default OrganizationSearchForm;
