"use strict";
/**
 * mplus_modal_medicalhistory
 */
var mplus_modal_medicalhistory = 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(){ //여기서 모든화면 콤보박스데이터를 집어넣자!!! 그래야지 화면이동할때 속도가 오래걸리지않음!!!
		// 초기 화면 조건 구성
		initCondition();
		
		// 이벤트 초기화
		addEvent();		
	};
	
	//==== 초기화 설정 { ====
	var initCondition = function(){
		// 초기 화면 조건 구성
		// 일자 설정
		initStartEndDate();
		
		searchMedicalHis();
	};
	
	var searchMedicalHis = function(){
		var param = {
				userId:gLoginUserId,
				patientId:gPatientId,
				startDt: moment($("#txtInMedicalHisStartDt").val()).format("YYYYMMDD"),
				endDt: moment($("#txtInMedicalHisEndDt").val()).format("YYYYMMDD")
		};
		self.medical.getOpHistoryList(param, processSearchMedicalOpHisList);
		self.medical.getInHistoryList(param, processSearchMedicalInHisList);
		self.medical.getOutHistoryList(param, processSearchMedicalOutHisList);
	};
	
	var processSearchMedicalInHisList = function(lists){
		//console.log(JSON.stringify(lists));

		var strDisplayTemp = "";
		$("#divMedicalInHistoryResultRows").empty();
		for( var i = 0; i < lists.length; i++){
			
			strDisplayTemp += "<div class=\"row list-group-item\" patientNo=\"" + i + "\" patientId=\"" + lists[i].patientId + "\" style=\"overflow: hidden; padding-left: 0px; padding-right: 0px;\">"
			+ "  <div class=\"col-xs-12 col-sm-6 col-lg-6\">"
			+ "    <div class=\"col-xs-2 col-sm-3 col-lg-3 text-center\" style=\"padding: 0px;\">입원</div>"
			+ "    <div class=\"col-xs-4 col-sm-3 col-lg-3 text-center\" style=\"padding: 0px;\">" + ((moment(lists[i].hospitalizationDt).format("YYYY-MM-DD") != "Invalid date") ? moment(lists[i].hospitalizationDt).format("YYYY-MM-DD") : "-") + "</div>"
			+ "    <div class=\"col-xs-3 col-sm-3 col-lg-3 text-center\" style=\"padding: 0px;\">" + lists[i].deptNm + "</div>"
			+ "    <div class=\"col-xs-3 col-sm-3 col-lg-3 text-center\" style=\"padding: 0px;\">" + lists[i].doctorNm + "</div>"
			+ "  </div>"
			+ "  <div class=\"col-xs-12 col-sm-6 col-lg-6\">"
			+ "    <div class=\"col-xs-4 col-sm-3 col-lg-3\" style=\"padding: 0px;\">" + ((lists[i].wardNm != undefined) ? (lists[i].wardNm) : "") 
															   + ((lists[i].roomNm != undefined) ? ("<p>" + lists[i].roomNm+"/") : "") 
															   + ((lists[i].bedNm != undefined) ? (lists[i].bedNm + "</p>") : "</p>") + "</div>"
			+ "    <div class=\"col-xs-8 col-sm-3 col-lg-3\" style=\"padding: 0px;\">" + lists[i].diagnosisNm + "</div>"
			+ "  </div>"
			+ "</div>"
			;
			//console.log( i + " : " + lists[i].deptNm);
		}
	
		$("#divMedicalInHistoryResultRows").append(strDisplayTemp);	// element 추가
	};
	
	var processSearchMedicalOutHisList = function(lists){
		//console.log(JSON.stringify(lists));

		var strDisplayTemp = "";
		$("#divMedicalOutHistoryResultRows").empty();
		for( var i = 0; i < lists.length; i++){
			
			strDisplayTemp += "<div class=\"row list-group-item\" patientNo=\"" + i + "\" patientId=\"" + lists[i].patientId + "\" style=\"overflow: hidden; padding-left: 0px; padding-right: 0px;\">"
			+ "  <div class=\"col-xs-12 col-sm-6 col-lg-6\">"
			+ "    <div class=\"col-xs-2 col-sm-3 col-lg-3 text-center\" style=\"padding: 0px;\">외래</div>"
			+ "    <div class=\"col-xs-4 col-sm-3 col-lg-3 text-center\" style=\"padding: 0px;\">" + ((moment(lists[i].visitDt).format("YYYY-MM-DD") != "Invalid date") ? moment(lists[i].visitDt).format("YYYY-MM-DD") : "-") + "</div>"
			+ "    <div class=\"col-xs-3 col-sm-3 col-lg-3 text-center\" style=\"padding: 0px;\">" + lists[i].deptNm + "</div>"
			+ "    <div class=\"col-xs-3 col-sm-3 col-lg-3 text-center\" style=\"padding: 0px;\">" + lists[i].doctorNm + "</div>"
			+ "  </div>"
			+ "  <div class=\"col-xs-12 col-sm-6 col-lg-6\">"
			+ "    <div class=\"col-xs-4 col-sm-3 col-lg-3\" style=\"padding: 0px;\">" + ((lists[i].wardNm != undefined) ? (lists[i].wardNm+"/") : "") 
															   + ((lists[i].roomNm != undefined) ? (lists[i].roomNm+"/") : "") 
															   + ((lists[i].bedNm != undefined) ? (lists[i].bedNm) : "") + "</div>"
			+ "    <div class=\"col-xs-8 col-sm-3 col-lg-3\" style=\"padding: 0px;\">" + lists[i].diagnosisNm + "</div>"
			+ "  </div>"
			+ "</div>"
			;
			//console.log( i + " : " + lists[i].deptNm);
		}
	
		$("#divMedicalOutHistoryResultRows").append(strDisplayTemp);	// element 추가
	};
	
	var processSearchMedicalOpHisList = function(lists){
		//console.log(JSON.stringify(lists));

		var strDisplayTemp = "";
		$("#divMedicalOpHistoryResultRows").empty();
		for( var i = 0; i < lists.length; i++){
			
			strDisplayTemp += "<div class=\"row list-group-item\" patientNo=\"" + i + "\" patientId=\"" + lists[i].patientId + "\" style=\"overflow: hidden; padding-left: 0px; padding-right: 0px;\">"
			+ "  <div class=\"col-xs-12 col-sm-6 col-lg-6\">"
			+ "    <div class=\"col-xs-2 col-sm-3 col-lg-3 text-center\" style=\"padding: 0px;\">수술</div>"
			+ "    <div class=\"col-xs-4 col-sm-3 col-lg-3 text-center\" style=\"padding: 0px;\">" + ((moment(lists[i].operationDt).format("YYYY-MM-DD") != "Invalid date") ? moment(lists[i].operationDt).format("YYYY-MM-DD") : "-") + "</div>"
			+ "    <div class=\"col-xs-3 col-sm-3 col-lg-3 text-center\" style=\"padding: 0px;\">" + lists[i].deptNm + "</div>"
			+ "    <div class=\"col-xs-3 col-sm-3 col-lg-3 text-center\" style=\"padding: 0px;\">" + lists[i].operationDrNm + "</div>"
			+ "  </div>"
			+ "  <div class=\"col-xs-12 col-sm-6 col-lg-6\">"
			+ "    <div class=\"col-xs-4 col-sm-3 col-lg-3\" style=\"padding: 0px;\">" + ((lists[i].wardNm != undefined) ? (lists[i].wardNm+"/") : "") 
															   + ((lists[i].roomNm != undefined) ? (lists[i].roomNm+"/") : "") 
															   + ((lists[i].bedNm != undefined) ? (lists[i].bedNm) : "") + "</div>"
			+ "    <div class=\"col-xs-8 col-sm-3 col-lg-3\" style=\"padding: 0px;\">" + lists[i].operationNm + "</div>"
			+ "  </div>"
			+ "</div>"
			;
			//console.log( i + " : " + lists[i].deptNm);
		}
	
		$("#divMedicalOpHistoryResultRows").append(strDisplayTemp);	// element 추가
	};
	
	var initStartEndDate = function(){
		//오늘일자 로드
		var varToday = new Date();
			
		//일요일이면 차주 가져오기
		//다른 요일이면 해당 요일 처리
		$("#txtInMedicalHisEndDt").val( moment(varToday).format("YYYY-MM-DD") );
		$("#txtInMedicalHisStartDt").val( moment().subtract(1, 'months').format("YYYY-MM-DD") );
		
		
	};
	//==== 초기화 설정 } ====
	
	//==== 이벤트 설정 { ====
	var addEvent = function(){
		//$("#btnLogout").on("click", function(){
		//	logout();
		//});
		
		eventColumnClick();
		
		eventRowClick();
		
		eventConditionClick();
	};
	
	var eventConditionClick = function(){
		// 1일
		$("#btnMedicalHisOneDaySearch").off("click"); // 기존 이벤트 해제
		$("#btnMedicalHisOneDaySearch").on("click", function(e){
			$("#txtInMedicalHisStartDt").val( moment().subtract(1, 'days').format("YYYY-MM-DD") );
			searchMedicalHis();
		});
		// 1달 버튼클릭
		$("#btnMedicalHisOneMonthSearch").off('click'); // 기존 이벤트 해제
		$("#btnMedicalHisOneMonthSearch").on('click', function(e){
			$("#txtInMedicalHisStartDt").val( moment().subtract(1, 'months').format("YYYY-MM-DD") );
			searchMedicalHis();
		});
		// 3달 버튼클릭
		$("#btnMedicalHisThreeMonthSearch").off('click'); // 기존 이벤트 해제
		$("#btnMedicalHisThreeMonthSearch").on('click', function(e){
			$("#txtInMedicalHisStartDt").val( moment().subtract(3, 'months').format("YYYY-MM-DD") );
			searchMedicalHis();
		});
		// 6달 버튼클릭
		$("#btnMedicalHisSixMonthSearch").off('click'); // 기존 이벤트 해제
		$("#btnMedicalHisSixMonthSearch").on('click', function(e){
			var varSelDate = new Date($("#txtInMedicalHisEndDt").val());
			varSelDate.setDate(varSelDate.getDate() - 1);
			varSelDate.setMonth(varSelDate.getMonth() - 6);
			$("#txtInMedicalHisStartDt").val( moment(varSelDate).format("YYYY-MM-DD") );
		});
		// 1년 버튼클릭
		$("#btnMedicalHisOneYearSearch").off('click'); // 기존 이벤트 해제
		$("#btnMedicalHisOneYearSearch").on('click', function(e){
			var varSelDate = new Date($("#txtInMedicalHisEndDt").val());
			varSelDate.setDate(varSelDate.getDate() - 1);
			varSelDate.setFullYear(varSelDate.getFullYear() - 1);
			$("#txtInMedicalHisStartDt").val( moment(varSelDate).format("YYYY-MM-DD") );
		});
		// 조회버튼
		$("#btnMedicalHisSearch").off('click'); // 기존 이벤트 해제
		$("#btnMedicalHisSearch").on('click', function(e){
			//로우 초기화
			$("#divMedicalInHistoryResultRows").empty();
			$("#divMedicalOutHistoryResultRows").empty();
			$("#divMedicalOpHistoryResultRows").empty();
			
			searchMedicalHis();
		});
		
	};
	
	var eventColumnClick = function(){

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