//质量分析模块
var quality_process = function(option, success) {
    var object = new QualityProcess(option);
    success();
    return object;
}

function QualityProcess(option) {
    this.markControl = option.markControl;
    this.container = this.markControl.container.sidebar;
    this.pageSize = 10;
    this.pageNumber = 1;
    this.markerId = 0;
    this.markerScore = 0;
    this.taskList = [];
    this.forbiddenInfo = option.forbiddenInfo;
    this.closeAssistant = option.closeAssistant;
    this.init();

    if(this.closeAssistant){
        var assistantButton = this.markControl.container.header.find('#assistant-button');
        assistantButton.hide();
    }
    var switchGroup = this.markControl.container.header.find('#switch-group-button');
    switchGroup.hide();
    
    this.markControl.on('task.get.none', this, function(event, context, data) {
        this.container.list.empty();
        this.markControl.trigger('mark.sidebar.close');
        this.container.hide();
    });
    this.markControl.on('task.get.success', this, function(event, context, data) {
    	var self  = this;
    	if (context.task.previous == true) {
    		self.markerScore = context.task.totalScore;
    		self.markerId =   context.task.markerId;
    		if(self.taskList == undefined || self.taskList.length == 0){
    			self.onSearch(self.markerId,self.markerScore,self.pageNumber);
    		}
    	}
    });
}

QualityProcess.prototype.init = function() {
    this.container.empty();

    this.container.header = getDom(this.header_dom, this.markControl).appendTo(this.container);
    this.container.list = getDom(this.history_list_dom, this.markControl).appendTo(this.container).find('#history-list');
    this.container.paginator = getDom(this.paginator_dom, this.markControl).appendTo(this.container);
    
    this.container.paginator.find('#last-page-button').click(this, function(event) {
        var self = event.data;
        if (self.taskList.length < self.pageSize) {
        	return ;
        }
        self.pageNumber = self.pageNumber + 1;
        self.onSearch(self.markerId,self.markerScore,self.pageNumber);
    });
    this.container.paginator.find('#next-page-button').click(this, function(event) {
    	var self = event.data;
        if (self.pageNumber == 1) {
        	return ;
        }
        if (self.pageNumber > 1) {
        	self.pageNumber = self.pageNumber - 1;
        	self.onSearch(self.markerId,self.markerScore,self.pageNumber);
        }
    });
}

QualityProcess.prototype.render = function(taskList,pageNumber) {
	this.taskList = taskList;
    var historyList = taskList;
    this.container.list.empty();
    if (isArray(historyList) && historyList.length > 0) {
		for (var i in historyList) {
			var history = historyList[i];          
			var row = getDom(this.history_row_dom, this.markControl).appendTo(this.container.list);
            row.find('.history-id').html(history.studentNumber);
            if(this.forbiddenInfo){
            	row.find('.history-name').html("***");
            }else{
            	row.find('.history-name').html(history.studentName);
            }
            row.find('.history-score').html(history.totalScore);
            row.attr('data-index', i);
    	}
    }
    
    this.container.list.find('td').click(this, function(event) {
        var qualityProcess = event.data;
        var index = $(event.target).parent().attr('data-index');
        qualityProcess.onTaskSelect(index);
    });
    this.pageNumber = pageNumber;
    this.updatePage();

    if (this.taskList.length > 0) {
        this.onTaskSelect(0);
    }
    
	this.markControl.trigger('mark.sidebar.open');
	this.container.show();
	
}

QualityProcess.prototype.onSearch = function(markerId,markerScore,pageNumber) {
	var self =  this; 
    if (pageNumber == undefined || pageNumber < 1) {
        pageNumber = 1;
    }
    $.post('/admin/exam/quality/history?markerId='+markerId+'&markerScore='+markerScore+'&pageNumber='+pageNumber+'&pageSize='+self.pageSize,
    		{}, function(data){
    	self.render(data,self.pageNumber);
    });
}

QualityProcess.prototype.onTaskSelect = function(index) {
    var number = new String(index);
    if (this.taskList != undefined && index < this.taskList.length) {
        this.container.list.find('tr').each(function(index, obj) {
            if ($(obj).attr('data-index') == number) {
                $(obj).addClass('active');
            } else {
                $(obj).removeClass('active');
            }
        });

        var task = this.taskList[index];
        this.markControl.setTask(task);
    }
}
QualityProcess.prototype.updatePage = function() {
    this.container.paginator.find('#history-start').html(this.pageSize * (this.pageNumber - 1));
    this.container.paginator.find('#history-end').html(this.pageSize * this.pageNumber);
}

QualityProcess.prototype.header_dom = '<div class="header">\
<p class="fl">给分记录</p></div>';

QualityProcess.prototype.history_list_dom = '<div class="sublist"><table class="table table-hover">\
<thead><tr><th>试卷ID</th><th>姓名</th><th>分值</th></tr></thead>\
<tbody id="history-list"></tbody></table></div>';

QualityProcess.prototype.history_row_dom = '<tr><td class="history-id"></td>\
	<td class="history-name"></td><td class="history-score"></tr>';

QualityProcess.prototype.paginator_dom = '<div class="c-page">\
<a class="back" id="last-page-button" href="#"></a>\
<a class="next" id="next-page-button" href="#"></a>\
<span>前<i class="yellow" id="history-start"></i>-前<i class="yellow" id="history-end"></i></span>\
</div>';
