//评卷状态模块
var mark_status = function(option, success) {
    var object = new MarkStatus(option);
    success();
    return object;
}

function MarkStatus(option) {
    this.markControl = option.markControl;
    this.init(option);
    this.markControl.on('task.get.before', this, function(event, context, statusInfo) {
        this.changeStatus(getMessage('mark.status.loading'));
        this.clearTopTitle();
    });
    this.markControl.on('task.get.success', this, function(event, context, eventObject) {
        var task = context.task;
        this.changeStatus(task.statusName);
        //修改页面显示
        if(task.studentNumber && task.studentNumber!=''){
        	this.topStatus.find('#student-number').html(task.studentNumber);
        	this.studentTitle.show();
        }else{
        	this.studentTitle.hide();
        }
        if(task.taskNumber && task.taskNumber!=''){
        	this.topStatus.find('#library-number').html(task.taskNumber);
        	this.libraryTitle.show();
        }else{
        	this.libraryTitle.hide();
        }
        if (task.objectiveScore) {
            this.objectiveArea.find('#objective-score').html(task.objectiveScore);
            this.objectiveArea.show();
        } else {
            this.objectiveArea.hide();
        }
        this.checkTopCount(task);
    });
    this.markControl.on('task.get.none', this, function(event, context, status) {
    	this.changeStatus();
        this.clearTopTitle();
    });
    this.markControl.on('task.get.finish', this, function(event, context, status) {
        this.changeStatus();
        this.clearTopTitle();
    });
    this.markControl.on('mark.status.change', this, function(event, context, status) {
        this.status = status;
        this.render(status);
    });
    this.markControl.on('view.sidebar.open', this, function(event, context, eventObject) {
        this.progress.hide();
    });
    this.markControl.on('view.sidebar.close', this, function(event, context, eventObject) {
        this.progress.show();
    });
}

MarkStatus.prototype.init = function(option) {
    this.topStatus = getDom(this.status_dom, this.markControl).prependTo(this.markControl.container.header);
    this.progress = getDom(this.progress_dom, this.markControl).insertAfter(this.topStatus);
    this.subjectTitle = this.topStatus.find('#subject-title');
    this.libraryTitle = this.topStatus.find('#library-title');
    this.studentTitle = this.topStatus.find('#student-title');
    this.objectiveArea = this.topStatus.find('#objective-area');

    this.popover = getDom(this.popover_dom, this.markControl);
    this.popover.hide();
    this.popover.appendTo(this.markControl.container);
    var self = this;
    this.popover.find('#continue-button').click(function() {
        self.ignoreTopCount = true;
        self.popover.hide();
    });

    this.subjectTitle.find('#subject-name').html(option.subjectName);
    if (option.subjectSelectUrl != undefined) {
        this.subjectTitle.click(function() {
            window.location.href = option.subjectSelectUrl;
        });
    }
}

MarkStatus.prototype.checkTopCount = function(task) {
    //任务限额提示
    if (task != undefined && task.previous != true && this.status != undefined && this.status.topCount > 0 && this.status.personCount >= this.status.topCount && this.ignoreTopCount != true) {
        this.popover.show();
    } else {
        this.popover.hide();
    }
}

MarkStatus.prototype.clearTopTitle = function() {
	this.topStatus.find('#student-number').html('');
	this.topStatus.find('#library-number').html('');
	this.topStatus.find('#objective-score').html('');
	this.studentTitle.hide();
	this.libraryTitle.hide();
	this.objectiveArea.hide();
}

MarkStatus.prototype.changeStatus = function(name) {
	if(name!=undefined && name!=''){
    	this.topStatus.find('#status-name').html(name);
	}else{
		this.topStatus.find('#status-name').html('');
	}
}

MarkStatus.prototype.changeTopCount = function(topCount) {
	if(topCount && topCount > 0){
		this.progress.find('#top-count').html(topCount);
		this.progress.find('#top-count-area').show();
	}else{
       	this.progress.find('#top-count').html('');
        this.progress.find('#top-count-area').hide();
    }
}

MarkStatus.prototype.render = function(status) {
    if (status != undefined && status.valid === true) {
    	//个人评卷数量
        this.topStatus.find('#person-count').html(status.personCount);
        //大题进度信息区域
        var totalCount = status.totalCount;
		var markedCount = status.markedCount;
        var exceptionCount = status.exceptionCount;            
        var leftCount = totalCount > (markedCount + exceptionCount) ? (totalCount - markedCount - exceptionCount) : 0;
        var markedPercent = totalCount > 0 ? new Number((totalCount - leftCount) * 100 / totalCount).toFixed(0) : '0';
        if (markedPercent == '100' && leftCount > 0) {
            markedPercent = '99';
        } else if (markedPercent == '0' && markedCount > 0) {
            markedPercent = '1';
        }
        this.progress.find('#total-count').html(totalCount);
        this.progress.find('#marked-count').html(markedCount);
        this.progress.find('#todo-count').html(leftCount);
        this.progress.find('#exception-count').html(exceptionCount);
        this.progress.find('#marked-percent').html(markedPercent + '%');
		//评卷任务上限
		this.changeTopCount(status.topCount);
        //判断评卷是否结束
        if (status.totalCount > 0 && status.totalCount == status.markedCount) {
            this.markControl.context.isFinish = true;
        }
    }
}

MarkStatus.prototype.status_dom = '<p class="text">\
<i id="subject-title" style="cursor:pointer"><em id="subject-name"></em></i>\
<i id="status-title"><em id="status-name" data-i18n-text="mark.status.loading"></em></i>\
<i id="library-title" style="display:none"><span data-i18n-text="mark.status.library.number">任务编号</span><em id="library-number"></em></i>\
<i id="student-title" style="display:none"><span data-i18n-text="mark.status.student.number">考生编号</span><em id="student-number"></em></i>\
<i id="objective-area" style="display:none"><span data-i18n-text="mark.status.objective.score">客观得分</span><em id="objective-score"></em></i>\
<i><span data-i18n-text="mark.status.marked.count">已评</span><em id="person-count"></em></i>\
</p>';

MarkStatus.prototype.progress_dom = '<p class="text">\
<i id="top-count-area" style="display:none"><span data-i18n-text="mark.status.top.count">分配</span><em id="top-count"></em></i>\
<i><span data-i18n-text="mark.status.unmark.count">未评</span><em id="todo-count"></em></i>\
<i><span data-i18n-text="mark.status.progress">进度</span><em id="marked-percent"></em></i>\
</p>';

MarkStatus.prototype.popover_dom = '<div class="warning-popover">\
<p data-i18n-text="mark.status.top.count.finish">分配任务已评完，是否继续？</p>\
<a href="#" class="btn btn-large btn-primary text-c" id="continue-button" data-i18n-text="mark.status.continue">继续</a>\
<a href="{logoutUrl}" class="btn btn-large text-c" id="exit-button" data-i18n-text="mark.status.logout">退出</a>\
</div>';