<!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="~{/mobile/layout/layout}">
<th:block layout:fragment="layout_css">
	<link rel="stylesheet" href="/css/mobile/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/mobile/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 src="/js/mobile/naverLogin_SDK_2.0.2.js"></script>
<script type="text/javascript">
$(function(){
	getDeviceInfo();
});

let ntUserId = '';
let email = '';
let snsEmail = '';


var clientId = "EhgHX46SiQBMflqt41YB";

var naverLogin = new naver.LoginWithNaverId({
	clientId: clientId
});

naverLogin.init();

$(function(){
    naverLogin.getLoginStatus(function (status){
        if(status){
        	let userEmail = naverLogin.user.email;
        	snsEmail = naverLogin.user.email;

			fn_selectEmailCheck(userEmail);
        }
    });
});

/****************************************************************************
* 아이디 확인
****************************************************************************/
function fn_selectEmailCheck(param){
	let formData = new FormData();
	formData.append("email", param);
	formData.append("loginType", "N");

	$.ajax({
		url: '/mobileuser/selectEmailCheck.do',
		data: formData,
		dataType: "json",
		processData: false,
		contentType: false,
		type: 'POST',
		success: function(data){
			if(data.msgCode=='0'){
				if(0<data.rows.length){
					muUserId = data.rows[0].muUserId;
					fn_snsLogin();
					// if('Y'==data.rows[0].naverYn){
					// 	fn_snsLogin();
					// }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=N");
}

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

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

	let formData = new FormData();
	formData.append("muUserId", muUserId);
	formData.append("loginType", "N");
	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, "", "N");
				// fn_leftFormAction("/mobilehome/homeIntro.do");
				fn_leftFormAction("/home");
			}else{
				modalEvent.danger("로그인 오류", data.msgDesc);
			}
		},
		error : function(xhr, status, error) {
			modalEvent.danger("로그인 오류", "조회 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
		}
	});
}
</script>
</th:block>
</html>