"use strict";
/**
 * mplus_modal_vitallist
 */
var mplus_modal_vitallist = function(){
	// 상속
	mplus_common.call(this);
	
	// 상위 객체 생성
	//var mplusPatient = new mplus_mobile_patient();
	//var mplusNursing = new mplus_nursing();
	
	//super
	var self = this;
	
	//==== 변수 { ====
	var varResultLists = [];
	
	//==== 변수 } ====
		
	/**
	 * 초기화
	 */
	this.init = function(){ //여기서 모든화면 콤보박스데이터를 집어넣자!!! 그래야지 화면이동할때 속도가 오래걸리지않음!!!
		//데이터
		initData();		
		
		// 초기 화면 조건 구성
		initCondition();
		
		// 이벤트 초기화
		addEvent();		
	};
	
	//==== 초기화 설정 { ====
	var initData = function(){
		if($("#sltVitalListSearch").val() != "1"){
			$("#sltVitalListSearch").val("1");//임상관찰조회
			divVitalListHeaderChange();
		}
		$("#divVitalListRows").empty();
	};
	
	var initCondition = function(){
		// 초기 화면 조건 구성
		// 일자 설정
		initStartEndDate();
		
		searchVitallist();
	};
	
	var searchVitallist= function(){
		//임상관찰조회 검색기간이 7일 이상 차이 날 경우 return
		
		if($("#sltVitalListSearch").val() == 1){//임상관찰조회
			//7일 이상 차이 날 경우 처리
			var varVitalListStartDt = moment($("#txtVitalListStartDt").val() , "YYYY-MM-DD");
			var varVitalListEndDt = moment($("#txtVitalListEndDt").val() , "YYYY-MM-DD");
			
			//console.log("날짜이동 : " + moment.duration(varVitalListStartDt.diff(varVitalListEndDt , "day")));
			/*
			if(moment.duration(varVitalListStartDt.diff(varVitalListEndDt , "day")) >= 1 || moment.duration(varVitalListStartDt.diff(varVitalListEndDt , "day")) <= -7){
				self.alert("1주 이상 검색할 수 없습니다.");
				initStartEndDate();
				return;
			}
			*/
			var param = {					
					userId:gLoginUserId,
					patientId:gPatientId,
					hospitalizationDt:moment(gPatientHospitalizationDt).format("YYYYMMDD"),
					startDt:moment($("#txtVitalListStartDt").val()).format("YYYYMMDD"),
					endDt:moment($("#txtVitalListEndDt").val()).format("YYYYMMDD")
			};
			//console.log("TEST : " + JSON.stringify(param));
			
			self.medical.getVitalList(param, processSearchVitalList);
		}else if($("#sltVitalListSearch").val() == 2){//I/O합계조회
			var param = {					
					userId:gLoginUserId,
					patientId:gPatientId,
					hospitalizationDt:moment(gPatientHospitalizationDt).format("YYYYMMDD"),
					searchDt:moment($("#txtVitalListSumEndDt").val()).format("YYYYMMDD")
			};
			//console.log("TEST : " + JSON.stringify(param));
			
			self.medical.getVitalListSum2(param, processSearchVitalListSum);
		}
	};
	
	var processSearchVitalList = function(lists){
		
		//header 초기화
		divVitalListHeaderChange();
		//console.log(JSON.stringify(lists));
		varResultLists = lists;

		var strDisplayTemp = "";
		$("#divVitalListRows").empty();
		for( var i = 0; i < lists.length; i++){
			//임상관찰 데이터 H 높음 , N 평균 , L 낮음
			var pulseEvalCdCSS = "";
			var respirationEvalCdCSS = "";
			var bodyTemperatureEvalCdCSS = "";
			var systolicBpEvalCdCSS = "";
			var diastolicBpEvalCdCSS = "";
			var bloodOxySaturationEvalCdCSS = "";
			
			if(lists[i].pulseEvalCd == "H"){
				pulseEvalCdCSS = "background:#c2d4de;";
			} else if(lists[i].pulseEvalCd == "L"){
				pulseEvalCdCSS = "background:#e2b49a;";
			}
			
			if(lists[i].respirationEvalCd == "H"){
				respirationEvalCdCSS = "background:#c2d4de;";
			} else if(lists[i].respirationEvalCd == "L"){
				respirationEvalCdCSS = "background:#e2b49a;";
			}

			if(lists[i].bodyTemperatureEvalCd == "H"){
				bodyTemperatureEvalCdCSS = "background:#c2d4de;";
			} else if(lists[i].bodyTemperatureEvalCd == "L"){
				bodyTemperatureEvalCdCSS = "background:#e2b49a;";
			}
			
			if(lists[i].systolicBpEvalCd == "H"){
				systolicBpEvalCdCSS = "background:#c2d4de;";
			} else if(lists[i].systolicBpEvalCd == "L"){
				systolicBpEvalCdCSS = "background:#e2b49a;";
			}
			
			if(lists[i].diastolicBpEvalCd == "H"){
				diastolicBpEvalCdCSS = "background:#c2d4de;";
			} else if(lists[i].diastolicBpEvalCd == "L"){
				diastolicBpEvalCdCSS = "background:#e2b49a;";
			}

			if(lists[i].bloodOxySaturationEvalCd == "H"){
				bloodOxySaturationEvalCdCSS = "background:#c2d4de;";
			} else if(lists[i].bloodOxySaturationEvalCd == "L"){
				bloodOxySaturationEvalCdCSS = "background:#e2b49a;";
			}
			
			strDisplayTemp += "<div class=\"row list-group-item\" rowNo=\"" + i + "\" medicalRecordSeq=\"" + lists[i].medicalRecordSeq + "\" style=\"overflow: hidden; padding:0 0 0 0;\">"
			+ "  <div class=\"col-xs-12\" style=\"padding: 0px; align-items: center; display: flex;\">"
			+ "    <div id=\"vitalPTagSize" + i + "\" class=\"col-xs-3 text-center\" style=\"padding: 0px; border-right:1px solid #999999;\">" + ((moment(lists[i].executionDtTm , "YYYYMMDDHHmmss").format("YYYY-MM-DD HH:mm:ss") != "Invalid date") ? (moment(lists[i].executionDtTm , "YYYYMMDDHHmmss").format("MM-DD HH:mm")) : "-") + "</div>"
			+ " <div class=\"col-xs-9 text-center\" style=\"padding: 0px\">"
			+ "    <div class=\"col-xs-2 text-center vitalptag\" style=\"padding: 0px; " + pulseEvalCdCSS + "border-right:1px solid #999999; color:red;\">" + ((lists[i].pulse != null ) ? lists[i].pulse : "") + "</div>"
			+ "    <div class=\"col-xs-2 text-center vitalptag\" style=\"padding: 0px; " + respirationEvalCdCSS + "border-right:1px solid #999999; color:green;\">" + ((lists[i].respiration != null ) ? lists[i].respiration : "") + "</div>"
			+ "    <div class=\"col-xs-2 text-center vitalptag\" style=\"padding: 0px; " + bodyTemperatureEvalCdCSS + "border-right:1px solid #999999; color:blue;\">" + ((lists[i].bodyTemperature != null ) ? lists[i].bodyTemperature : "") + "</div>"
			+ "    <div class=\"col-xs-2 text-center vitalptag\" style=\"padding: 0px; " + systolicBpEvalCdCSS + "border-right:1px solid #999999;\">" + ((lists[i].systolicBp != null ) ? lists[i].systolicBp : "") + "</div>"
			+ "    <div class=\"col-xs-2 text-center vitalptag\" style=\"padding: 0px; " + diastolicBpEvalCdCSS + "border-right:1px solid #999999;\">" + ((lists[i].diastolicBp != null ) ? lists[i].diastolicBp : "") + "</div>"
			+ "    <div class=\"col-xs-2 text-center vitalptag\" style=\"padding: 0px; " + bloodOxySaturationEvalCdCSS + "\">" + ((lists[i].bloodOxySaturation != null ) ? lists[i].bloodOxySaturation : "") + "</div>"
			+ "  </div>"
			+ " </div>"
			+ "</div>"
			;
			//console.log( i + " : " + lists[i].deptNm);
		}
	
		$("#divVitalListRows").append(strDisplayTemp);	// element 추가
		
		if($("#vitalPTagSize0") !== undefined && $("#vitalPTagSize0").height() != null){
			//P 사이즈 수정
			$(".vitalptag").height($("#vitalPTagSize0").height());
			//$(".vitalptag").css("padding-top",($("#vitalPTagSize0").height()/4));			
		}
		//addEventSearchMediRecord(); // row click event 추가
	};
	
	var processSearchVitalListSum = function(lists){
		//header 초기화
		divVitalListSumHeaderChange();
		
		//console.log(JSON.stringify(lists));
		varResultLists = lists;

		var strDisplayTemp = "";
		$("#divVitalListRows").empty();
		for( var i = 0; i < lists.length; i++){
			
			strDisplayTemp += "<div class=\"row list-group-item\" rowNo=\"" + i + "\" medicalRecordSeq=\"" + lists[i].medicalRecordSeq + "\" style=\"overflow: hidden; padding:0 0 0 0;\">"
			+ "  <div id=\"vitalPlusPTagSize" + i + "\" class=\"col-xs-12\" style=\"padding: 0px; align-items: center; display: flex;\">"
			+ "    <div class=\"col-xs-3 text-center\" style=\"padding: 0px; border-right:1px solid #999999;\">" + lists[i].item + "</div>"
			+ "    <div class=\"col-xs-3 text-center\" style=\"padding: 0px; border-right:1px solid #999999;\">" + ((lists[i].total2DaysAgo != null ) ? lists[i].total2DaysAgo : "") + "</div>"
			+ "    <div class=\"col-xs-3 text-center\" style=\"padding: 0px; border-right:1px solid #999999;\">" + ((lists[i].total1DayAgo != null ) ? lists[i].total1DayAgo : "") + "</div>"
			+ "    <div class=\"col-xs-3 text-center\" style=\"padding: 0px;\">" + ((lists[i].totalBaseDay != null ) ? lists[i].totalBaseDay : "") + "</div>"
			+ "  </div>"
			+ "</div>"
			;
			//console.log( i + " : " + lists[i].deptNm);
		}
	
		$("#divVitalListRows").append(strDisplayTemp);	// element 추가
		
		/*
		if($("#vitalPlusPTagSize0") !== undefined && $("#vitalPlusPTagSize0").height() != null){
			//P 사이즈 수정
			$(".vitalplusptag").height($("#vitalPlusPTagSize0").height());
			$(".vitalplusptag").css("padding-top",($("#vitalPlusPTagSize0").height()/4));			
		}*/
		//addEventSearchMediRecord(); // row click event 추가
	};
		
	var initStartEndDate = function(){
		//오늘일자 로드
		var varToday = new Date();
			
		//일요일이면 차주 가져오기
		//다른 요일이면 해당 요일 처리
		/*
		$("#txtVitalListStartDt").val( moment(varToday).format("YYYY-MM-DD") );
		$("#txtVitalListEndDt").val( moment(varToday).format("YYYY-MM-DD") );
		*/
		$("#txtVitalListEndDt").val( moment(varToday).format("YYYY-MM-DD") );
		$("#txtVitalListStartDt").val( moment().subtract(1, 'months').format("YYYY-MM-DD") );

		//I/O합계 일자
		$("#txtVitalListSumEndDt").val( moment(varToday).format("YYYY-MM-DD") );
		
	};
	//==== 초기화 설정 } ====
	
	//==== 이벤트 설정 { ====
	var addEvent = function(){
		//$("#btnLogout").on("click", function(){
		//	logout();
		//});
		
		eventColumnClick();
		
		eventRowClick();
		
		eventConditionClick();
	};
	
	var eventConditionClick = function(){
		// 1일 버튼클릭
		$("#btnVitalListOneDaySearch").off('click'); // 기존 이벤트 해제
		$("#btnVitalListOneDaySearch").on('click', function(e){
			$("#txtVitalListStartDt").val( moment().subtract(1, 'days').format("YYYY-MM-DD") );
			searchVitallist();
		});
		/*
		// 3일 버튼클릭
		$("#btnVitalListThreeDaySearch").off('click'); // 기존 이벤트 해제
		$("#btnVitalListThreeDaySearch").on('click', function(e){
			var varSelDate = new Date($("#txtVitalListEndDt").val());
			varSelDate.setDate(varSelDate.getDate() - 2);
			$("#txtVitalListStartDt").val( moment(varSelDate).format("YYYY-MM-DD") );
		});
		// 1주 버튼클릭
		$("#btnVitalListOneWeekSearch").off('click'); // 기존 이벤트 해제
		$("#btnVitalListOneWeekSearch").on('click', function(e){
			var varSelDate = new Date($("#txtVitalListEndDt").val());
			varSelDate.setDate(varSelDate.getDate() - 6);
			$("#txtVitalListStartDt").val( moment(varSelDate).format("YYYY-MM-DD") );
		});
		*/
		// 1달 버튼클릭
		$("#btnVitalListOneMonthSearch").off("click"); // 기존 이벤트 해제
		$("#btnVitalListOneMonthSearch").on("click", function(e){
			$("#txtVitalListStartDt").val( moment().subtract(1, 'months').format("YYYY-MM-DD") );
			searchVitallist();
		});
		// 3달 버튼클릭
		$("#btnVitalListThreeMonthSearch").off('click'); // 기존 이벤트 해제
		$("#btnVitalListThreeMonthSearch").on('click', function(e){
			$("#txtVitalListStartDt").val( moment().subtract(3, 'months').format("YYYY-MM-DD") );
			searchVitallist();
		});

		// 조회버튼
		$("#btnVitalListSearch").off('click'); // 기존 이벤트 해제
		$("#btnVitalListSearch").on('click', function(e){			
			searchVitallist();
		});
		
		$("#sltVitalListSearch").off("change");
		$("#sltVitalListSearch").on("change", function(){			
			searchVitallist();
		});
		
	};
	
	var divVitalListHeaderChange = function(){
		$(".divVitalListSumView").hide();
		$(".divVitalListView").show();
		var strDisplayTemp = "";
		$("#divVitalListResultHeader").empty();
		strDisplayTemp = "<div class=\"chart-title well\">"
			+ "<div class=\"row\">"
			+ "  <div class=\"col-xs-3 text-center\" style=\"padding: 4px 0px; border-right:1px solid #999999;\">"
			+ "	<p>기록일시</p>"
			+ "  </div>"
			+ " <div class=\"col-xs-9 text-center\" style=\"padding: 0px\">"
			+ "  <div class=\"col-xs-2 text-center\" style=\"padding: 4px 0px; border-right:1px solid #999999; color:red;\">"
			+ "	<p>맥박</p>"
			+ "  </div>"
			+ "  <div class=\"col-xs-2 text-center\" style=\"padding: 4px 0px; border-right:1px solid #999999; color:green;\">"
			+ "	<p>호흡</p>"
			+ "  </div>"
			+ "  <div class=\"col-xs-2 text-center\" style=\"padding: 4px 0px; border-right:1px solid #999999; color:blue;\">"
			+ "	<p>체온</p>"
			+ "  </div>"
			+ "  <div class=\"col-xs-2 text-center\" style=\"padding: 4px 0px; border-right:1px solid #999999;\">"
			+ "	<p>SBP</p>"
			+ "  </div>"
			+ "  <div class=\"col-xs-2 text-center\" style=\"padding: 4px 0px; border-right:1px solid #999999;\">"
			+ "	<p>DBP</p>"
			+ "  </div>"
			+ "  <div class=\"col-xs-2 text-center\" style=\"padding: 4px 0px;\">"
			+ "	<p>SpO2</p>"
			+ "  </div>"
			+ "</div>"
			+ "</div>"
			+ "</div>"
			;
		$("#divVitalListResultHeader").append(strDisplayTemp);
	};
	var divVitalListSumHeaderChange = function(){
		$(".divVitalListView").hide();
		$(".divVitalListSumView").show();
		var strDisplayTemp = "";
		//날짜정보 확인
		var varVitalListSumEndDt  = new Date($("#txtVitalListSumEndDt").val());
		varVitalListSumEndDt.setDate(varVitalListSumEndDt.getDate() - 2);
		
		$("#divVitalListResultHeader").empty();
		strDisplayTemp = "<div class=\"chart-title well\">"
			+ "<div class=\"row\">"
			+ "  <div class=\"col-xs-3 text-center\" style=\"padding: 4px 0px; border-right:1px solid #999999;\">"
			+ "	<p>항목</p>"
			+ "  </div>"
			+ "  <div class=\"col-xs-3 text-center\" style=\"padding: 4px 0px; border-right:1px solid #999999;\">"
			+ "	<p>"+ moment(varVitalListSumEndDt).format("MM월 DD일") +"</p>"
			+ "  </div>";
		varVitalListSumEndDt.setDate(varVitalListSumEndDt.getDate() + 1);
		strDisplayTemp += "  <div class=\"col-xs-3 text-center\" style=\"padding: 4px 0px; border-right:1px solid #999999;\">"
			+ "	<p>"+ moment(varVitalListSumEndDt).format("MM월 DD일") +"</p>"
			+ "  </div>";
		varVitalListSumEndDt.setDate(varVitalListSumEndDt.getDate() + 1);
		strDisplayTemp += "  <div class=\"col-xs-3 text-center\" style=\"padding: 4px 0px; color:red;\">"
			+ "	<p>"+ moment(varVitalListSumEndDt).format("MM월 DD일") +"</p>"
			+ "  </div>"
			+ "  </div>"
			+ "</div>"
			;
	
		$("#divVitalListResultHeader").append(strDisplayTemp);
		
	};
	
	var eventColumnClick = function(){

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