/**
 * mplus_admin/ msgcheck
 */
var mplus_admin_msgCheck = function(){
	mplus_admin.call(this);
	
	var self = this;
	
	var $txtUserId = $("#txtUserId"),
		$btnSearch = $("#btnSearch"),
		$txtStrDate = $("#txtStrDate"),
		$txtEndDate = $("#txtEndDate"),
		$sltOption = $("#sltOption"),
		$crudServiceBaseUrl = contextPath + "/admin/msgcheck";
	
	var pageSize = 16;
	var msgList = [];
	var msgInfo = {};
	var totalCount;
	
	this.init = function(){
		initDatePicker();
		addEvent();
		initMsgCheckList(1);
		setPageList(1);
		if($('#page_1')!=null) $('#page_1').addClass('active');
	};
	
	var addEvent = function(){
		$btnSearch.on("click", function(e){
			$('#table_msgCheckDetail').empty();
			initMsgCheckList(1);
		});
	};
	
	var initDatePicker = function(){
		//mplus_admin 상속
		self.datePicker( $txtEndDate, $txtStrDate, $sltOption );
	};
	
	var initMsgCheckList = function(page){
		var param = { 
				pageSize: pageSize, 
				page: page, 
				strDate: new Date($txtStrDate.val()).setHours(0,0,0,0),
				endDate: new Date($txtEndDate.val()).setHours(0,0,0,0),
				userId: $txtUserId.val()
		};
		
		var opt = {
				url: $crudServiceBaseUrl + "/getMsgList.json",
		    	type : "post",
		    	async: false,
		        dataType: "json",
		        contentType: "application/json",
		        data: self.util.stringifyJson(param)
		};
		
		var success = function(result){
			msgList = result.data;
			totalCount = result.totalCount;
			$('#table_msgCheck').empty();
			$('#table_msgCheckDetail').empty();
    		for(var i=0; i<msgList.length; i++){
    			var tr = $('<tr></tr>').clone().attr('seq', i).attr('contentsSeq', msgList[i].contentsSeq);
    			var tdUserId = $('<td></td>').clone().text(msgList[i].userId);
    			var tdFormId = $('<td></td>').clone().text(msgList[i].formId);
    			var tdMessageValue = $('<td></td>').clone().text(msgList[i].messageValue.length>20? msgList[i].messageValue.substring(0, 21):msgList[i].messageValue);
    			var tdLogDt = $('<td></td>').clone().text(msgList[i].logDt);
    			
    			$('#table_msgCheck').append(tr.append(tdUserId).append(tdFormId).append(tdMessageValue).append(tdLogDt));    			
    		}
    		
    		$('#table_msgCheck tr').on('click', function(e) {
    			getMsgCheckInfo(e);
    			
    			$('#table_msgCheck tr').removeClass('active');
				$(this).addClass('active');
    		});			
		};
		
		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' ) {
    		initMsgCheckList(page);
    		$('#ulPagination li[id*="page_"]').removeClass('active');
    	} else if ( type == 'next') {
    		initMsgCheckList(page);
    		setPageList(page);
    	} else if ( type == 'previous' ) {
    		initMsgCheckList(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);});
    	
    };
    
    var getMsgCheckInfo = function(e){
    	msgInfo = msgList[e.currentTarget.attributes.seq.value];
    	var opt = {
       		 url: $crudServiceBaseUrl + "/getMsgDetailInfoList.json",
                method: "post",
                dataType: "json", 
                contentType: "application/json",
                data:self.util.stringifyJson({contentsSeq:msgInfo.contentsSeq,receiverId:msgInfo.userId})
    	};
    	var success = function(result){
    		msgDetailList = result;
			$('#table_msgCheckDetail').empty();
    		for(var i=0; i<msgDetailList.length; i++){
    			var tr = $('<tr></tr>').clone().attr('seq', i).attr('contentsSeq', msgDetailList[i].contentsSeq);
    			var tdPlatformType = $('<td></td>').clone().text(msgDetailList[i].platformType == 'A'? 'Android': 'iOS');
    			var tdSuccessYn = $('<td></td>').clone().text(msgDetailList[i].successYn == 'Y'? '성공': '실패');
    			var tdErrorType = $('<td></td>').clone().text(msgDetailList[i].errorType==null? '': msgDetailList[i].errorType);
    			
    			$('#table_msgCheckDetail').append(tr.append(tdPlatformType).append(tdSuccessYn).append(tdErrorType));    			
    		};
    	};
    	var error = function(xhr,d,t){
    		options.error(xhr);
           alert("상세 정보 조회 실패");
    	};
    	self.ajaxAdmin( opt, success, error );
    }
};
