//回评模块
var mark_history = function(option, success) {
	var object = new MarkHistory(option);
	success();
	return object;
}

function MarkHistory(option) {
	this.markControl = option.markControl;
	// this.url = option.url;
	this.pageSize = option.pageSize;
	this.taskList = [];
	this.container = this.markControl.container.sidebar;
	this.loading = false;

	this.markControl.initMarkFunction();
	this.container.toggleButton = getDom(this.toggle_button_dom, this.markControl).appendTo(this.markControl.container.assistant.functionList);
	this.container.toggleButton.click(this, function(event) {
		event.data.toggle(true);
	});

	this.markControl.on('history.get.success', this, function(event, context, data) {
		var taskList = data.result;
        this.taskList = [];
		this.loading = false;
		this.container.list.empty();
		if(isArray(taskList) && taskList.length > 0) {
			this.taskList = taskList;
			for( var i in taskList) {
				var task = taskList[i];
				var row = getDom(this.history_row_dom, this.markControl).appendTo(this.container.list);
				row.find('.history-secret-number').html(task.studentId);

				var date = new Date();
				date.setTime(task.markTime);
				row.find('.history-time').html(date.format('hh:mm:ss'));
				row.find('.history-score').html(task.totalScore);
				row.attr('data-index', i);

				// 回评任务处理
				task.previous = true;
				// task.markFinish = true;
				// task.totalScore = parseFloat(task.totalScore);
				// var scoreList = task.scoreList != undefined ?
				// task.scoreList.split(',') : [];
				// for (var j in task.markStepList) {
				// var step = task.markStepList[j];
				// if (scoreList != undefined && scoreList.length > j) {
				// step.markScore = new Number(scoreList[j]);
				// step.score = new String(step.markScore);
				// step.markFinish = true;
				// }
				// }
			}
		}
		this.container.list.find('td').click(this, function(event) {
			var markHistory = event.data;
			var index = $(event.target).parent().attr('data-index');
			markHistory.onTaskSelect(index);
		});
		this.pageNumber = data.pageNumber;
		this.updateHeader();

		if(this.taskList.length > 0) {
			this.onTaskSelect(0);
		}
	});
	this.markControl.on('history.get.error', this, function(event, context, data) {
		alert('暂时无法读取评卷历史，请稍后重试');
	});
	this.markControl.on('task.submit.success', this, function(event, context, data) {
		if(this.enable) {
			this.toggle(false);
		}
	});
	this.markControl.on('task.get.success', this, function(event, context, data) {
		this.loading = false;
	});
}

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

	this.container.header = getDom(this.header_dom, this.markControl).appendTo(this.container);
	this.container.paginator = getDom(this.paginator_dom, this.markControl).appendTo(this.container);
	this.container.list = getDom(this.history_list_dom, this.markControl).appendTo(this.container).find('#history-list');

	this.container.header.find('#close-history-button').click(this, function(event) {
		event.data.toggle(false);
		this.markControl.context.task = undefined;
        this.markControl.getTask();
	})
	this.container.paginator.find('#last-page-button').click(this, function(event) {
		var markHistory = event.data;
		markHistory.onSearch(markHistory.pageNumber + 1);
	});
	this.container.paginator.find('#next-page-button').click(this, function(event) {
		var markHistory = event.data;
		if(markHistory.pageNumber > 1) {
			markHistory.onSearch(markHistory.pageNumber - 1);
		}
	});
}

MarkHistory.prototype.toggle = function(enable) {
	this.enable = enable;
	if(enable) {
		this.init();
		this.pageNumber = 1;
		this.markControl.setTask(undefined);
		this.markControl.trigger('mark.sidebar.open');
		this.markControl.trigger('mark.history.open');
		this.updateHeader();
		this.container.list.empty();
		this.container.removeClass('hide');
		this.onSearch();
	} else {
		this.container.addClass('hide');
		this.markControl.trigger('mark.sidebar.close');
		this.markControl.trigger('mark.history.close');
	}
}

MarkHistory.prototype.updateHeader = function() {
	this.container.header.find('#history-start').html(this.pageSize * (this.pageNumber - 1));
	this.container.header.find('#history-end').html(this.pageSize * this.pageNumber);

	if(this.pageNumber == 1) {
		this.container.paginator.find('#next-page-label').show();
		this.container.paginator.find('#next-page-button').hide();
	} else {
		this.container.paginator.find('#next-page-label').hide();
		this.container.paginator.find('#next-page-button').show();
	}

	if(this.taskList.length < this.pageSize) {
		this.container.paginator.find('#last-page-label').show();
		this.container.paginator.find('#last-page-button').hide();
	} else {
		this.container.paginator.find('#last-page-label').hide();
		this.container.paginator.find('#last-page-button').show();
	}
}

MarkHistory.prototype.onSearch = function(pageNumber) {
	if(pageNumber == undefined || pageNumber < 1) {
		pageNumber = 1;
	}
	this.markControl.getHistory({
	    pageNumber: pageNumber,
	    pageSize: this.pageSize
	});
}

MarkHistory.prototype.onTaskSelect = function(index) {
    //初始化--特殊标识
    this.markControl.trigger('mark.specialTag.success');
	var number = new String(index);
	if(this.taskList != undefined && index < this.taskList.length && this.loading != true) {
		this.container.list.find('tr').each(function(index, obj) {
			if($(obj).attr('data-index') == number) {
				$(obj).addClass('active');
			} else {
				$(obj).removeClass('active');
			}
		});

		this.markControl.setTask(jQuery.extend(true, {}, this.taskList[index]));
		this.loading = true;
	}
}

MarkHistory.prototype.toggle_button_dom = '<a href="#">回评</a>';

MarkHistory.prototype.header_dom = '<div class="header">\
<p class="fl">前<i class="yellow" id="history-start"></i>-前<i class="yellow" id="history-end"></i></p>\
<a href="#" id="close-history-button"><img src="{staticServer}/mark-new/images/hp-close.png"></a>\
</div>';

MarkHistory.prototype.paginator_dom = '<div class="m-pagination"><a id="last-page-button" href="#">上一页</a>\
<i id="last-page-label">上一页</i>\
<a id="next-page-button" href="#">下一页</a>\
<i id="next-page-label">下一页</i></div>';

MarkHistory.prototype.history_list_dom = '<div class="sublist"><table class="table table-hover">\
<thead><tr><th>编号</th><th>时间</th><th>总分</th></tr></thead>\
<tbody id="history-list"></tbody></table></div>';

MarkHistory.prototype.history_row_dom = '<tr><td class="history-secret-number"></td>\
<td class="history-time"></td><td class="history-score"></td></tr>';