"use strict";
/**
 * mplus_modal_patientinfo
 */
var mplus_modal_patientinfo = function(){
	// 상속
	mplus_common.call(this);
	
	// 상위 객체 생성
	//var mplusPatient = new mplus_mobile_patient();
	//var mplusNursing = new mplus_nursing();
	
	//super
	var self = this;
	
	//==== 변수 { ====
	var varResultLists = [];
	var varDeptCd = "";	// 진료과 코드
	var varDeptNm = "";	// 진료과 명
	var varDoctorId = "";	// 의사 ID
	var varDoctorNm = "";	// 의사명
	//==== 변수 } ====
		
	/**
	 * 초기화
	 */
	this.init = function(){ //여기서 모든화면 콤보박스데이터를 집어넣자!!! 그래야지 화면이동할때 속도가 오래걸리지않음!!!
		// 접근 권한 체크
		checkAccessPermission();
		
		// 초기 화면 조건 구성
		initCondition();
		
		// 이벤트 초기화
		addEvent();
		
		//  환자 정보 화면 출력
		getPatientInfo();
	};
	
	//==== 접근 권한 체크 { ====
	var checkAccessPermission = function(){
/*		if( !self.common.checkAccessJobKind( "1000")){
			self.alertTrue("접근 권한이 없습니다.", function(){ $("#modalPatientInfo").modal("hide")});
		}
			
		if( !self.common.checkAccessUserAuth( "01")){
			self.alertTrue("접근 권한이 없습니다.", function(){ $("#modalPatientInfo").modal("hide");});
		}
		
		if( !self.common.checkAccessJobKind( "1000")){
			self.common.disableElements( $("#myModalBtn"));
		} else{
			self.common.enableElements( $("#myModalBtn"));
		}
		
		self.common.disableElements( $("#btnCollaborationDetail"));
		self.common.disableElements( $("#btnInternWorkSchDetail"));*/
		
		checkAccessRights();
	};
	//==== 접근 권한 체크 } ====
	
	//==== 초기화 설정 { ====
	var initCondition = function(){
		// 초기 화면 조건 구성
	};
	//==== 초기화 설정 } ====
	
	//==== 이벤트 설정 { ====
	var addEvent = function(){
		eventPatientInfoClick();
	};
	
	var eventPatientInfoClick = function(){
		$("#btnPatientInfoMedicalHistory").off("click");
		$("#btnPatientInfoMedicalHistory").on("click", function(){
			$("#modalMedicalHistory").modal("show");
			mplusModalMedicalhistory.init();
		});	
		
		$("#btnPatientInfoMedicalRecord").off("click");
		$("#btnPatientInfoMedicalRecord").on("click", function(){
			$("#modalMedicalRecord").modal("show");
			mplusModalMedicalrecord.init();
		});	
		
		$("#btnPrescriptionPrescription").off("click");
		$("#btnPrescriptionPrescription").on("click", function(){
			$("#modalPrescription").modal("show");
			mplusModalPrescription.init();
		});	
		
		$("#btnPatientInfoExamList").off("click");
		$("#btnPatientInfoExamList").on("click", function(){
			$("#modalExamList").modal("show");
			mplusModalExamlist.init();
		});	
		
		$("#btnPatientInfoOperation").off("click");
		$("#btnPatientInfoOperation").on("click", function(){
			$("#modalOperationDetail").modal("show");
			mplusModalOperationdetail.init();
		});	
		
		$("#btnPatientInfoTreatment").off("click");
		$("#btnPatientInfoTreatment").on("click", function(){
			$("#modalTreatmentDetail").modal("show");
			mplusModalTreatmentdetail.init();
		});	
		
		$("#btnVitalList").off("click");
		$("#btnVitalList").on("click", function(){
			$("#modalVitalList").modal("show");
			mplusModalVitallist.init();
		});	
		
		$("#btMedicationList").off("click");
		$("#btMedicationList").on("click", function(){
			$("#modalMedicationList").modal("show");
			mplusModalMedicationlist.init();
		});	
		
		$("#btnPatientInfoCollaboration").off("click");
		$("#btnPatientInfoCollaboration").on("click", function(){
			$("#modalCollaborationDetail").modal("show");
			mplusModalCollaborationdetail.init();
		});	
		
		// 20190228 shinsunwoo 환부 이미지 촬영 버튼 클릭
		$("#btnWoundImg").off("click");
		$("#btnWoundImg").on("click", function(){
			readPicture();
		});			
	};
	//==== 이벤트 설정 } ====
	
	//==== 서비스 실행 { ====
	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(){$("#modalPatientInfo").modal("hide");});
			}
		} else {
			// 환자 정보 화면을 종료함
			self.alertTrue( "환자 정보 접근 권한이 없습니다.", function(){$("#modalPatientInfo").modal("hide");});
		}
	};
	
	var getPatientInfo = function(){
		var param = {
				userId: gLoginUserId,
				patientId: gPatientId, 
				receiptNo: gPatientReceiptNo,
				treatCls: gPatientTreatCls
				};
		self.medical.getPatientInfo(param, processGetPatientInfo);
	};
	
	var processGetPatientInfo = function(lists){
		// 1. 환자 정보 조회 결과를 varResultLists로 복사
		varResultLists = lists;	
		// 3. 환자 정보 결과를 화면에 disaplay
		drawPatientInfo();
		
		// 4. 글로벌 변수 처리
		if(lists[0].hospitalizationDt != undefined && lists[0].hospitalizationDt != "" && lists[0].hospitalizationDt != null){
			self.common.setHospitalizationDt(lists[0].hospitalizationDt);
		}
	};
	
	// 환자 정보 화면에 그리기
	var drawPatientInfo = function(){
		var lists = varResultLists;
		var strDisplayTemp = "";
		$("#divPatientInfoResult").empty();
		
		//2019.03.12. 진료기록 29990030 오픈
		//var tempMedirecordCSS = "control-disabled";
		
		
		for( var i = 0; i < lists.length; i++){
//			strDisplayTemp += "<hr>" 
//				+ "<div>"
//				+ "  <p>"
//				+ "  <label>생년월일:</label><span>" + lists[i].birthDt + "</span>"
//				+ "  <label>전화번호:</label>" + lists[i].cellphoneNo + "<span></span><a href=\"tel:" + lists[i].cellphoneNo + "\">[전화걸기]</a><a href=\"sms:" + lists[i].cellphoneNo + "\">[문자 보내기]</a>"
//				+ "  </p>"
//				+ "  <p>"
//				+ "  <label>내원구분:</label><span>" + self.common.displayTreatCls( gPatientTreatCls) + "</span>"
//				+ "  <label>병동/병상:</label><span>" + lists[i].wardNm + " / " + lists[i].roomNm + "</span>"
//				+ "  </p>"
//				+ "  <p>"
//				+ "  <label>진료의:</label><span>" + lists[i].doctorNm + "</span>"
//				+ "  <label>주치의:</label><span>" + lists[i].specialistNm + "</span>"
//				+ "  </p>"
//				+ "  <p>"
//				+ "  <label>주진단명:</label><span>" + lists[i].diagnosisNm + "</span>"
//				+ "  </p>"
//				+ "  <p>"
//				+ "  <label>수술일/주수술명:</label><span>" + lists[i].operationDt + " / " + lists[i].operationNm + "</span>"
//				+ "  </p>"
//				+ "</div>"
//				+ "<hr>"
//				+ "<div>"
//				+ "  <button type=\"button\" class=\"btn btn-info\" id=\"btnPatientInfoMedicalHistory\">과거이력</button>"
//				+ "  <button type=\"button\" class=\"btn btn-info\" id=\"btnPatientInfoMedicalRecord\">진료기록</button>"
//				+ "  <button type=\"button\" class=\"btn btn-info\" id=\"btnPrescriptionPrescription\">처방조회</button>"
//				+ "  <button type=\"button\" class=\"btn btn-info\" id=\"btnPatientInfoExamList\">검사결과</button>"
//				+ "  <button type=\"button\" class=\"btn btn-info\" id=\"btnPatientInfoCollaboration\">협진내역</button>"
//				+ "  <button type=\"button\" class=\"btn btn-info\" id=\"btnPatientInfoOperation\">수술내역</button>"
//				+ "  <button type=\"button\" class=\"btn btn-info\" id=\"btnPatientInfoTreatment\">시술내역</button>"
//				+ "</div>"
//				+ "<hr>	"
//				+ "<div>"
//				+ "  <button type=\"button\" class=\"btn btn-info\" id=\"btnPatientInfoWoundPicture\">환부이미지관리</button>"
//				+ "</div>"
//				+ "<hr>	"
//				+ "<div>"
//				+ "  <label>Notice:</label>"
//				+ "  <textarea class=\"form-control\" rows=\"5\" id=\"areaPatientInfoNoticeTxt\"></textarea>"
//				+ "</div>"
//				;
			
			var tmpIsExamResult = lists[i].isExamResult == "1" ? " on" : "";
			var tmpIsCollaboration = lists[i].isCollaboration == "1" ? " on" : "";
			var tmpIsOperation = lists[i].isOperation == "1" ? " on" : "";
			var tmpIsTreatment = lists[i].isTreatment == "1" ? " on" : "";
			var tmpoperationDt = (moment(lists[i].operationDt).format("YYYY-MM-DD HH:mm") != "Invalid date") ? moment(lists[i].operationDt).format("YYYY-MM-DD HH:mm") : "-";
							
			strDisplayTemp += "<div class=\"chart-view-top\">"
			+ "  <div class=\"row\">"
			+ "    <div class=\"col-xs-12\">"
			+ "      <p class=\"patient-name\">" + lists[i].patientNm + " (" + lists[i].patientId + ")</p>"
			+ "      <p class=\"patient-info\">"
			+ "        <span></span><span><a href=\"sms:" + lists[i].cellphoneNo + "\" class=\"sms\"><i>문자</i></a> <a href=\"tel:" + lists[i].cellphoneNo + "\" class=\"tel\"><i>전화</i></a></span>"
			+ "      </p>"
			+ "    </div>"
			+ "  </div>"
			+ "  <div class=\"row\">"
			+ "    <div class=\"col-xs-6\">"
			+ "      <p class=\"patient-info\">"
			+ "        <span>나이/성별</span> <span>" + lists[i].age + "/" + lists[i].gender + "</span>"
			+ "      </p>"
			+ "    </div>"
			+ "    <div class=\"col-xs-6\">"
			+ "      <p class=\"patient-info\">"
			+ "        <span>진료</span> <span>" + self.common.displayTreatCls( gPatientTreatCls) + "</span>"
			+ "      </p>"
			+ "    </div>"
			+ "  </div>"
			+ "  <div class=\"row\">"
			+ "    <div class=\"col-xs-12\">"
			+ "      <p class=\"patient-info\">"
			+ "        <span>진료과</span> <span>" + lists[i].deptNm + "</span>"
			+ "      </p>"
			+ "    </div>"
			+ "  </div>"
			+ "  <div class=\"row\">"
			+ "    <div class=\"col-xs-12\">"
			+ "      <p class=\"patient-info\">"
			+ "        <span>담당의 / 주치의</span>"
			+ " <span>" 
			+         lists[i].doctorNm + "<a href=\"sms:" + lists[i].doctorCellphoneNo + "\" class=\"smssub\"><i>문자</i></a> <a href=\"tel:" + lists[i].doctorCellphoneNo + "\" class=\"telsub\"><i>전화</i></a>" 
			+ " / " + lists[i].specialistNm + "<a href=\"sms:" + lists[i].specialistCellphoneNo + "\" class=\"smssub\" style=\"margin-left: 5px;\"><i>문자</i></a> <a href=\"tel:" + lists[i].specialistCellphoneNo + "\" class=\"telsub\"><i>전화</i></a>" 
			+ " </span>"
			+ "      </p>"
			+ "    </div>"
			+ "  </div>"
			+ "  <div class=\"row description\">"
			+ "    <div class=\"col-xs-12\">"
			+ "      <p class=\"patient-info\">"
			+ "        <span>주진단명</span> <span class=\"description\">" + lists[i].diagnosisNm + "</span>"
			+ "      </p>"
			+ "    </div>"
			+ "  </div>"
			+ "  <div class=\"row description\">"
			+ "    <div class=\"col-xs-12\">"
			+ "      <p class=\"patient-info\">"
			+ "        <span>수술일 /주 수술 명</span> <span class=\"description\">" + tmpoperationDt+ " / " + lists[i].operationNm + "</span>"
			+ "      </p>"
			+ "    </div>"
			+ "  </div>"
			+ "</div>"
			+ "<!-- /chart-view-top -->"
			+ "<div class=\"chart-view-item \">"
			+ "  <div class=\"row\">"
			+ "    <div class=\"col-xs-12\">"
			+ "      <p class=\"view-item-title\">"
			+ "        <i class=\"icon-patient01\"></i> <span>진료정보</span>"
			+ "      </p>"
			+ "    </div>"
			+ "  </div>"
			+ "  <div class=\"row\">"
			+ "    <div class=\"col-xs-4\" style=\"padding-left: 5px; padding-right: 5px;\">"
			+ "      <button type=\"button\" name=\"treatmentBtn\" class=\"btn\" id=\"btnPatientInfoMedicalHistory\">진료이력</button>"
			+ "    </div>"
			+ "    <div class=\"col-xs-4\" style=\"padding-left: 5px; padding-right: 5px;\">"
			+ "      <button type=\"button\" name=\"treatmentBtn\" class=\"btn\" id=\"btnPrescriptionPrescription\">처방조회</button>"
			+ "    </div>"
//			+ "    <div class=\"col-xs-4\" style=\"padding-left: 5px; padding-right: 5px;\">"
//			+ "      <button type=\"button\" name=\"treatmentBtn\" class=\"btn\" id=\"btnPatientInfoMedicalRecord\">진료기록</button>"
//			+ "    </div>"
			+ "    <div class=\"col-xs-4\" style=\"padding-left: 5px; padding-right: 5px;\">"
			+ "      <button type=\"button\" name=\"treatmentBtn\" class=\"btn " + tmpIsExamResult + "\" id=\"btnPatientInfoExamList\">검사결과</button>"
			+ "    </div>"
			+ "  </div>"
			+ "  <div class=\"row \">"
			+ "    <div class=\"col-xs-4\" style=\"padding-left: 5px; padding-right: 5px;\">"
			+ "      <button type=\"button\" name=\"treatmentBtn\" class=\"btn " + tmpIsOperation + "\" id=\"btnPatientInfoOperation\">수술내역</button>"
			+ "    </div>"
			+ "    <div class=\"col-xs-4\" style=\"padding-left: 5px; padding-right: 5px;\">"
			+ "      <button type=\"button\" name=\"treatmentBtn\" class=\"btn " + tmpIsCollaboration + "\" id=\"btnPatientInfoCollaboration\">협진내역</button>"
			+ "    </div>"
			+ "    <div class=\"col-xs-4\" style=\"padding-left: 5px; padding-right: 5px;\">"
			+ "      <button type=\"button\" name=\"treatmentBtn\" class=\"btn " + tmpIsTreatment + "\" id=\"btnPatientInfoTreatment\">시술내역</button>"
			+ "    </div>"
			+ "  </div>"
			//20190528 dkchoi75 백병원 별 환자정보_서비스 메뉴 미노출 처리
//			if (gClientIp == "14.40.0.113" || gClientIp == "14.40.4.137" || gClientIp == "100.100.251.251" || gClientIp == "10.10.10.36" || gClientIp == "127.0.0.1") {
			//	console.log(gClientIp);
			+ "  <div class=\"row \">"
			+ "    <div class=\"col-xs-4\" style=\"padding-left: 5px; padding-right: 5px;\">"
			+ "      <button type=\"button\" name=\"treatmentBtn\" class=\"btn\" id=\"btnVitalList\">임상관찰</button>"
			+ "    </div>"
			+ "    <div class=\"col-xs-4\" style=\"padding-left: 5px; padding-right: 5px;\">"
			+ "      <button type=\"button\" name=\"treatmentBtn\" class=\"btn\" id=\"btMedicationList\">투약조회</button>"
			+ "    </div>"
			+ "  </div>"

//			}

//			+ "  <div class=\"row \">"
//			+ "    <div class=\"col-xs-4\">"
//			+ "      <button type=\"button\" name=\"treatmentBtn\" class=\"btn col-xs-4 control-disabled\">임상관찰조회</button>"
//			+ "    </div>"
//			+ "    <div class=\"col-xs-4 px-0\">"
//			+ "      <button type=\"button\" name=\"treatmentBtn\" class=\"btn col-xs-4 control-disabled\">투약정보</button>"
//			+ "    </div>"
//			+ "    <div class=\"col-xs-4\">"
//			+ "      <button type=\"button\" name=\"treatmentBtn\" class=\"btn col-xs-4 control-disabled\">환부 이미지 조회</button>"
//			+ "    </div>"
//			+ "  </div>"
//			+ "</div>"
//			+ "<div class=\"chart-view-item \">"
//			+ "  <div class=\"row \">"
//			+ "    <div class=\"col-xs-12\">"
//			+ "      <p class=\"view-item-title\">"
//			+ "        <i class=\"icon-patient02\"></i><span>조치</span>"
//			+ "      </p>"
//			+ "    </div>"
//			+ "  </div>"
//			+ "  <div class=\"row \">"
//			+ "    <div class=\"col-xs-4\">"
//			+ "      <button type=\"button\" name=\"treatmentBtn\" class=\"btn icon-btn camera-icon control-disabled\" id=\"btnWoundImg\">환부 촬영</button>"
//			+ "    </div>"
//			+ "    <div class=\"col-xs-4 px-0\">"
//			+ "      <button type=\"button\" name=\"treatmentBtn\" class=\"btn icon-btn eye-icon control-disabled\">임상 관찰</button>"
//			+ "    </div>"
//			+ "    <div class=\"col-xs-4 \">"
//			+ "      <button type=\"button\" name=\"treatmentBtn\" class=\"btn icon-btn medicine-icon control-disabled\">투약 실시</button>"
//			+ "    </div>"
//			+ "  </div>"
//			+ "</div>"
//			+ "<div class=\"chart-notice\">"
//			+ "  <div class=\"row \">"
//			+ "    <div class=\"col-sm-12\">"
//			+ "      <p class=\"notice\">2018-12-12, 홍길동</p>"
//			+ "    </div>"
//			+ "    <div class=\"col-sm-12\">"
//			+ "    	<p class=\"text\">환자가 알약 투약이 어려우니 처방된 약을 분말로...</p>"
//			+ "    </div>"
//			+ "  </div>"
//			+ "</div>"
			;
			
			// 20190228 shinsunwoo 환부촬영 이미지에서 사용할 변수에 값을 할당
			varDeptCd = lists[i].deptCd;	// 진료과 코드
			varDeptNm = lists[i].deptNm;	// 진료과 명
			varDoctorId = lists[i].doctorId;	// 의사 ID
			varDoctorNm = lists[i].doctorNm;	// 의사명
		}
	
		$("#divPatientInfoResult").append(strDisplayTemp);	// element 추가
		eventPatientInfoClick();	// 클릭 이벤트 처리
		
		// notice 를 추가해줌
		getPatientNotice();
	}
	
	// notice를 조회
	var getPatientNotice = function(){
		var param = {
				userId: gLoginUserId,
				patientId: gPatientId
				};
		self.medical.getPatientNotice(param, processGetPatientNotice);
	};
	
	// notice 처리
	var processGetPatientNotice = function(lists){
		var strDisplayTemp = "";
		
		if( lists.length < 0){
			return;
		}
		
		// 초기 notice div element 추가
		strDisplayTemp = "<div class=\"chart-notice\"></div>";
		$("#divPatientInfoResult").append(strDisplayTemp);	// element 추가
		strDisplayTemp = "";
		
		//$(".chart-notice").empty();		
		for( var i = 0; i < lists.length; i++){
			//strDisplayTemp += lists[i].noticeTxt + " , " + lists[i].noticeWriter + " , " + lists[i].writeDtTM + "\n";
			strDisplayTemp += "<div class=\"row\">"
			+ "  <div class=\"col-sm-12\">"
			+ "    <p class=\"notice\">" + lists[i].writeDtTm + ", " + lists[i].noticeWriter + "</p>"
			+ "  </div>"
			+ "  <div class=\"col-sm-12\">"
			+ "    <p class=\"text\">" + lists[i].noticeTxt + "</p>"
			+ "  </div>"
			+ "</div>"
			;

		}
		$(".chart-notice").html(strDisplayTemp);
	};
	//==== 서비스 실행 } ====
	
	// 20190228 shinsunwoo 환자의 환부이미지 추출 toNative 호출
	var readPicture = function( param_deptNm, param_doctorNm){
		console.log('call readPicture');
		var serviceUrl = contextPath + "/mobile/medical/inputWoundImg.json";
		var msg = {
				type : "command", 
				functionType : "readPicture", 
				value : {
					hospitalCD : "01",
					userId: gLoginUserId,
					patientId: gPatientId, 
					patientNm: gPatientNm,
					age: gPatientAge,
					gender: gPatientGender,
					deptCd: varDeptCd,
					deptNm: varDeptNm,
					doctorId: varDoctorId,
					doctorNm : varDoctorNm,
					treatCls: gPatientTreatCls,
					serviceUrl: serviceUrl,
					callbackFn : "window.activeObj.readBarcodeCallBack"
				}
		}; 
		self.toNative( msg );
	};

	// 20190228 shinsunwoo 환자의 바코드 추출 callback 처리
	this.readPictureCallBack = function( returnCode){
		var results = new Object();
		if( typeof returnCode === "string" ){			
			results = self.util.parseJson( returnCode );
		} else {
			results = returnCode;
		}
		
		if( results.success != undefined && results.success == "true" ) {
			console.log("readPicture : success");
			console.log( results.result);			
		} else {
			console.log("readPicture : failed");
			self.alert( "readPicture APP function 호출 오류 " );
		}
	};
};