"use strict";
/**
 * mplus_modal_operationdetail
 */
var mplus_modal_operationdetail = function(){
	// 상속
	mplus_common.call(this);
	
	// 상위 객체 생성
	//var mplusPatient = new mplus_mobile_patient();
	//var mplusNursing = new mplus_nursing();
	
	//super
	var self = this;
	var varOpDetailHistoryList = [];
	//==== 변수 { ====
	//==== 변수 } ====
		
	/**
	 * 초기화
	 */
	this.init = function(permissionFlag){ //여기서 모든화면 콤보박스데이터를 집어넣자!!! 그래야지 화면이동할때 속도가 오래걸리지않음!!!
		// 접근 권한 체크
		if(permissionFlag == "Y"){
			checkAccessPermission();
		}
		
		// 초기 화면 조건 구성
		initCondition();
		
		// 이벤트 초기화
		addEvent();		
	};
	
	//접근 권한 체크
	var checkAccessPermission = function(){
		checkAccessRights();
	};
	
	var checkAccessRights = function(){
		var param = {
				userId: gLoginUserId,
				patientId: gPatientId,
				accessCd : "00"
				};
		self.medical.checkAccessRights(param, processCheckAccessRights);
	};
	
	var processCheckAccessRights = function(lists){
		if( lists.length == 1){
			if( lists[0].isAllowed == "0"){
				// 환자 정보 화면을 종료함
				self.alertTrue( "환자 정보 접근 권한이 없습니다, " + lists[0].rejectedMsg, function(){$("#modalCollaborationDetail").modal("hide");});
			}
		} else {
			// 환자 정보 화면을 종료함
			self.alertTrue( "환자 정보 접근 권한이 없습니다.", function(){$("#modalCollaborationDetail").modal("hide");});
		}
	};
	
	//==== 초기화 설정 { ====
	var initCondition = function(){
		// 초기 화면 조건 구성
		initEmptyData();
		//####################################################################################################################################
		//#################################조건을 서버에서 가져 올 경우 서버호출 > 콜백 > 서버호출 > 콜백으로 처리해야함 #####################
		//##    initOperationHistory > processSearchOperationHistory > initOperationDetail > processSearchOperationDetail   ##
		//####################################################################################################################################
		initOperationHistory();
		
		
	};
	
	var initEmptyData = function(){
		varOpDetailHistoryList = [];
		
		$("#selOperationHistory").empty();
		
		$("#spanOperationDetailWardRoomBed").text("");
		$("#spanOperationDeptDrNm").text("");
		$("#spanOperationStatNm").text("");
		$("#spanOperationStartDt").text("");
		$("#spanOperationCntrRoomNm").text("");
		$("#spanOperationDiagnosisNm").text("");
		$("#spanOperationNm").text("");
		$("#spanOperationStartEndTm").text("");
		$("#spanOperationRmk").text("");
	};
	
	var initOperationHistory = function(){
		var param = {
				userId:gLoginUserId,
				patientId:gPatientId,
				operationDeptCd:gPatientDeptCd,
				operationDrId:gPatientDoctorId,
			// 20190523 dkchoi75 이력 API input 시 접수번호 receiptNo 추가
			    receiptNo:gPatientReceiptNo
		};
		
		self.medical.getOpDetailHistoryList(param, processSearchOperationHistory);
	};
	var processSearchOperationHistory = function(lists){
				
		$("#selOperationHistory").empty();
		var strDisplayTemp = "";
		varOpDetailHistoryList = lists;
		if(lists == undefined || lists.length == 0){
			strDisplayTemp = "<option value=\"\">수술 이력이 없습니다.</option>";
			$("#selOperationHistory").append(strDisplayTemp);
			$("#selOperationHistory").val($("#selOperationHistory option:first").val());
			return;
		}
		
		if(lists != undefined){
			for(var i=0; i<lists.length; i++){
				var strTailTemp = "";
				var operationDt = "";
				if(lists[i].operationDeptNm != undefined && lists[i].operationDeptNm != ""){
					strTailTemp += lists[i].operationDeptNm
				}
				if(lists[i].operationDrNm != undefined && lists[i].operationDrNm != ""){
					strTailTemp += " / " + lists[i].operationDrNm;
				}
				
				// strDisplayTemp += "<option value=\""+ i + "\" >"+ moment(lists[i].operationDt).format("YYYY-MM-DD") + " " + strTailTemp + "</option>";
				strDisplayTemp += "<option value=\""+ i + "\" >"+ lists[i].operationDt + " " + strTailTemp + "</option>";
			}
		}
		$("#selOperationHistory").append(strDisplayTemp);
				
		$("#selOperationHistory").val($("#selOperationHistory option:first").val());
		
		
		initOperationDetail();
	};
	var initOperationDetail = function(){
		var param = {
				userId:				gLoginUserId,
				patientId:			gPatientId,
				operationDt:		varOpDetailHistoryList[$("#selOperationHistory").val()].operationDt != undefined ? varOpDetailHistoryList[$("#selOperationHistory").val()].operationDt : "",
				operationDeptCd:	varOpDetailHistoryList[$("#selOperationHistory").val()].operationDeptCd != undefined ? varOpDetailHistoryList[$("#selOperationHistory").val()].operationDeptCd : "",
				operationDrId:		varOpDetailHistoryList[$("#selOperationHistory").val()].operationDrId != undefined ? varOpDetailHistoryList[$("#selOperationHistory").val()].operationDrId : "",
				operationCntrCd:	varOpDetailHistoryList[$("#selOperationHistory").val()].operationCntrCd != undefined ? varOpDetailHistoryList[$("#selOperationHistory").val()].operationCntrCd : "",
				operationRoomCd:	varOpDetailHistoryList[$("#selOperationHistory").val()].operationRoomCd != undefined ? varOpDetailHistoryList[$("#selOperationHistory").val()].operationRoomCd : "",
				// 20190521 dkchoi75 백병원 수술 상세 조회 API, Input Parameter receiptNo(접수번호) 추가
				receiptNo:			varOpDetailHistoryList[$("#selOperationHistory").val()].receiptNo != undefined ? varOpDetailHistoryList[$("#selOperationHistory").val()].receiptNo : ""
		};
		
		self.medical.getOperationDetail(param, processSearchOperationDetail);
	};
	
	var processSearchOperationDetail = function(lists){
		//console.log(JSON.stringify(lists));
		if(lists != undefined){
			var varOperationDetailWardRoomBed = ((lists[0].wardNm != null)? lists[0].wardNm : "");
			varOperationDetailWardRoomBed = varOperationDetailWardRoomBed + " / " + ((lists[0].roomNm != null)? lists[0].roomNm : "");
			varOperationDetailWardRoomBed = varOperationDetailWardRoomBed + " / " + ((lists[0].bedNm != null)? lists[0].bedNm : "");
			var varOperationDeptDrNm = ((lists[0].operationDeptNm != null)? lists[0].operationDeptNm : "");
			varOperationDeptDrNm = varOperationDeptDrNm + " / " + ((lists[0].operationDrNm != null)? lists[0].operationDrNm : "");
			// var varOperationStartDt = (lists[0].operationStartDt != null)? moment(lists[0].operationStartDt).format("YYYY-MM-DD") : "";
			var varOperationStartDt = (lists[0].operationStartDt != null)? lists[0].operationStartDt : "";
			var varOperationCntrRoomNm = ((lists[0].operationCntrNm != null)? lists[0].operationCntrNm : "");
			varOperationCntrRoomNm = varOperationCntrRoomNm + " / " + ((lists[0].operationRoomNm != null)? lists[0].operationRoomNm : "");
			var varOperationStartEndTm = (lists[0].operationStartTm != null)? moment("20000101T" + lists[0].operationStartTm).format("HH:mm:ss") : "";
			varOperationStartEndTm = varOperationStartEndTm + " ~ " + ((lists[0].operationEndTm != null)? moment("20000101T" + lists[0].operationEndTm).format("HH:mm:ss") : "");
	
			$("#spanOperationDetailWardRoomBed").text(varOperationDetailWardRoomBed);
			$("#spanOperationDeptDrNm").text(varOperationDeptDrNm);
			$("#spanOperationStatNm").text(lists[0].operationStatNm);
			$("#spanOperationStartDt").text(varOperationStartDt);
			$("#spanOperationCntrRoomNm").text(varOperationCntrRoomNm);
			$("#spanOperationDiagnosisNm").text(lists[0].diagnosisNm);
			$("#spanOperationNm").text(lists[0].operationNm);
			$("#spanOperationStartEndTm").text(varOperationStartEndTm);
			$("#spanOperationRmk").text(lists[0].operationRmk);
		}
	};

	var emptyOperationDetailView = function(){
		$("#spanOperationDetailWardRoomBed").text("");
		$("#spanOperationDeptDrNm").text("");
		$("#spanOperationStatNm").text("");
		$("#spanOperationStartDt").text("");
		$("#spanOperationCntrRoomNm").text("");
		$("#spanOperationDiagnosisNm").text("");
		$("#spanOperationNm").text("");
		$("#spanOperationStartEndTm").text("");
		$("#spanOperationRmk").text("");
	};
	//==== 초기화 설정 } ====
	
	//==== 이벤트 설정 { ====
	var addEvent = function(){
		//$("#btnLogout").on("click", function(){
		//	logout();
		//});
		
		eventColumnClick();
		
		eventRowClick();
		
		eventConditionClick();
	};
	
	var eventConditionClick = function(){
		$("#selOperationHistory").off("change");
		$("#selOperationHistory").on("change", function(){
			emptyOperationDetailView();
			initOperationDetail();
		});
	};
	
	var eventColumnClick = function(){

	};
	
	var eventRowClick = function(){
		
	};
	//==== 이벤트 설정 } ====
	
	
	//==== 서비스 실행 { ====
	//==== 서비스 실행 { ====
	
};