
/****************************************************************************
* 예약 등록 팝업
****************************************************************************/
function fn_insertReservePopup(param, param2){
	if(!fn_reserveRelationCheck()){
		modalEvent.warning("", "등록 권한이 없습니다.");
		return false;
	}

	// 예약일자, 예약시간 추가
	let reserveDate = "";
	let reserveTime = param2;
	if(0<$("#date_reserveSearchDate").length){
		reserveDate = $("#date_reserveSearchDate").data().date;
	}
	if(!reserveTime){
		reserveTime = "";
	}else{
		reserveTime = reserveTime.substring(0,2) + ":" + reserveTime.substring(2,4);
	}

	let reqParam =  {"muUserId":param,
		             "reserveDate":reserveDate,
		             "reserveTime":reserveTime};
	reserveInsertModal.popup(function(obj, reqParam) {
		let pathname = window.location.pathname;

		if("/" == pathname || "/webhome/homeIntro.do" == pathname){
			fn_searchReserveDate();
		}
	}, reqParam);
}

/****************************************************************************
* 바로접수 등록 팝업
****************************************************************************/
function fn_insertReserveReadyPopup(param){
	if(!fn_reserveRelationCheck()){
		modalEvent.warning("", "등록 권한이 없습니다.");
		return false;
	}
	let reqParam =  {'muUserId':param};
	reserveReadyInsertModal.popup(function(obj, reqParam) {
		let pathname = window.location.pathname;

		if("/" == pathname || "/webhome/homeIntro.do" == pathname){
			fn_searchReserveDate();
		}
	}, reqParam);
}
/****************************************************************************
 * 고객등록
 ****************************************************************************/
function fn_insertUserPopup2() {
	if(!fn_userRelationCheck()){
		modalEvent.warning("", "등록 권한이 없습니다.");
		return false;
	}
	let reqParam = {};
	userInsertModal2.popup(function (obj, reqParam) {
	}, reqParam);
}

/****************************************************************************
* 고객 검색 (상단 검색)
****************************************************************************/
function fn_selectUserSearch(){
	let searchKeyword = $(".header_section .search_wrap .search_box input[name='searchKeyword']").val();
	
	if(true != fn_emptyCheck(searchKeyword)){
		modalEvent.warning("조회", "검색어 정보가 없습니다.");	
		return;
	}
	
	let formData = new FormData();
	formData.append("menuClass", menuClass);
	formData.append("searchKeyword", searchKeyword);

	$.ajax({
		url: encodeURI('/webuser/selectUserSearch.do'),
		data: formData,
		dataType: "json",
		processData: false,
		contentType: false,
		type: 'POST',
		async: true,
		success: function(data){
			if('0'==data.msgCode){
				let listHTML = '';
				
				if(0<data.rows.length){
					for(let i=0 ; i < data.rows.length ; i++){
						let muUserId      = data.rows[i].muUserId;
						let userName      = data.rows[i].userName;
						let birthday      = data.rows[i].birthday;
						let gender        = data.rows[i].gender;
						let phoneNumber   = data.rows[i].phoneNumber;
						let userType2     = data.rows[i].userType2;
						let lastVisitDate = data.rows[i].lastVisitDate;
						
						if('T' == userType2){
							listHTML += '<li>';
							listHTML += '	<p class="first user">'+userName+'</p>';
							listHTML += '	<p>'+fn_setFormatPhone(phoneNumber)+'</p>';
							listHTML += '	<p>최근내원 <span>'+lastVisitDate+'</span></p>';
							listHTML += '	<div class="button_box">';
							listHTML += '		<button class="reservation_btn" onclick="fn_insertReservePopup(\''+muUserId+'\')">예약</button>';
							listHTML += '		<button class="register_btn" onclick="fn_insertReserveReadyPopup(\''+muUserId+'\')">접수</button>';
							listHTML += '	</div>';
							listHTML += '</li>';
						}
						else{
							listHTML += '<li>';
							listHTML += '	<p class="first user">'+userName+'</p>';
							listHTML += '	<p>'+birthday+'</p>';
							listHTML += '	<p>'+gender+'</p>';
							listHTML += '	<p>'+fn_setFormatPhone(phoneNumber)+'</p>';
							listHTML += '	<p>최근내원 <span>'+lastVisitDate+'</span></p>';
							listHTML += '	<div class="button_box">';
							listHTML += '		<button class="reservation_btn" onclick="fn_insertReservePopup(\''+muUserId+'\')">예약</button>';
							listHTML += '		<button class="register_btn" onclick="fn_insertReserveReadyPopup(\''+muUserId+'\')">접수</button>';
							listHTML += '	</div>';
							listHTML += '</li>';
						}
					}
				}
				else{
					listHTML += '<li>';
					listHTML += '	<span>조회결과가 없습니다.</span>';
					listHTML += '</li>';
				}
				
				$(".header_section .search_wrap .search_box .search_list_modal .search_list").html(listHTML);
			}
			else{
				modalEvent.danger("조회 오류", data.msgDesc);
			}
		},
		error : function(xhr, status, error) {
			modalEvent.danger("조회 오류", "조회 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
		},
		beforeSend:function(){
		},
		complete:function(){
			
		}
	});
}

/****************************************************************************
* 상단 고객 검색 닫기
****************************************************************************/
function fn_closeUserSearch(){
	$(".header_section .search_wrap .search_box .search_list_modal").hide();
	$(".header_section .search_wrap .search_box input[name='searchKeyword']").val('');
}

/****************************************************************************
* header 페이지 init
****************************************************************************/
function fn_headerPageInit(){
	$("#loginMemberName").text(loginMemberName);
}

/****************************************************************************
* header 페이지 event
****************************************************************************/
function fn_headerPageEvent(){
	// 예약 등록 팝업 버튼
	$("#btnHeaderInsertReserve").click(function () {
		fn_insertReservePopup('');
	});
	
	// 바로접수 등록 팝업 버튼
	$("#btnHeaderInsertReserveReady").click(function () {
		fn_insertReserveReadyPopup('');
	});
	
	// 신환 등록 팝업 버튼
	$("#btnHeaderInsertUser").click(function () {
		fn_insertUserPopup2();
	});

	
	// 상단 고객 검색
	$(".header_section .search_wrap .search_box input[name='searchKeyword']").on("keypress", function(e) {
		if(e.which){
			// 파이어폭스
			if(13 == e.which) {
				let temp = $(this).val();

				if(2 <= temp.length){
					fn_selectUserSearch();

					$(this).siblings('.search_list_modal').show();
				}
				else{
					$(this).siblings('.search_list_modal').hide();
				}
			}
		}else{
			// 윈도우, 사파리, 크롬
			if(13 == event.keyCode) {
				let temp = $(this).val();

				if(2 <= temp.length){
					fn_selectUserSearch();

					$(this).siblings('.search_list_modal').show();
				}
				else{
					$(this).siblings('.search_list_modal').hide();
				}
			}
		}
	});
	
	$('html').click(function(e){
		// 상단 고객 검색 포커스 아웃을 경우 닫기
		if($(e.target).parents(".header_section .search_wrap .search_box .search_list_modal ,.header_section .search_wrap .search_box input[name='searchKeyword']").length < 1){
			fn_closeUserSearch();
		}
	});
	
	// 상단 고객검색 - 신환 등록 팝업 버튼
	$("#btnHeaderInsertUser2").click(function () {
		fn_closeUserSearch();
		
		fn_insertUserPopup2();
	});
}

$(function(){
	// header 페이지 init
	fn_headerPageInit();
	
	// header 페이지 event
	fn_headerPageEvent();
});
