import React, {useEffect, useState} from "react";
import Form from "react-bootstrap/Form";
import * as EgovNet from "api/egovFetch";

function SelectOption({name, grpCd, selectedValue}){

    const [options, setOptions] = useState();
    const [value, setValue] = useState(selectedValue)

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

    useEffect(() => {
        setValue(selectedValue)
    }, [selectedValue]);

    function getCodeItemList(){
        EgovNet.requestFetch(
            '/commonCode/code-item?grpCd='+grpCd,
            {
                method: "GET"
            },
            (resp) => {
                let optionTag = [];
                // 리스트 항목 구성
                resp.result.codeList.forEach(function (item, index) {
                    optionTag.push(
                        <option value={item.itemCd} key={`${grpCd}option${index}`}>{item.itemNm}</option>
                    );
                });
                setOptions(optionTag);
            },
            function (resp) {
                console.log("err response : ", resp);
            }
        );
    }

    return (
        <Form.Select name={name} value={value} onChange={(e)=>{setValue(e.target.value)}}>
            {options}
        </Form.Select>
    )
}

export default SelectOption;