
/* ##############################################################
	설  명 : [Modal]시술 정보 진료 이동 팝업
	작성일 : 2023. 01. 19.
	작성자 : NTsoft
	수정일 	        수정자 	 수정내용
	-------         -------- ---------------------------
	2023. 01. 19.	NTsoft 	 최초 생성
*/
let treatmentProcedureTreatmentIdUpdateModal = {
	callback : null,
	reqParam : null,
	dataList : null,
	/* 모달 초기화 작업 */
	init : function () {
		$('#treatmentProcedureTreatmentIdUpdateModal').remove();
		$('body').append( this.HtmlTemplate);
		//그리드 이벤트
		this.setEvent();
	},
	/* 그리드 이벤트 설정 */
	setEvent:function() {
		$('#treatmentProcedureTreatmentIdUpdateModal .btnCancle').on("click", function(){
			treatmentProcedureTreatmentIdUpdateModal.close();
		});
		
		$('#treatmentProcedureTreatmentIdUpdateModal .btnSave').on("click", function(){
			treatmentProcedureTreatmentIdUpdateModal.save("I");
		});
	},
	/* 데이터 선택 */
	setSelect:function(selectNumber) {
		let checkedData = dataList[selectNumber];
		if(dataList==null||checkedData==undefined){
			return ;
		}
		
		if( treatmentProcedureTreatmentIdUpdateModal.callback )  {
			treatmentProcedureTreatmentIdUpdateModal.callback(checkedData);
		}
		treatmentProcedureTreatmentIdUpdateModal.close();
	},
	//저장
	save:function(state) {
		let selectId          = treatmentProcedureTreatmentIdUpdateModal.reqParam.selectId;
		let muTreatmentId = $("#treatmentProcedureTreatmentIdUpdateModal #modalTreatmentId").val();
		
		if(true != fn_emptyCheck(selectId)){
			modalEvent.warning("수정", "비활성화 대상 정보가 없습니다.");	
			return;
		}
		
		if(true != fn_emptyCheck(muTreatmentId)){
			modalEvent.warning("수정", "진료이동 정보가 없습니다.");	
			return;
		}
		
		modalEvent.info("수정", "선택한 시술 정보를 진료이동 하시겠습니까?", function(){
			let formData = new FormData();
			formData.append("menuClass", menuClass);
			formData.append("selectId", selectId);
			formData.append("muTreatmentId", muTreatmentId);
	
			$.ajax({
				url: encodeURI('/webtreatmentprocedure/updateTreatmentProcedureTreatmentId.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( treatmentProcedureTreatmentIdUpdateModal.callback )  {
								treatmentProcedureTreatmentIdUpdateModal.callback();
							}
							treatmentProcedureTreatmentIdUpdateModal.close();
						});
					}
					else{
						modalEvent.danger("수정 오류", data.msgDesc);
					}
				},
				error : function(xhr, status, error) {
					modalEvent.danger("수정 오류", "등록 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
				},
				beforeSend:function(){
					
				},
				complete:function(){
					
				}
			});
		});
	},
	/*
	 * 팝업
	 * callback : 콜백함수, reqParam: 팝업파라미터
	 */
	popup:function(callback, reqParam) {
		// 초기화 후 열기
		treatmentProcedureTreatmentIdUpdateModal.init();
		
		//콜백함수 지정.
		treatmentProcedureTreatmentIdUpdateModal.callback = callback;
		treatmentProcedureTreatmentIdUpdateModal.reqParam = reqParam;
		treatmentProcedureTreatmentIdUpdateModal.dataList = null;
		
		//조직
		fn_insertModal_selectListTreatmentOption();
		
		setTimeout(function(){
			$('#treatmentProcedureTreatmentIdUpdateModal').modal('show')
		}, 200);

	},
	/*
	 * 팝업 닫기
	 */
	close:function() {
		$('#treatmentProcedureTreatmentIdUpdateModal').modal('hide');

		setTimeout(function(){
			$('#treatmentProcedureTreatmentIdUpdateModal').remove();
		},500);
	},
	HtmlTemplate : `
	<div id="treatmentProcedureTreatmentIdUpdateModal" class="modal diagnosis_move_modal fade" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<p class="title">진료 이동</p>
				</div>
	
				<div class="modal-body">
					<div class="form_box">
						<div class="input_box">
							<label>진료 구분</label>
							
							<div class="select_box dropdown">
								<button class="label" class="label dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">진료구분</button>
								<input type="hidden" id="modalTreatmentId"></input>
								
								<ul class="select_option_list dropdown-menu"></ul>
							</div>
						</div>
					</div>
				</div>

				<div class="modal-footer">
					<div class="btn_box">
						<button type="button" class="cancel_btn btnCancle">취소</button>
						<button type="button" class="save_bth btnSave">저장</button>
					</div>
				</div>
			</div>
		</div>
	</div>
    `
};

/****************************************************************************
* 조직 조회
****************************************************************************/
function fn_insertModal_selectListTreatmentOption(){
	let formData = new FormData();
	
	$.ajax({
		url: encodeURI('/webtreatment/selectListTreatmentOption.do'),
		data: formData,
		dataType: "json",
		processData: false,
		contentType: false,
		type: 'POST',
		async: true,
		success: function(data){
			if('0'==data.msgCode){
				let listHTML = '';
				
				for(let i = 0; i < data.rows.length; i++){
					let muTreatmentId     = data.rows[i].muTreatmentId;
					let treatmentName     = data.rows[i].treatmentName;
					
					listHTML += '<li id="li_'+muTreatmentId+'" onClick="fn_insertModal_clickSelectTreatmentValue(this);" class="option_list_item ">'+treatmentName+'</li>';
				}
				$("#treatmentProcedureTreatmentIdUpdateModal #modalTreatmentId").siblings("ul.select_option_list").empty().html(listHTML).trigger("create");
				
			}
			else if(data.msgCode=='-1'){
				modalEvent.warning("조회 오류", data.msgDesc);
			}
			else{
				modalEvent.danger("조회 오류", "조회 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
			}
		},
		error : function(xhr, status, error) {
			modalEvent.danger("조회 오류", "조회 중 오류가 발생하였습니다. 잠시후 다시시도하십시오.");
		},
		beforeSend:function(){
			
		},
		complete:function(){
			
		}
	});
}

/****************************************************************************
* 조직 select box 선택 값 셋팅
****************************************************************************/
function fn_insertModal_clickSelectTreatmentValue(target){
	let selectIdArr = $(target).prop("id").split('_');
	let selectId    = selectIdArr[1];
	let selectName  = $(target).text();
	
	$(target).parent().siblings("button").text(selectName);
	$(target).parent().siblings("input").val(selectId);
}