"use strict";
/**
 * mplus_modal_collaborationdetail
 */
var mplus_modal_collaborationdetail = function(){
	// 상속
	mplus_common.call(this);
	
	// 상위 객체 생성
	//var mplusPatient = new mplus_mobile_patient();
	//var mplusNursing = new mplus_nursing();
	
	//super
	var self = this;
	var varCoDetailHistoryList = [];
	//==== 변수 { ====
	//==== 변수 } ====
		
	/**
	 * 초기화
	 */
	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();
		//####################################################################################################################################
		//#################################조건을 서버에서 가져 올 경우 서버호출 > 콜백 > 서버호출 > 콜백으로 처리해야함 #####################
		//##    initCollaborationHistory > processSearchCollaborationHistory > initCollaborationDetail > processSearchCollaborationDetail   ##
		//####################################################################################################################################
		initCollaborationHistory();
		
		
	};
	
	var initEmptyData = function(){
		varCoDetailHistoryList = [];
		
		$("#selCollaborationHistory").empty();
		
		$("#spanCollaborationDetailDeptNm").text("");
		$("#spanCollaborationDetailDoctorNm").text("");
		$("#spanCollaborationDetailDiagnosisNm").text("");
		$("#spanCollaborationDetailOperationDtNm").text("");
		$("#spanCollaborationDetailCollaborationReResqDt").text("");
		$("#spanCollaborationDetailCollaborationDeptDocNm").text("");
		$("#pCollaborationDetailCollaborationReqTxt").text("");
		$("#pCollaborationDetailCollaborationResTxt").text("");
	};
	
	var initCollaborationHistory = function(){
		var param = {
				userId:gLoginUserId,
				patientId:gPatientId,
				deptCd:gPatientDeptCd,
				doctorId:gPatientDoctorId,
				// 20190523 dkchoi75 이력 API input 시 접수번호 receiptNo 추가
			    receiptNo:gPatientReceiptNo
		};
		
		self.medical.getCoDetailHistoryList(param, processSearchCollaborationHistory);
	};
	var processSearchCollaborationHistory = function(lists){
				
		$("#selCollaborationHistory").empty();
		var strDisplayTemp = "";
		varCoDetailHistoryList = lists;
		if(lists == undefined || lists.length == 0){
			strDisplayTemp = "<option value=\"\">협진 이력이 없습니다.</option>";
			$("#selCollaborationHistory").append(strDisplayTemp);
			$("#selCollaborationHistory").val($("#selCollaborationHistory option:first").val());
			return;
		}
		
		if(lists != undefined){
			for(var i=0; i<lists.length; i++){
				var strTailTemp = "";
				if(lists[i].collaborationDeptNm != undefined && lists[i].collaborationDeptNm != ""){
					strTailTemp += lists[i].collaborationDeptNm
				}
				if(lists[i].collaborationDrNm != undefined && lists[i].collaborationDrNm != ""){
					strTailTemp += " / " + lists[i].collaborationDrNm;
				}

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

				// strDisplayTemp += "<option value=\""+ i + "\" >"+ moment(lists[i].collaborationReqDt).format("YYYY-MM-DD") + " " + strTailTemp + "</option>";
				strDisplayTemp += "<option value=\""+ i + "\" >"+ varCollaborationReqDt + " " + strTailTemp + "</option>";
			}
		}
		$("#selCollaborationHistory").append(strDisplayTemp);
				
		$("#selCollaborationHistory").val($("#selCollaborationHistory option:first").val());
		
		
		initCollaborationDetail();
	};
	var initCollaborationDetail = function(){
		var param = {
				userId:				gLoginUserId,
				patientId:			gPatientId,
				collaborationReqDt:	varCoDetailHistoryList[$("#selCollaborationHistory").val()].collaborationReqDt != undefined ? varCoDetailHistoryList[$("#selCollaborationHistory").val()].collaborationReqDt : "",
				deptCd:				varCoDetailHistoryList[$("#selCollaborationHistory").val()].deptCd != undefined ? varCoDetailHistoryList[$("#selCollaborationHistory").val()].deptCd : "",
				doctorId:			varCoDetailHistoryList[$("#selCollaborationHistory").val()].doctorId != undefined ? varCoDetailHistoryList[$("#selCollaborationHistory").val()].doctorId : "",
				collaborationDeptCd:varCoDetailHistoryList[$("#selCollaborationHistory").val()].collaborationDeptCd != undefined ? varCoDetailHistoryList[$("#selCollaborationHistory").val()].collaborationDeptCd : "",
				collaborationDrId:	varCoDetailHistoryList[$("#selCollaborationHistory").val()].collaborationDrId != undefined ? varCoDetailHistoryList[$("#selCollaborationHistory").val()].collaborationDrId : "",
				// 20190521 dkchoi75 백병원 협진 상세 조회 API, Input Parameter receiptNo(접수번호) 추가
				receiptNo:			varCoDetailHistoryList[$("#selCollaborationHistory").val()].receiptNo != undefined ? varCoDetailHistoryList[$("#selCollaborationHistory").val()].receiptNo : ""

		};
		
		self.medical.getCollaborationDetail(param, processSearchCollaborationDetail);
	};
	
	var processSearchCollaborationDetail = function(lists){
		//console.log(JSON.stringify(lists));
		if(lists != undefined){
			var varCollaborationDetailDoctorNmTxt = "";
			var varCollaborationDetailCollaborationDeptDocNmTxt = "";
			//var varCollaborationReqDt = (lists[0].collaborationReqDt != null)? moment(lists[0].collaborationReqDt, "YYYYMMDDHHmmss").format("YYYY-MM-DD HH:mm") : "";
			//var varCollaborationResDt = (lists[0].collaborationResDt != null)? moment(lists[0].collaborationResDt, "YYYYMMDDHHmmss").format("YYYY-MM-DD HH:mm") : "";
			var varCollaborationReqDt = "";
			var varCollaborationResDt = "";
			if(lists[0].collaborationReqDt != undefined && lists[0].collaborationReqDt != "" && lists[0].collaborationReqDt != null) {
				varCollaborationReqDt = moment(lists[0].collaborationReqDt, "YYYYMMDDHHmmss").format("YYYY-MM-DD HH:mm");
			} else {
				varCollaborationReqDt = lists[0].collaborationReqDt;
			}

			if(lists[0].collaborationResDt != undefined && lists[0].collaborationResDt != "" && lists[0].collaborationResDt != null) {
				varCollaborationResDt = moment(lists[0].collaborationResDt, "YYYYMMDDHHmmss").format("YYYY-MM-DD HH:mm");
			} else {
				varCollaborationResDt = lists[0].collaborationResDt;
			}

			if(lists[0].doctorTel != undefined && lists[0].doctorTel != "" && lists[0].doctorTel != null)
				varCollaborationDetailDoctorNmTxt = "<a href=\"sms:"+lists[0].doctorTel+"\" class=\"sms\"><i>문자</i></a> <a href=\"tel:"+lists[0].doctorTel+"\" class=\"tel\"><i>전화</i></a>";
			if(lists[0].collaborationDrTel != undefined && lists[0].collaborationDrTel != "" && lists[0].collaborationDrTel != null)
				varCollaborationDetailCollaborationDeptDocNmTxt ="<a href=\"sms:"+lists[0].collaborationDrTel+"\" class=\"sms\"><i>문자</i></a> <a href=\"tel:"+lists[0].collaborationDrTel+"\" class=\"tel\"><i>전화</i></a>";

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

			$("#spanCollaborationDetailDeptNm").text(lists[0].deptNm);
			$("#spanCollaborationDetailDoctorNm").append(lists[0].doctorNm + varCollaborationDetailDoctorNmTxt);
			$("#spanCollaborationDetailDiagnosisNm").text(lists[0].diagnosisNm);
			// $("#spanCollaborationDetailOperationDtNm").text(lists[0].operationDt + " / " + lists[0].operationNm);
			$("#spanCollaborationDetailOperationDtNm").text(varOperationDt + " / " + lists[0].operationNm);
			$("#spanCollaborationDetailCollaborationReResqDt").text(varCollaborationReqDt + " / " + varCollaborationResDt);
			$("#spanCollaborationDetailCollaborationDeptDocNm").append(lists[0].collaborationDeptNm + " / " + lists[0].collaborationDrNm + varCollaborationDetailCollaborationDeptDocNmTxt);
			//$("#pCollaborationDetailCollaborationReqTxt").text(lists[0].collaborationReqTxt);
			$("#pCollaborationDetailCollaborationReqTxt").append(lists[0].collaborationReqTxt);
			$("#pCollaborationDetailCollaborationResTxt").append(lists[0].collaborationResTxt);
		}
	};

	var emptyCollaborationDetailView = function(){
		$("#spanCollaborationDetailDeptNm").text("");
		$("#spanCollaborationDetailDoctorNm").text("");
		$("#spanCollaborationDetailDiagnosisNm").text("");
		$("#spanCollaborationDetailOperationDtNm").text("");
		$("#spanCollaborationDetailCollaborationReResqDt").text("");
		$("#spanCollaborationDetailCollaborationDeptDocNm").text("");
		$("#pCollaborationDetailCollaborationReqTxt").text("");
		$("#pCollaborationDetailCollaborationResTxt").text("");
	};
	//==== 초기화 설정 } ====
	
	//==== 이벤트 설정 { ====
	var addEvent = function(){
		//$("#btnLogout").on("click", function(){
		//	logout();
		//});
		
		eventColumnClick();
		
		eventRowClick();
		
		eventConditionClick();
	};
	
	var eventConditionClick = function(){
		$("#selCollaborationHistory").off("change");
		$("#selCollaborationHistory").on("change", function(){
			emptyCollaborationDetailView();
			initCollaborationDetail();
		});
	};
	
	var eventColumnClick = function(){

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