<%@ 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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script>
function notAlreadySingUp()
{
	window.close();
}
$(document).ready(function(){
	var cnt=${cnt};
	
	if(cnt > 0) {
	    fn_showLayerPop(); 
	} else {
	    window.close();
	}
});

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

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

</script>
</head>
<body>
<form id="alreadySingUpForm" name="alreadySingUpForm">
<p1>서비스에 이미 가입된 계정이 있습니다.</p1>
<table>
    <c:forEach items="${result}" var="result" varStatus="status">

                                    <tr onClick="selectAccount('<c:out value="${result.mbInfoId}"/>','<c:out value="${result.name}"/>','<c:out value="${result.email}"/>');">
                                        <td><c:out value="${result.name}"/></td>
                                        <td><c:out value="${result.email}"/></td>
                                    </tr>
   </c:forEach>
</table>
   <a href="javascript:void(0);" onclick="fn_closeLayerPopup();">아니요, 내가 아닙니다.</a>
 </form>
<form id="selectAccountForm" name="selectAccountForm" style="display:none;">
	<p1>비밀번호 입력</p1>
	<div id="accountInfo">
	   <input type="text" id="selectName" name="selectName" readonly><input type="text" id="selectEmail" name="selectEmail" readonly>
	</div>
	<input type="password" id="selectPassword" name="selectPassword" placeholder="비밀번호"><br>
	<a href="javascript:notAlreadySingUp();" onclick="return false;">비밀번호를 잊으셨나요?</a>
	<input type="button" onclick="goPrevious();" value="이전으로"><input type="button" onclick="accountLink();" value="계정 연결하기">
	<input type="hidden" id="mbInfoId" name="mbInfoId">
</form>
</body>
</html>