import React, {useCallback, useEffect, useState} from "react"
import Modal from "react-bootstrap/Modal";
import * as EgovNet from "api/egovFetch";
import Form from "react-bootstrap/Form";
import Row from "react-bootstrap/Row";
import Col from "react-bootstrap/Col";
import Button from "react-bootstrap/Button";
import SelectOption from "components/commonCode/SelectOption";
import CheckBox from "components/commonCode/CheckBox";
import CODE from "../../../constants/code";

function UserInfoModal({savedInfo, reloadFunction}){
    
    function userInfoChange(e){
        e.preventDefault();
        e.stopPropagation();
        const form = e.target;
        const info = {
            userSeq: form.userSeq.value,
            userId: form.userId.value,
            password: form.password.value,
            passwordChk: form.passwordChk.value,
            userNm: form.userNm.value,
            email: form.email.value,
            phoneNum: form.phoneNum.value,
            userRole: '',
            status: form.status.value,
        }
        let userRole = '';
        form.userRole.forEach(function (input){
            if(input.checked){
                userRole += input.value+','
            }
        })
        if(userRole){
            info.userRole = userRole.slice(0, -1)
        }
        EgovNet.requestFetch(
            '/admin/users/info',
            {
                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.result.resultMessage)
                }
            }
        )
    }

    function modalOpen(){
        EgovNet.requestFetch(
            '/admin/users/info?userId='+savedInfo?.userId,
            {
                method: "GET",
                headers: {
                    'Content-type': 'application/json'
                }
            },
            (resp) => {
                debugger
            }
        )
    }

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

    return (
        <>
            <Modal.Header closeButton>
                <Modal.Title>
                    {savedInfo?.userNm} 상세정보
                </Modal.Title>
            </Modal.Header>
            <Modal.Body>
                <Form onSubmit={(e) =>{userInfoChange(e)}} noValidate>
                    <input type={"hidden"} name={"userSeq"} defaultValue={savedInfo?.userSeq} />
                    <Form.Group as={Row} className="mb-3">
                        <Form.Label column sm={3}>
                            아이디
                        </Form.Label>
                        <Col sm={9}>
                            <Form.Control type="text" name="userId" placeholder="아이디" required defaultValue={savedInfo?.userId} readOnly/>
                        </Col>
                    </Form.Group>
                    <Form.Group as={Row} className="mb-3">
                        <Form.Label column sm={3}>
                            비밀번호
                        </Form.Label>
                        <Col sm={9}>
                            <Form.Control type="password" name="password" placeholder="비밀번호 변경 시 입력" />
                        </Col>
                    </Form.Group>
                    <Form.Group as={Row} className="mb-3">
                        <Form.Label column sm={3}>
                            비밀번호 확인
                        </Form.Label>
                        <Col sm={9}>
                            <Form.Control type="password" name="passwordChk" placeholder="비밀번호 변경 시 입력" />
                        </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="userNm" placeholder="이름" required defaultValue={savedInfo?.userNm} />
                        </Col>
                    </Form.Group>
                    <Form.Group as={Row} className="mb-3">
                        <Form.Label column sm={3}>
                            이메일
                        </Form.Label>
                        <Col sm={9}>
                            <Form.Control type="email" name="email" placeholder="email" required defaultValue={savedInfo?.email} />
                        </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="phoneNum" placeholder="연락처" required defaultValue={savedInfo?.phoneNum} />
                        </Col>
                    </Form.Group>
                    <Form.Group as={Row} className="mb-3">
                        <Form.Label column sm={3}>
                            사용자 권한
                        </Form.Label>
                        <Col sm={9}>
                            <CheckBox name={"userRole"} grpCd={"ROLE"} selectedValue={savedInfo?.userRole} />
                        </Col>
                    </Form.Group>
                    <Form.Group as={Row} className="mb-3">
                        <Form.Label column sm={3}>
                            상태
                        </Form.Label>
                        <Col sm={9}>
                            <SelectOption name={"status"} grpCd={"ACC_STUS"} selectedValue={savedInfo?.status} />
                        </Col>
                    </Form.Group>
                    <Row className="mb-3">
                        <Form.Label column xs={{span:8, offset:2}} id="findResultLabel"></Form.Label>
                        <Col xs={2}>
                            <Button type="submit">저장</Button>
                        </Col>
                    </Row>
                </Form>
            </Modal.Body>
        </>
    )
}

export default UserInfoModal;