/**
 * admin/pacs
 */
var mplus_admin_pacs = function(){
	//상속
	mplus_admin.call(this);
	
	var self = this;
	//변수
	var $crudServiceBaseUrl = contextPath + "/admin/log";
	/**
	 * 초기화
	 */
	this.init = function(){
		initLogList(1);
		setPageList(1);
		if($('#page_1')!=null) $('#page_1').addClass('active');
		addEvent();
	};
	/**
	 * 이벤트 등록
	 */
	var addEvent = function(){

	};
	var logList = [];
	var totalCount = '';
	var saveType = '';
	var pageSize = 10;
	var logType = 'PACS';
	
    /**
     * 그리드 초기화
     */
    var initLogList = function(page){
    	var opt = {
				url: $crudServiceBaseUrl + "/getList.json",
				async: false,
				type: "post",
				dataType: "json",
				contentType: "application/json",
				data: self.util.stringifyJson({
					pageSize: pageSize, 
					page: page,
					logType: logType
				})
			};
    	var success = function(result){
    		logList = result.data;
    		totalCount = result.totalCount;
    		$('#table_log').empty();
    		$('.table-wrapper-lg').scrollTop(0);
    		for(var i=0; i<logList.length; i++){
    			var tr = $('<tr></tr>').clone().attr('seq', i);
    			var tdTime = $('<td></td>').clone().text(moment(logList[i].createDt).format('YYYY-MM-DD HH:mm:ss'));
    			var tdHospital = $('<td></td>').clone().text(logList[i].content3 == '01'? '신촌': logList[i].content3 == '10' ? '강남' : '-' );
    			var tdName = $('<td></td>').clone().text(logList[i].userName + '(' + logList[i].userId + ')');
    			var tdDept = $('<td></td>').clone().text(logList[i].deptName);
    			var tdTitle = $('<td></td>').clone().text(logList[i].title);
    			var tdContent = $('<td></td>').clone().text(logList[i].content);
    			var tdContent2 = $('<td></td>').clone().text(logList[i].content2);
    			
    			$('#table_log').append(tr.append(tdTime).append(tdHospital)
    					.append(tdName).append(tdDept).append(tdTitle).append(tdContent).append(tdContent2));
    		}
    	};
    	var error = function(xhr,d,t){
    		console.log(xhr);
			console.log(t);
    	};
    	self.ajaxAdmin( opt, success, error );
    };
    
    var pageEvent = function(e) {
    	var type = e.currentTarget.id.split('_')[0];
    	var page = Number(e.currentTarget.id.split('_')[1]);
    	
    	if( type == 'page' ) {
    		initLogList(page);
    		$('#ulPagination li[id*="page_"]').removeClass('active');
    	} else if ( type == 'next') {
    		initLogList(page);
    		setPageList(page);
    	} else if ( type == 'previous' ) {
    		initLogList(page);
    		setPageList(page - 9);
    	}
    	
    	if($('#page_'+page)!=null) $('#page_'+page).addClass('active');
    };
    
    var setPageList = function(start) {
    	$('#ulPagination').empty();
    	
    	var end = start + 9;
    	if( Math.ceil(totalCount/pageSize) < end ) {
    		end = Math.ceil(totalCount/pageSize);
    	}
    	
    	if( start > 10 ) {
    		$('#ulPagination').append('<li id="previous_'+(start - 1)+'"><a href="#"><<</a></li>');
    	}
    	    	
    	for( var i = start; i <= end; i++ ) {
    		$('#ulPagination').append('<li id="page_'+i+'"><a href="#">'+i+'</a></li>');
    	}
    	
    	if( end < Math.ceil(totalCount/pageSize) ) {
    		$('#ulPagination').append('<li id="next_'+(end + 1)+'"><a href="#">>></a></li>');
    	}
    	
    	$('#ulPagination li').unbind();
    	$('#ulPagination li').click(function(e) {pageEvent(e);});
    	
    };
};