<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="ui" uri="http://egovframework.gov/ctl/ui" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<script>
$(document).ready(function(){
    var cnt=${cnt};
    
    if(cnt > 0) {
        fn_showLayerPop(); 
    } else {
        window.close();
    }
     
    <%//비밀번호 찾기 페이지에서 다시 돌아왔을 경우 %>
    var previousPage = "${vo.previousPage}";
    if(previousPage == "alreadySignUpPopup")
    {
        $("#alreadySingUpForm").css("display","none");  
        $("#selectAccountForm").css("display","block");
        
    }
});

function selectAccount(mbInfoId,name,email)
{
    $("#alreadySingUpForm").css("display","none");
    $("#mbInfoId").val(mbInfoId);
    $("#selectName").text(name);
    $("#selectEmail").text(email);
    $("#nameTemp").val(name);
    $("#emailTemp").val(email);
    $("#selectAccountForm").css("display","block");
}
function goPrevious()
{
    $("#selectAccountForm").css("display","none");
    $("#alreadySingUpForm").css("display","block");
}

function accountLink()
{
    password=$("#selectPassword").val();

    if(password == "")
    {
        alert("비밀번호 입력이 필요합니다.");
        return false;
    }
    
    $.ajax({
        url : "${pageContext.request.contextPath}/member/snsAccountLink.ajax",
        type : "POST",
        async: false,
        data : {"mbInfoId":$("#mbInfoId").val()
                ,"password":password
                ,"name":$("#nameTemp").val()
                ,"email":$("#emailTemp").val()
                },
        success : function(result){
            if(result=="link")
            {
                alert("계정이 통합되었습니다.");
                location.href="${pageContext.request.contextPath}/member/snsAccountLinkLogin.do"
            }else if(result=="mismatch")
            {
                alert("올바르지 않은 비밀번호입니다.");
            }
            
        },error : function(){
        }
    })
    
  
}

</script>
<form id="alreadySingUpForm" name="alreadySingUpForm">
<!-- 계정연결 팝업 -->
    <div class="pop-login-connect">
        <div class="row Title">
            <p>계정연결</p>
            <h3>서비스에 이미 가입된<br>계정이 있습니다.</h3>
        </div>
        <div class="inner">
            <div class="row Text">
                <p>계정연결을 통해 간편하게 <span>로그인할 계정</span>을 선택하세요.</p>
            </div>
            <div class="row First">
                <c:forEach items="${result}" var="result" varStatus="status">
                    <div class="Btn" onClick="selectAccount('<c:out value="${result.mbInfoId}"/>','<c:out value="${result.name}"/>','<c:out value="${result.email}"/>');">
                       <button type="button"><span class="name"><c:out value="${result.name}"/></span><span class="mail"><c:out value="${result.email}"/></span></button>
                    </div>
                </c:forEach>
            </div>
            <div class="row btn">
                <p><a href="javascript:void(0);" class="cancel" onclick="fn_closeLayerPopup();">아니요, 내가 아닙니다.</a></p>
            </div>
            <button type="button" class="login-close" onclick="fn_closeLayerPopup();"></button>
        </div>
        <div class="Bg"></div>
    </div>
 </form>
<form id="selectAccountForm" name="selectAccountForm" style="display:none;">
    <input type="hidden" id="mbInfoId" name="mbInfoId" value="<c:out value='${vo.mbInfoId }'/>">
    <input type="hidden" id="nameTemp" name="nameTemp" value="<c:out value='${vo.nameTemp }'/>">
    <input type="hidden" id="emailTemp" name="emailTemp" value="<c:out value='${vo.emailTemp }'/>">
    <input type="hidden" id="mobileNo" name="mobileNo" value="<c:out value='${vo.mobileNo }'/>">
    <input type="hidden" id="previousPage" name="previousPage" value="alreadySignUpPopup">
    <!-- 계정연결 비밀번호 팝업 -->
    <div class="pop-login-password">
        <div class="row Title">
            <p>계정연결</p>
            <h3>비밀번호 입력</h3>
        </div>
        <div class="inner">
            <div class="row Text">
                <p>선택한 계정의 <span>비밀번호</span>를 입력해주세요!</p>
            </div>
            <div class="row First">
                <div class="Btn"><button type="button"><span class="name" id="selectName" name="selectName"><c:out value="${vo.nameTemp}"/></span><span class="mail" id="selectEmail" name="selectEmail"><c:out value="${vo.emailTemp}"/></span></button></div>
                <div class="passwd"><label for="password" style="display:none;">비밀번호</label><input type="password" id="selectPassword" name="selectPassword" placeholder="비밀번호"></div>
            </div>
            <div class="row btn">
                <p><a href="javascript:void(0);" class="cancel" onclick="forgetPassword('selectAccountForm');">비밀번호를 잊으셨나요? &gt;</a></p>
            </div>
            <div class="row btn-box">
                <button type="button" class="btn btn-gray" onclick="goPrevious();" id="cancel">이전으로</button>
                <button type="button" class="btn btn-color" onclick="accountLink();" >계정 연결하기</button>
            </div>
            <button type="button" class="login-close" onclick="fn_closeLayerPopup();"></button>
        </div>
        <div class="Bg"></div>
    </div>
 </form>
