/**
 * mplus_mobile_setting
 */
var mplus_mobile_setting = 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 $btnSettingSave = $('#btnSettingSave'); 
	
	/**
	 * 초기화
	 */
	this.init = function(){
		addEvent();
		initDataSetting();
		
	};
	/**
	 * 이벤트 등록
	 */
	var addEvent = function(){
		$btnSettingSave.on('click', function(){
			saveSatting();
		});
	};
	
	/**
	 * 기초데이터 셋팅
	 */
	var initDataSetting = function(){
		//속성선택
		/*var userAttrList = self.setting.getUserAttrList(JSON.parse(localStorage.user));
		bindingUserAttrList(userAttrList);*/
		
		//부서선택
		var deptList = JSON.parse(localStorage.user).departmentList;
		
		$('#tableDept').empty();
		
		for(var i=0; i<deptList.length; i++){
			  var tdRadio = td.clone().addClass('tdRadio').append(
			  			div.clone().addClass('radio3 radio-check').append(
			  				$('<input></input>').clone().attr('name', 'deptRadio').attr('type', 'radio').attr('id', 'deptRadio'+i).attr('value', deptList[i].departmentCd).attr('deptName', deptList[i].departmentNm)
						).append(
								$('<label></label>').clone().attr('for', 'deptRadio'+i).text('')	
						)
				);
			  
			  
			  var tdDeptName = td.clone().attr('colspan', '3').append(deptList[i].departmentNm);
			  					  
			  var trDept = tr.clone().append(tdRadio).append(tdDeptName);
			  
			  $('#tableDept').append(trDept);
			};
			
		$('input[name=deptRadio][value="'+localStorage.getItem('selectDeptCode')+'"]').prop('checked', true);
		
		/*if(userAttrList.length==0){
			$('#boxAttr').css('display', 'none');
		}else{
			$('#boxAttr').css('display', 'block');
		};
		*/
		if(deptList.length==0){
			$('#boxDept').css('display', 'none');
		}else{
			$('#boxDept').css('display', 'block');
		};
		
		if(deptList.length==0){ /*userAttrList.length==0 && */
			$('#boxSetBtn').css('display', 'none');
		}else{
			$('#boxSetBtn').css('display', 'block');
		}
	};
		
	bindingUserAttrList = function(bindUserAttrList){
		$('#tableAttr').empty();
		userAttrList = bindUserAttrList;
		
		 for(var i=0; i<userAttrList.length; i++){
			  var tdRadio = td.clone().addClass('tdRadio').append(
			  			div.clone().addClass('radio3 radio-check').append(
			  				$('<input></input>').clone().attr('name', 'attrRadio').attr('type', 'radio').attr('id', 'attrRadio'+i).attr('value', userAttrList[i].userAttrOrder)
						).append(
								$('<label></label>').clone().attr('for', 'attrRadio'+i).text('')	
						)
				);
			  
			  
			  var tdHospital = td.clone().append(userAttrList[i].hospitalName);
			  var tdDuty = td.clone().append(userAttrList[i].dutyName);
			  var tdWork = td.clone().append(userAttrList[i].workName);
			  					  
			  var trAttr = tr.clone().append(tdRadio).append(tdHospital).append(tdDuty).append(tdWork);
			  
			  $('#tableAttr').append(trAttr);
			};
			
		$('input[name=attrRadio][value="'+JSON.parse(localStorage.getItem('user')).useAttrOrder+'"]').prop('checked', true);
		
		$('table tr').unbind();
		$('table tr').click(function(){
		    $(this).find('input[type=radio]').prop('checked', true);
		});
	};
	
	var saveSatting = function(){
		/*if(JSON.parse(localStorage.getItem('user')).userAttrOrder != $('input[name=attrRadio]:checked').val()){
			var param = {
					'userId': JSON.parse(localStorage.getItem('user')).userId,
					'userAttrOrder': Number($('input[name=attrRadio]:checked').val())
			};
			self.setting.changeUserAttr(param);
		};
		*/
		if(localStorage.getItem('selectDeptCode')!= $('input[name=deptRadio]:checked').val()){
			localStorage.setItem('selectDeptCode', $('[name=deptRadio]:checked').val());
			localStorage.setItem('selectDeptName', $('[name=deptRadio]:checked')[0].attributes.deptName.value);
		};
		
		self.alertTrue('저장되었습니다.', function(callback){
			location.reload();
		});
	};
	
	/*saveAttrSuccess = function(){
		var userInfo = JSON.parse(localStorage.getItem('user'));
		
		userInfo.useAttrOrder = Number($('input[name=attrRadio]:checked').val());
		
		localStorage.setItem('user', JSON.stringify(userInfo));		
	}*/
};
