const FindID = {
    emailAuthOk: false,
    tossCertOk: false,

    // 토스 인증서 본인확인 요청
    requestTossCertToPopup: function () {
        $.ajax({
            url: (BASE_URL + "/api/requestTossCertToPopup"),
            type: "POST",
            async: true,
            cache: true,
            timeout: 3000,
            processData: true,
            dataType: "JSON",
            beforeSend: function (xhr) {
                xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            },
            success: function (response) {
                if (response.resultType === "FAIL") { // 실패
                    alert(response.error.reason);

                    // 만료되었을 경우
                    if (response.error.errorCode === "CE1000" || response.error.errorCode === "CE3103") {
                        location.reload();
                    }
                } else {
                    let tossCert = TossCert();
                    tossCert.preparePopup();

                    tossCert.start({
                        authUrl: response.success.authUrl,
                        txId: response.success.txId,
                        onSuccess: function () {
                            document.getElementById("txId").value = response.success.txId;
                            document.getElementById("fFindID").submit();
                        },
                        onFail: function (error) {
                            console.error('에러가 발생했습니다', error);
                        },
                    });
                }
            },
            error: function (xhr, status, error) {
                procErrorEvent(xhr, status, error);
            }
        });
    }
}

// 토스 인증서 본인확인 요청
$(document).on("click", "#btnTossCertRequest", FindID.requestTossCertToPopup);
