require(['config'], function() {
	require([
		'jquery', 
		'check', 
		'crypto' //Crypto 암호화 js - 필요한 페이지만 불러서 쓴다
	], function($) {

	});
});

let API_USER_INFO_USER = '/api/user/info-user'; // 사용자헤더정보 
let API_USER_CHG_PW = '/api/user/chg-pw'; // 사용자개인패스워드변경
let API_USER_SAVE_PRIVATE_USER = '/api/user/save-private-user'; // 사용자팝업개인정보저장

function itp_fn_USER_INFO_MODIFY_POPUP(parentPopFn, args, returnType) {
	
	fn_ajax_call(API_USER_INFO_USER, args, function(result) {
		
		$('#ITP_FORM_USER_INFO_MODIFY_POPUP_USER_ID').val(result.userId);		
		$('#ITP_FORM_USER_INFO_MODIFY_POPUP .fnUserId').text(result.userId);
		$('#ITP_FORM_USER_INFO_MODIFY_POPUP .fnUserNm').text(result.userNm);
		$('#ITP_FORM_USER_INFO_MODIFY_POPUP_EMAIL').val(result.email);
		$('#ITP_FORM_USER_INFO_MODIFY_POPUP_TEL_NO').val(result.telNo);
		 	
	}, 'GET');
	
	$('#ITP_USER_INFO_MODIFY_POPUP_BTN_SAVE').on('click', function() {
		
		const formId = '#ITP_FORM_USER_INFO_MODIFY_POPUP';
		itp_fn_form_clear_validate(null, formId);
		var vali_email = itp_fn_form_validate(formId, formId + '_EMAIL', ['empty'], undefined);
		var vali_telNo = itp_fn_form_validate(formId, formId + '_TEL_NO', ['empty'], undefined);

		if (vali_email && vali_telNo) {
			let param = $('#ITP_FORM_USER_INFO_MODIFY_POPUP').serializeObject();
		
			fn_ajax_call(API_USER_SAVE_PRIVATE_USER, JSON.stringify(param), function() {			
				parentPopFn();
				$('#ITP_USER_INFO_MODIFY_POPUP').modal('hide'); // 팝업닫기			
			}, 'POST');
		}
	});	
	
	$('#ITP_USER_INFO_MODIFY_POPUP_BTN_PASSWORD_CHG').on('click', function() {	
		
		const formId = '#ITP_FORM_USER_INFO_MODIFY_PW';
		itp_fn_form_clear_validate(null, formId);
		var vali_password = itp_fn_form_validate(formId, formId + '_PASSWORD', ['empty'], undefined);
		var vali_chgPassword = itp_fn_form_validate(formId, formId + '_CHG_PASSWORD', ['empty'], undefined);
		var vali_chgPasswordConfirm = itp_fn_form_validate(formId, formId + '_CHG_PASSWORD_CONFIRM', ['empty'], undefined);

		if (vali_password && vali_chgPassword && vali_chgPasswordConfirm) {
			
			if($('#ITP_FORM_USER_INFO_MODIFY_PW_CHG_PASSWORD').val() != $('#ITP_FORM_USER_INFO_MODIFY_PW_CHG_PASSWORD_CONFIRM').val()) {
				itp_fn_modal_alert('변경 패스워드와 변경 패스워드 확인 값이 다릅니다.');
				return;
			}
			
			fn_ajax_call('/api/session/hashed', null, function(result) {
				
				var hashed = fnHashed(result.hashed);
				var password = CryptoJS.AES.encrypt($('#ITP_FORM_USER_INFO_MODIFY_PW_PASSWORD').val(), hashed);
				var passwordConfirm = CryptoJS.AES.encrypt($('#ITP_FORM_USER_INFO_MODIFY_PW_CHG_PASSWORD_CONFIRM').val(), hashed);
				
				const key = {password: password.toString(), chgPassword: passwordConfirm.toString() };
				var param = $.param(key);
			
				fn_ajax_call(API_USER_CHG_PW, param, function(chg_result) {			
					parentPopFn();
					$('#ITP_USER_INFO_MODIFY_POPUP').modal('hide'); // 팝업닫기			
				}, 'GET');
				
			}, 'GET');
			
		}
	});
	
	$('#ITP_USER_INFO_MODIFY_POPUP').one('shown.bs.modal', function(e) {		
		itp_fn_fire_window_resize();
	}).one('hidden.bs.modal', function(e) {
		$('#ITP_USER_INFO_MODIFY_POPUP').remove();
	}).modal('show');
	
};

