<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
	  xmlns:th="http://www.thymeleaf.org"
	  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
	  layout:decorate="~{/web/layout/layout}">
<th:block layout:fragment="layout_css">
	<link rel="stylesheet" href="/css/web/loginCallback.css?v1.1">
</th:block>

<th:block layout:fragment="layout_top_script">
<script>

</script>
</th:block>

<th:block layout:fragment="layout_content">
<div class="project_wrap">
		<div class="content_wrap">
			<div class="content_box">
				<img class="center_logo" src="/image/web/center_logo.svg">

				<div class="form_wrap">
					<!--input-->
					<div class="input_wrap">
						<p id="commentP"></p>
						<p id="email"></p>
					</div>
				</div>

				<div class="button_wrap" id="buttonWrap">
				</div>
			</div>
		</div>
	</div>
</th:block>
<th:block layout:fragment="layout_script">
<script type="text/javascript" src="https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js"></script>
<script type="text/javascript">
let appleParam 		= '[[${param}]]';
let appleIdToken 	= '[[${param.id_token}]]';
let appleCode 		= '[[${param.code}]]';
let appleState 		= '[[${param.state}]]';
let sessionNtUserId = "[[${session.ntUserId}]]";

$(function(){
	getDeviceInfo();
	fn_getUserInfo();
});

function fn_getUserInfo(){
	if(''==appleIdToken){
		modalEvent.danger("로그인 오류", "조회 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
	}else{
		let jwtPayload = appleIdToken.split(".")[1];

		payloadResult = fn_getToken(jwtPayload);

	    let email = payloadResult.email;

	    if(''==email){
	    	modalEvent.danger("로그인 오류", "조회 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
	    }else{
            fn_selectEmailCheck(email);
	    }
	}
}

function fn_selectEmailCheck(param){
	let formData = new FormData();
	formData.append("email", param);
	formData.append("snsType", "A");

	$.ajax({
		url: '/webuser/selectEmailCheck.do',
		data: formData,
		dataType: "json",
		processData: false,
		contentType: false,
		type: 'POST',
		success: function(data){
			if(data.msgCode=='0'){
				if(0<data.rows.length){
					ntUserId = data.rows[0].ntUserId;
                    fn_snsLogin();
                    // if('Y'==data.rows[0].appleYn){
					// }else{
					// 	$('#commentP').text("이미 가입된 아이디가 있습니다.");
					// 	$('#buttonWrap').append('<button type="button" class="btn" onclick="javascript:fn_insertSnsLogin();">애플로 연결하기</button>');
					// }
					// $('#email').val(email);
				}else{
					$('#commentP').text("회원을 찾을 수 없습니다. 회원가입을 하시겠습니까?");
					$('#buttonWrap').append('<button type="button" class="btn" onclick="javascript:fn_insertSnsUser(\''+param+'\');">가입하기</button>');
				}
			}
		},
		error : function(xhr, status, error) {
			modalEvent.danger("로그인 오류", "조회 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
		}
	});
}

/*****************************************
* sns 회원가입 이동
******************************************/
function fn_insertSnsUser(param){
	fn_leftFormAction("/mobilejoin/joinIntro.do?snsEmail="+param+"&loginType=A");
}

/*****************************************
* sns 로그인
******************************************/
function fn_snsLogin(){

    // 로그인할 때 한 번 더 호출
    getDeviceInfo();

	let formData = new FormData();
	formData.append("ntUserId", ntUserId);
	formData.append("loginType", "A");
	formData.append("loginId", snsEmail);
	formData.append("deviceUuid", deviceUuid1);
	formData.append("phoneType", phoneType1);
	formData.append("modelName", modelName1);
	formData.append("osVersion", osVersion1);
	formData.append("pushKey", pushKey1);
	formData.append("appVersion", appVersion1);
	formData.append("deviceYn", deviceYn);

	$.ajax({
		url: '/mobilelogin/snsLogin.do',
		data: formData,
		dataType: "json",
		processData: false,
		contentType: false,
		type: 'POST',
		success: function(data){
			if(data.msgCode=='0'){
				setLoginInfo(snsEmail, "", "A");
				// fn_leftFormAction("/mobilehome/homeIntro.do");
				fn_leftFormAction("/home");
			}else{
				modalEvent.danger("로그인 오류", data.msgDesc);
			}
		},
		error : function(xhr, status, error) {
			modalEvent.danger("로그인 오류", "조회 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
		}
	});
}
</script>
</th:block>
</html>