"use strict";
/**
 * mplus_modal_treatmentdetail
 */
var mplus_modal_treatmentdetail = function(){
	// 상속
	mplus_common.call(this);
	
	// 상위 객체 생성
	//var mplusPatient = new mplus_mobile_patient();
	//var mplusNursing = new mplus_nursing();
	
	//super
	var self = this;
	var varTrDetailHistoryList = [];
	//==== 변수 { ====
	//==== 변수 } ====
		
	/**
	 * 초기화
	 */
	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();
		//####################################################################################################################################
		//#################################조건을 서버에서 가져 올 경우 서버호출 > 콜백 > 서버호출 > 콜백으로 처리해야함 #####################
		//##    initTreatmentHistory > processSearchTreatmentHistory > initTreatmentDetail > processSearchTreatmentDetail   ##
		//####################################################################################################################################
		initTreatmentHistory();
		
		
	};
	
	var initEmptyData = function(){
		varTrDetailHistoryList = [];
		
		$("#selTreatmentHistory").empty();
		
		$("#spanTreatmentDetailWardRoomBed").text("");
		$("#spanTreatmentDeptDrNm").text("");
		$("#spanTreatmentStatNm").text("");
		$("#spanTreatmentStartDt").text("");
		$("#spanTreatmentCntrRoomNm").text("");
		$("#spanTreatmentDiagnosisNm").text("");
		$("#spanTreatmentNm").text("");
		$("#spanTreatmentStartEndTm").text("");
		$("#spanTreatmentRmk").text("");
	};
	
	var initTreatmentHistory = function(){
		var param = {
				userId:gLoginUserId,
				patientId:gPatientId,
				treatmentDeptCd:gPatientDeptCd,
				treatmentDrId:gPatientDoctorId,
				// 20190523 dkchoi75 이력 API input 시 접수번호 receiptNo 추가
				receiptNo:gPatientReceiptNo
		};
		
		self.medical.getTrDetailHistoryList(param, processSearchTreatmentHistory);
	};
	var processSearchTreatmentHistory = function(lists){
				
		$("#selTreatmentHistory").empty();
		var strDisplayTemp = "";
		varTrDetailHistoryList = lists;
		if(lists == undefined || lists.length == 0){
			strDisplayTemp = "<option value=\"\">시술 이력이 없습니다.</option>";
			$("#selTreatmentHistory").append(strDisplayTemp);
			$("#selTreatmentHistory").val($("#selTreatmentHistory option:first").val());
			return;
		}
		
		if(lists != undefined){
			for(var i=0; i<lists.length; i++){
				var strTailTemp = "";
				if(lists[i].treatmentDeptNm != undefined && lists[i].treatmentDeptNm != ""){
					strTailTemp += lists[i].treatmentDeptNm
				}
				if(lists[i].treatmentDrNm != undefined && lists[i].treatmentDrNm != ""){
					strTailTemp += " / " + lists[i].treatmentDrNm;
				}

				var varTreatmentDt = "";
				if(lists[i].treatmentDt != undefined && lists[i].treatmentDt != "" && lists[i].treatmentDt != null) {
					varTreatmentDt = moment(lists[i].treatmentDt).format("YYYY-MM-DD");
				} else {
					varTreatmentDt = lists[i].treatmentDt;
				}

				// strDisplayTemp += "<option value=\""+ i + "\" >"+ moment(lists[i].treatmentDt).format("YYYY-MM-DD") + " " + strTailTemp + "</option>";
				strDisplayTemp += "<option value=\""+ i + "\" >"+ varTreatmentDt + " " + strTailTemp + "</option>";
			}
		}
		$("#selTreatmentHistory").append(strDisplayTemp);
		
		$("#selTreatmentHistory").val($("#selTreatmentHistory option:first").val());
		
		
		initTreatmentDetail();
	};
	var initTreatmentDetail = function(){
		var param = {
		        hospitalCd:         localStorage.hospitalCd,
				userId:				gLoginUserId,
				patientId:			gPatientId,
				treatmentDt:		varTrDetailHistoryList[$("#selTreatmentHistory").val()].treatmentDt != undefined ? varTrDetailHistoryList[$("#selTreatmentHistory").val()].treatmentDt : "",
	    		treatmentDeptCd:	varTrDetailHistoryList[$("#selTreatmentHistory").val()].treatmentDeptCd != undefined ? varTrDetailHistoryList[$("#selTreatmentHistory").val()].treatmentDeptCd : "",
		    	treatmentDrId:		varTrDetailHistoryList[$("#selTreatmentHistory").val()].treatmentDrId != undefined ? varTrDetailHistoryList[$("#selTreatmentHistory").val()].treatmentDrId : "",
			    treatmentCntrCd:	varTrDetailHistoryList[$("#selTreatmentHistory").val()].treatmentCntrCd != undefined ? varTrDetailHistoryList[$("#selTreatmentHistory").val()].treatmentCntrCd : "",
			  //treatmentRoomCd:	varTrDetailHistoryList[$("#selTreatmentHistory").val()].treatmentRoomCd != undefined ? varTrDetailHistoryList[$("#selTreatmentHistory").val()].treatmentRoomCd : "",
				// 20190521 dkchoi75 백병원 시술 상세 조회 API, Input Parameter receiptNo(접수번호) 추가
				receiptNo:			varTrDetailHistoryList[$("#selTreatmentHistory").val()].receiptNo != undefined ? varTrDetailHistoryList[$("#selTreatmentHistory").val()].receiptNo : ""

		};
		
		self.medical.getTreatmentDetail(param, processSearchTreatmentDetail);
	};
	
	var processSearchTreatmentDetail = function(lists){
		//console.log(JSON.stringify(lists));
		if(lists != undefined){
			var varTreatmentDetailWardRoomBed = ((lists[0].wardNm != null)? lists[0].wardNm : "");
			varTreatmentDetailWardRoomBed = varTreatmentDetailWardRoomBed + " / " + ((lists[0].roomNm != null)? lists[0].roomNm : "");
			varTreatmentDetailWardRoomBed = varTreatmentDetailWardRoomBed + " / " + ((lists[0].bedNm != null)? lists[0].bedNm : "");
			var varTreatmentDeptDrNm = ((lists[0].treatmentDeptNm != null)? lists[0].treatmentDeptNm : "");
			varTreatmentDeptDrNm = varTreatmentDeptDrNm + " / " + ((lists[0].treatmentDrNm != null)? lists[0].treatmentDrNm : "");
			// var varTreatmentStartDt = (lists[0].treatmentStartDt != null)? moment(lists[0].treatmentStartDt).format("YYYY-MM-DD") : "";
			var varTreatmentStartDt = "";
			if(lists[0].treatmentStartDt != undefined && lists[0].treatmentStartDt != "" && lists[0].treatmentStartDt != null) {
				varTreatmentStartDt = moment(lists[0].treatmentStartDt).format("YYYY-MM-DD");
			} else {
				varTreatmentStartDt = lists[0].treatmentStartDt;
			}
			var varTreatmentCntrRoomNm = ((lists[0].treatmentCntrNm != null)? lists[0].treatmentCntrNm : "");

			// var varTreatmentStartEndTm = (lists[0].treatmentStartTm != null)? moment("20000101T" + lists[0].treatmentStartTm).format("HH:mm:ss") : "";
			var varTreatmentStartEndTm = "";
			if(lists[0].treatmentStartTm != undefined && lists[0].treatmentStartTm != "" && lists[0].treatmentStartTm != null) {
				varTreatmentStartEndTm = moment("20000101T" + lists[0].treatmentStartTm).format("HH:mm:ss");
			} else {
				varTreatmentStartEndTm = lists[0].treatmentStartTm;
			}

			var varTreatmentEndTm = "";
			if(lists[0].treatmentEndTm != undefined && lists[0].treatmentEndTm != "" && lists[0].treatmentEndTm != null) {
				varTreatmentEndTm = moment("20000101T" + lists[0].treatmentEndTm).format("HH:mm:ss");
			} else {
				varTreatmentEndTm = lists[0].treatmentEndTm;
			}

			//varTreatmentStartEndTm = varTreatmentStartEndTm + " ~ " + ((lists[0].treatmentEndTm != null)? moment("20000101T" + lists[0].treatmentEndTm).format("HH:mm:ss") : "");
			varTreatmentStartEndTm = varTreatmentStartEndTm + " ~ " + varTreatmentEndTm;

			$("#spanTreatmentDetailWardRoomBed").text(varTreatmentDetailWardRoomBed);
			$("#spanTreatmentDeptDrNm").text(varTreatmentDeptDrNm);
			$("#spanTreatmentStatNm").text(lists[0].treatmentStatNm);
			$("#spanTreatmentStartDt").text(varTreatmentStartDt);
			$("#spanTreatmentCntrRoomNm").text(varTreatmentCntrRoomNm);
			$("#spanTreatmentDiagnosisNm").text(lists[0].diagnosisNm);
			$("#spanTreatmentNm").text(lists[0].treatmentNm);
			$("#spanTreatmentStartEndTm").text(varTreatmentStartEndTm);
			$("#spanTreatmentRmk").text(lists[0].operationRmk);
		}
	};

	var emptyTreatmentDetailView = function(){
		$("#spanTreatmentDetailWardRoomBed").text("");
		$("#spanTreatmentDeptDrNm").text("");
		$("#spanTreatmentStatNm").text("");
		$("#spanTreatmentStartDt").text("");
		$("#spanTreatmentCntrRoomNm").text("");
		$("#spanTreatmentDiagnosisNm").text("");
		$("#spanTreatmentNm").text("");
		$("#spanTreatmentStartEndTm").text("");
		$("#spanTreatmentRmk").text("");
	};
	//==== 초기화 설정 } ====
	
	//==== 이벤트 설정 { ====
	var addEvent = function(){
		//$("#btnLogout").on("click", function(){
		//	logout();
		//});
		
		eventColumnClick();
		
		eventRowClick();
		
		eventConditionClick();
	};
	
	var eventConditionClick = function(){
		$("#selTreatmentHistory").off("change");
		$("#selTreatmentHistory").on("change", function(){
			emptyTreatmentDetailView();
			initTreatmentDetail();
		});
	};
	
	var eventColumnClick = function(){

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