/**
 * mplus_mobile_treatment
 */
var mplus_mobile_treatment = function(){
	// 상속
	mplus_common.call(this);
	// super
	var self = this; 
	
	//변수
	var div = $("<div></div>");
	var tr = $("<tr></tr>");
	var th = $("<th></th>");
	var td = $("<td></td>");
	var table = $("<table></table>");
	var li = $("<li></li>");
	var a = $("<a></a>");
	var span = $("<span></span>");
	var button = $("<button></button>");
	
	var $sltDept = $('#sltDept'), 
		$scheduleList = $('#scheduleList'); 
	
	var scheduleList = [];
	var openFlag = false;
	
	$(window).on('load resize',function(){
		if( self.isTablet() ) {
			$('#treatmentList table').css({
			    'width': '100%', 
			    'max-width': '100%' 
			});
		}else{
			$('#treatmentList table').css({
			    'width': 'max-content', 
			    'max-width': 'initial' 
			});
		};
    });	
	
	/**
	 * 초기화
	 */
	this.init = function(){
		addEvent();
		initDataSetting();
		searchScheduleList(localStorage.selectDeptCode);
	};
	/**
	 * 이벤트 등록
	 */
	var addEvent = function(){
		$('#sltDept').change(function(){
			searchScheduleList($('#sltDept').val());
		});
	};
	
	/**
	 * 기초데이터 셋팅
	 */
	var initDataSetting = function(){
		var deptList = [];
		self.hospital.getDeptList('D', function(result){
			deptList = result;
			
			$('#sltDept').empty();
			for(var i=0; i<deptList.length; i++){
				option = $('<option></option>').clone().attr('value', deptList[i].departmentCd)
														.attr('type', deptList[i].departmentTyp)
														.text(deptList[i].departmentNm);
				$('#sltDept').append(option);
			};
			$('#sltDept').val(localStorage.selectDeptCode);
			
			if($('#sltDept').val()==null){
				$("#sltDept").val($("#sltDept option:first").val());
			};
		});
	};
	
	var searchScheduleList = function(deptCode){
		$scheduleList.empty();
		scheduleList = [];
		var param = {
					departmentCd: deptCode
				};
		self.schedule.getTreatSchedule(param);
		
	};
	
	bindingPatientList = function(bindScheduleList, extraMsg){
		$scheduleList.empty();
		scheduleList =  bindScheduleList;
			
		if(scheduleList.length==0){
			$scheduleList.append(tr.clone().addClass('').append(
					td.clone().addClass('text-center no-result').attr('colspan', '13').text('―')));
		};
		
		for(var i=0; i<scheduleList.length; i++){
			var trWeekday = tr.clone().addClass('row-style').append(
								td.clone().addClass('td-doctor').attr('data-row', i).text(scheduleList[i].doctorNm)
							).append(
								td.clone().append(div.clone().addClass(scheduleList[i].MonAm=='G'? 'circle': scheduleList[i].MonAm=='S'? 'diamond': ''))
							).append(
								td.clone().append(div.clone().addClass(scheduleList[i].MonPm=='G'? 'circle': scheduleList[i].MonPm=='S'? 'diamond': ''))
							).append(
								td.clone().addClass('td-shade').append(div.clone().addClass(scheduleList[i].TueAm=='G'? 'circle': scheduleList[i].TueAm=='S'? 'diamond': ''))
							).append(
								td.clone().addClass('td-shade').append(div.clone().addClass(scheduleList[i].TuePm=='G'? 'circle': scheduleList[i].TuePm=='S'? 'diamond': ''))
							).append(
								td.clone().append(div.clone().addClass(scheduleList[i].WedAm=='G'? 'circle': scheduleList[i].WedAm=='S'? 'diamond': ''))
							).append(
								td.clone().append(div.clone().addClass(scheduleList[i].WedPm=='G'? 'circle': scheduleList[i].WedPm=='S'? 'diamond': ''))
							).append(
								td.clone().addClass('td-shade').append(div.clone().addClass(scheduleList[i].ThuAm=='G'? 'circle': scheduleList[i].ThuAm=='S'? 'diamond': ''))
							).append(
								td.clone().addClass('td-shade').append(div.clone().addClass(scheduleList[i].ThuPm=='G'? 'circle': scheduleList[i].ThuPm=='S'? 'diamond': ''))
							).append(
								td.clone().append(div.clone().addClass(scheduleList[i].FriAm=='G'? 'circle': scheduleList[i].FriAm=='S'? 'diamond': ''))
							).append(
								td.clone().append(div.clone().addClass(scheduleList[i].FriPm=='G'? 'circle': scheduleList[i].FriPm=='S'? 'diamond': ''))
							).append(
								td.clone().addClass('td-shade').append(div.clone().addClass(scheduleList[i].SatAm=='G'? 'circle': scheduleList[i].SatAm=='S'? 'diamond': ''))
							).append(
								td.clone().addClass('td-shade').append(div.clone().addClass(scheduleList[i].SatPm=='G'? 'circle': scheduleList[i].SatPm=='S'? 'diamond': ''))
							)
			$scheduleList.append(trWeekday);
		};
		
		$('.td-doctor').on('click', function(e){
			var rowCount = e.currentTarget.getAttribute("data-row");
			
			var scheduleInfo = scheduleList[rowCount];
			var docTel = scheduleInfo.doctorTel + "";
			
			$('#doctorNm').text(scheduleInfo.doctorNm);
			$('#doctorSpecialArea').text(scheduleInfo.speciality);
			$('#doctorTel').text(scheduleInfo.doctorTel);
			if(scheduleInfo.doctorTel==undefined||scheduleInfo.doctorTel==null||scheduleInfo.doctorTel==''){
				$('#doctorTelBtn').css('display', 'none');
			}else{
				//2018.05.02. 원내번호 입력 시 연락 버튼 감춤
				if(docTel.indexOf("-") != -1){
					var arrlocNum = docTel.split("-");
					if(arrlocNum[0] != "010"){
						$('#doctorTelBtn').css('display', 'none');
					}else{
						$('#doctorTelBtn').css('display', 'inline-block');
					}
				}else{
					$('#doctorTelBtn').css('display', 'none');
				}
			};	
			$('#modalDoctorInfo').modal('show');
			
			$('#doctorTelBtn').unbind();
			$('#doctorTelBtn').on('click', function(e){
				if(scheduleInfo.doctorTel==undefined||scheduleInfo.doctorTel==null||scheduleInfo.doctorTel==''){
					self.alert('해당 의사의 전화번호가 존재하지 않습니다.');
					return;
				};
				location.href = 'tel:'+scheduleInfo.doctorTel;
			});
		});
		$('#treatmentList').scrollLeft(0);
	};
};
