
/* ##############################################################
	설  명 : [Modal]시술차트 상세/저장 팝업
	작성일 : 2023. 01. 19.
	작성자 : NTsoft
	수정일 	        수정자 	 수정내용
	-------         -------- ---------------------------
	2023. 01. 19.	NTsoft 	 최초 생성
*/
let userMedicalChartUpdateModal = {
	callback : null,
	reqParam : null,
	dataList : null,
	/* 모달 초기화 작업 */
	init : function () {
		$('#userMedicalChartUpdateModal').remove();
		$('body').append( this.HtmlTemplate);
		//그리드 이벤트
		this.setEvent();
	},
	/* 그리드 이벤트 설정 */
	setEvent:function() {
		$('#userMedicalChartUpdateModal .btnCancle').on("click", function(){
			userMedicalChartUpdateModal.close();
		});
		
		$('#userMedicalChartUpdateModal .btnSave').on("click", function(){
			userMedicalChartUpdateModal.save("I");
		});
		$('#userMedicalChartUpdateModal .more_btn').on("click", function(){
			fn_selectChartUpMenuCode();
		});
	},
	// 상세 조회
	search:function(state) {
		let muUserMedicalChartId = userMedicalChartUpdateModal.reqParam.muUserMedicalChartId;
		let muUserId             = userMedicalChartUpdateModal.reqParam.muUserId;

		if(true != fn_emptyCheck(muUserMedicalChartId)){
			modalEvent.warning("저장", "시술차트 정보가 없습니다.");
			return;
		}
		
		if(true != fn_emptyCheck(muUserId)){
			modalEvent.warning("저장", "고객 정보가 없습니다.");
			return;
		}
		
		let formData = new FormData();
		formData.append("menuClass", menuClass);
		formData.append("muUserMedicalChartId", muUserMedicalChartId);
		formData.append("muUserId", muUserId);

		$.ajax({
			url: encodeURI('/webusermedicalchart/selectUserMedicalChart.do'),
			data: formData,
			dataType: "json",
			processData: false,
			contentType: false,
			type: 'POST',
			async: true,
			success: function(data){
				if('0'==data.msgCode){
					// 고객 인적사항
					$('#userMedicalChartUpdateModal input[name="modalUserId"]').val(data.rows[0].muUserId);
					$('#userMedicalChartUpdateModal .modalUserName').text(data.rows[0].userName);
					
					$('#userMedicalChartUpdateModal .modalBirthday').text(data.rows[0].birthday);
					$('#userMedicalChartUpdateModal .modalAge').text(data.rows[0].age);
					$('#userMedicalChartUpdateModal .modalGender').text(data.rows[0].gender);
					$('#userMedicalChartUpdateModal .modalNationality').text(data.rows[0].nationality);
					$('#userMedicalChartUpdateModal .modalUserType').html(data.rows[0].userType?'<img src="'+data.rows[0].userType+'" alt="고객구분" />':'');
					$('#userMedicalChartUpdateModal .modalPhoneNumber').text(fn_setFormatPhone(data.rows[0].phoneNumber));
					$('#userMedicalChartUpdateModal textarea[name="modalEtc"]').val(data.rows[0].memo);
					
					// 멤버쉽 충전금액 추가 필요.
					
					// 최근시술차트 Top5
					let listHTML = '';
					for(let i = 0; i < data.userMedicalChartList.length; i++){
						let writeDate              = data.userMedicalChartList[i].writeDate;
						let treatmentName          = data.userMedicalChartList[i].treatmentName;
						let treatmentProcedureName = data.userMedicalChartList[i].treatmentProcedureName;
						
						listHTML += '<tr>';
						listHTML += '	<td>'+writeDate+'</td>';
						listHTML += '	<td>'+treatmentName+'</td>';
						listHTML += '	<td>'+treatmentProcedureName+'</td>';
						listHTML += '</tr>';
					}
					$('#userMedicalChartUpdateModal .modalUserMedicalChartTop5List').html(listHTML);
					
					// 시술차트
					$('#userMedicalChartUpdateModal .modalWriteDate').text(data.rows[0].writeDate);
					$('#userMedicalChartUpdateModal .modalWriteMemberName').text(data.rows[0].writeMemberName);
					$('#userMedicalChartUpdateModal .modalReserveDate').text(data.rows[0].reserveDate);
					$('#userMedicalChartUpdateModal .modalReserveTime').text(data.rows[0].reserveTime);
					$('#userMedicalChartUpdateModal .modalTreatmentName').text(data.rows[0].treatmentName);
					$('#userMedicalChartUpdateModal .modalTreatmentProcedureName').text(data.rows[0].treatmentProcedureName);
					$('#userMedicalChartUpdateModal .modalReserveMemberName').text(data.rows[0].reserveMemberName);
					$('#userMedicalChartUpdateModal textarea[name="modalUserConsultingChartContent"]').val(data.rows[0].userConsultingChartContent);
					$('#userMedicalChartUpdateModal textarea[name="modalContent"]').val(data.rows[0].content);
					
				}
				else{
					modalEvent.danger("조회 오류", data.msgDesc);
				}
			},
			error : function(xhr, status, error) {
				modalEvent.danger("조회 오류", "조회 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
			},
			beforeSend:function(){

			},
			complete:function(){

			}
		});
	},
	// 저장
	save:function(state) {
		let muUserMedicalChartId = userMedicalChartUpdateModal.reqParam.muUserMedicalChartId;
		let muUserId             = userMedicalChartUpdateModal.reqParam.muUserId;
		let content              = $('#userMedicalChartUpdateModal textarea[name="modalContent"]').val();

		if(true != fn_emptyCheck(muUserMedicalChartId)){
			modalEvent.warning("저장", "시술차트 정보가 없습니다.");
			return;
		}
		
		if(true != fn_emptyCheck(muUserId)){
			modalEvent.warning("저장", "고객 정보가 없습니다.");
			return;
		}
		
		if(true != fn_emptyCheck(content)){
			modalEvent.warning("저장", "시술내용 정보가 없습니다.");
			return;
		}
		
		modalEvent.info("저장", "시술 차트를 저장하시겠습니까?", function(){
			let formData = new FormData();
			formData.append("menuClass", menuClass);
			formData.append("muUserMedicalChartId", muUserMedicalChartId);
			formData.append("muUserId", muUserId);
			formData.append("content", content);
	
			$.ajax({
				url: encodeURI('/webusermedicalchart/updateUserMedicalChart.do'),
				data: formData,
				dataType: "json",
				processData: false,
				contentType: false,
				type: 'POST',
				async: true,
				success: function(data){
					if('0'==data.msgCode){
						modalEvent.success("저장 성공", data.msgDesc, function(){
							if( userMedicalChartUpdateModal.callback )  {
								userMedicalChartUpdateModal.callback();
							}
							userMedicalChartUpdateModal.close();
						});
					}
					else{
						modalEvent.danger("저장 오류", data.msgDesc);
					}
				},
				error : function(xhr, status, error) {
					modalEvent.danger("저장 오류", "저장 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
				},
				beforeSend:function(){
					
				},
				complete:function(){
					
				}
			});
		});
	},
	/*
	 * 팝업
	 * callback : 콜백함수, reqParam: 팝업파라미터
	 */
	popup:function(callback, reqParam) {
		// 초기화 후 열기
		userMedicalChartUpdateModal.init();
		
		//콜백함수 지정.
		userMedicalChartUpdateModal.callback = callback;
		userMedicalChartUpdateModal.reqParam = reqParam;
		userMedicalChartUpdateModal.dataList = null;
		
		setTimeout(function(){
			$('#userMedicalChartUpdateModal').modal({keyboard:false, backdrop:'static'});
		}, 200);
		
		userMedicalChartUpdateModal.search();
		
	},
	/*
	 * 팝업 닫기
	 */
	close:function() {
		$('#userMedicalChartUpdateModal').modal('hide');

		setTimeout(function(){
			$('#userMedicalChartUpdateModal').remove();
		},500);
	},
	HtmlTemplate : `
	<div id="userMedicalChartUpdateModal" class="modal charting_modal sisul fade" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header"></div>
					<div class="modal-body">
						<div class="charting_left">
							<p class="title modalUserName"></p>
							<ul class="charting_list">
								<li>
									<label>인적사항</label>
									<p>
										<span class="modalBirthday"></span>
										<span class="modalAge"></span>
										<span class="modalGender"></span>
										<span class="modalNationality"></span>
									</p>
								</li>
								<li>
									<label>고객구분</label>
									<p class="modalUserType"></p>
								</li>
								<li>
									<label>연락처</label>
									<p class="modalPhoneNumber"></p>
								</li>
								<li>
									<label>멤버십 충전금</label>
									<p>0</p>
								</li>
							</ul>
							
							<div class="textarea_box">
								<label>특이 사항</label>
								<p class="title_info">*시술 및 약물 관련 특이사항</p>
								<textarea name="modalMemo" placeholder="" readonly></textarea>
							</div>
							
							<div class="table_box">
								<label>상담차트 내역</label>
								
								<table>
									<thead>
										<tr>
											<th>작성일시</th>
											<th>진료 유형</th>
											<th>시술(상품)명</th>
										</tr>
									</thead>
									<tbody class="modalUserMedicalChartTop5List"></tbody>
								</table>
							</div>
							
							<div class="botton_box">
								<button class="more_btn">차트내역 더보기</button>
							</div>
						</div>
						<div class="charting_right">
							<div class="right_top">
								<p class="chart_numder">차트번호 U20220405-0000-A1</p>
								<p class="title">시술 차트</p>
								<div class="right_text">
									<p class="first">
										작성일자
										<span class="modalWriteDate"></span>
									</p>
									<p>
										작성자
										<span class="modalWriteMemberName"></span>
									</p>
								</div>
							</div>
							
							<div class="right_bottom">
								<div class="table_box">
									<table>
										<thead>
											<tr>
												<th>예약일시</th>
												<th>진료/시술</th>
												<th>닥터</th>
											</tr>
										</thead>
										<tbody>
											<tr>
												<td><span class="modalReserveDate"></span>, <span class="modalReserveTime"></span></td>
												<td><span class="modalTreatmentName"></span> / <span class="modalTreatmentProcedureName"></span></td>
												<td><span class="modalReserveMemberName"></span></td>
											</tr>
										</tbody>
									</table>
								</div>
								
								<div class="textarea_box first">
									<label>상담 내용</label>
									<textarea name="modalUserConsultingChartContent" placeholder="" readonly></textarea>
								</div>
								
								 <div class="textarea_box">
									<label>시술 내용</label>
									<textarea name="modalContent" placeholder="시술 내용을 입력하세요."></textarea>
								</div>
										
								<div class="button_box">
									<button class="cancel_btn btnCancle">취소</button>
									<button class="save_bth btnSave">저장</button>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	`
};
function fn_selectChartUpMenuCode(){
	let formData = new FormData();
	formData.append("menuClass", menuClass);
	formData.append("menuName", '시술차트');
	$.ajax({
		url: encodeURI('/webmenu/selectMenuCode.do'),
		data: formData,
		dataType: "json",
		processData: false,
		contentType: false,
		type: 'POST',
		async: true,
		success: function(data){
			if('0'==data.msgCode){
				let pagingParam = "?menuClass="+data.menuCode;
				pagingParam    += "&muUserId="+userMedicalChartUpdateModal.reqParam.muUserId;
				fn_leftFormAction("/webusermedicalchart/selectListUserMedicalChartIntro.do"+pagingParam);
			}
			else{
				modalEvent.danger("조회 오류", data.msgDesc);
			}
		},
		error : function(xhr, status, error) {
			modalEvent.danger("조회 오류", "조회 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
		},
		beforeSend:function(){
		},
		complete:function(){
		}
	});
}