import React, { useState, useEffect } from 'react';
import TextField from '@mui/material/TextField';

import * as EgovNet from 'api/egovFetch';

import CODE from 'constants/code';

import FormDialog from '../../../../components/alert/FormDialog';

function CommitteeCodeRegistrationPopup(props) {

    const [inputValues, setInputValues]       = useState({});

    const [mode, setMode] = useState("등록");

    useEffect(function () {

        if( typeof props.createOrModifyCondition === "undefined" ) {
            return;
        }

        if( typeof props.createOrModifyCondition.mode === "undefined" ) {
            return;
        }
        
        //모드가 변경 되면 값을 초기화 한다.
        setInputValues({});

        if( props.createOrModifyCondition.mode === CODE.MODE_MODIFY ) {
            setMode("수정");
        } else if( props.createOrModifyCondition.mode === CODE.MODE_CREATE ) {
            setMode("등록");
        }

    // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [props.createOrModifyCondition && props.createOrModifyCondition.mode]);

    useEffect(function () {

        if( typeof props.createOrModifyCondition === "undefined" ) {
            return;
        }

        if( typeof props.createOrModifyCondition.mode === "undefined" ) {
            return;
        }

        if( props.createOrModifyCondition.mode === CODE.MODE_CREATE ) {            
            return;
        }

        if( typeof props.createOrModifyCondition.target === "undefined" ) {
            return;
        }

        

        setInputValues({
            ...inputValues,
            paramOrgNm : props.createOrModifyCondition.target.orgNm,
            paramOrgDesc : props.createOrModifyCondition.target.orgDesc,
        });
        

    // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [props.createOrModifyCondition && props.createOrModifyCondition.target]);

    


    const handleClickCreateCommitteeCodeManagement = (createOrModifyCondition) => {  
        
        if( createOrModifyCondition.paramOrgId === undefined ) {
            alert('상위 코드를 선택해주세요.');
            props.setOpen(false);
            return false;
        }
        const formData = new FormData();
        
        for (let key in createOrModifyCondition) {
            formData.append(key, createOrModifyCondition[key]);
        }
        
        Object.keys(inputValues).forEach(function(key){
            formData.append(key, inputValues[key]);
        });


        const requestOptions = {
            method: props.createOrModifyCondition.mode === CODE.MODE_CREATE ? "POST" : props.createOrModifyCondition.mode === CODE.MODE_MODIFY ? "PUT" : "",
            body: formData,
        };

        let appendRequestURL = "";
        if( props.createOrModifyCondition.mode === CODE.MODE_MODIFY ) {
            appendRequestURL = `/${props.createOrModifyCondition.target.orgId}`;
        }
        const requestURL = "/admin/config/committee-code-management" +  appendRequestURL;
        EgovNet.requestFetch(requestURL,
            requestOptions,
            function (resp) {
                //새로 읽어 드린다.
                let paramCodeGroup = props.createOrModifyCondition.paramOrgId;
                if( paramCodeGroup === "00" ) {
                    paramCodeGroup = null;
                }
                props.setSearchCondition({...props.searchCondition, paramCodeGroup, paramCodeLevel : props.createOrModifyCondition.paramCodeLevel});

                props.setOpen(false);
                setInputValues({});
            }
        );
    }

    const handleTextFieldChange = (e) => {
        setInputValues({
            ...inputValues,
            [e.target.name]: e.target.value,
        });
    };

    return (// handleClickCreateCommitteeCodeManagement create or modiofy 로 분기처리해야 한다. thkim 20240305 1500
        <FormDialog open={props.open} setOpen={props.setOpen} title={`위원회 코드 ${mode}`} contentText="위원회 코드 항목을 입력해주세요." handleOk={(e) => {handleClickCreateCommitteeCodeManagement(props.createOrModifyCondition);}} >
            <TextField
                autoFocus
                required
                margin="dense"
                id="insert-org-nm"
                name="paramOrgNm"
                label="명칭"
                type="text"
                fullWidth
                variant="standard"
                value={inputValues.paramOrgNm ? inputValues.paramOrgNm : "" }
                onChange={handleTextFieldChange}
            />
            <TextField
                required
                margin="dense"
                id="insert-org-desc"
                name="paramOrgDesc"
                label="위원회 설명"
                type="text"
                fullWidth
                variant="standard"
                value={inputValues.paramOrgDesc ? inputValues.paramOrgDesc : ""}
                onChange={handleTextFieldChange}
            />
        </FormDialog>
    );
}

export default CommitteeCodeRegistrationPopup;