import React from "react";
import Modal from "react-bootstrap/Modal";
import Form from "react-bootstrap/Form";
import Row from "react-bootstrap/Row";
import Col from "react-bootstrap/Col";
import * as EgovNet from "api/egovFetch";
import CODE from "constants/code";

function SurveyModal({savedInfo, reloadFunction}){

    function editSurvey(e){
        e.preventDefault();
        e.stopPropagation();
        const form = e.target;
        const info = {
            menuId: form.menuId.value,
            menuTitle: form.menuTitle.value,
            menuGroup: form.menuGroup.value,
            menuLevel: form.menuLevel.value,
            menuSort: form.menuSort.value,
            menuUrl: form.menuUrl.value,
            menuTypeCd: form.menuTypeCd.value,
        }
        EgovNet.requestFetch(
            '/admin/config/menu-mgt',
            {
                method: "PUT",
                headers: {
                    'Content-type': 'application/json'
                },
                body: JSON.stringify(info)
            },
            (resp) => {
                if (Number(resp.resultCode) === Number(CODE.RCV_SUCCESS)) {
                    alert("저장되었습니다.")
                    reloadFunction();
                }else if(Number(resp.resultCode) === Number(CODE.RCV_ERROR_AUTH)){
                    console.log("토큰 갱신중.")
                }else{
                    alert(resp.resultMessage)
                }
            }
        )
    }

    return (
        <>
            <Modal.Header closeButton>
                <Modal.Title>
                    {savedInfo!==undefined?savedInfo?.svyTitle:'설문 생성'}
                </Modal.Title>
            </Modal.Header>
            <Modal.Body>
                <Form onSubmit={(e) =>{editSurvey(e)}} noValidate>
                    <Form.Group as={Row} className="mb-3">
                        <Form.Label column sm={3}>
                            제목
                        </Form.Label>
                        <Col sm={9}>
                            <Form.Control type="email" name="svyTitle" required defaultValue={savedInfo?.svyTitle} />
                        </Col>
                    </Form.Group>
                    <Form.Group as={Row} className="mb-3">
                        <Form.Label column sm={3}>
                            설명
                        </Form.Label>
                        <Col sm={9}>
                            <Form.Control as={"textarea"} name="svyDesc" rows={3} defaultValue={savedInfo?.svyDesc}/>
                        </Col>
                    </Form.Group>
                    <Form.Group as={Row} className="mb-3">
                        <Form.Label column sm={3}>
                            시작일
                        </Form.Label>
                        <Col sm={9}>
                            <Form.Control type="text" name="svyStartDt" required defaultValue={savedInfo?.svyStartDt} />
                        </Col>
                    </Form.Group>
                    <Form.Group as={Row} className="mb-3">
                        <Form.Label column sm={3}>
                            종료일
                        </Form.Label>
                        <Col sm={9}>
                            <Form.Control type="text" name="svyEndDt" required defaultValue={savedInfo?.svyEndDt} />
                        </Col>
                    </Form.Group>
                    <Form.Group as={Row} className="mb-3">
                        <Form.Label column sm={3}>
                            첨부파일
                        </Form.Label>
                        <Col sm={9}>
                            <Form.Control type="text" name="fileGrpId" selectedValue={savedInfo?.fileGrpId}/>
                        </Col>
                    </Form.Group>
                    <Row className="mb-3">
                        <Col xs={10}></Col>
                        <Col xs={2}>
                            <button type="submit" className={"btn btn_blue_h31 px-3"}>저장</button>
                        </Col>
                    </Row>
                </Form>
            </Modal.Body>
        </>
    );
}

export default SurveyModal;