//评卷状态模块
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.topStatus.find('#stage-name').html('正在加载');
    });
    this.markControl.on('task.get.success', this, function(event, context, eventObject) {
        var task = context.task;
        //修改页面显示
        this.topStatus.find('#stage-name').html('');
        this.topStatus.find('#student-number').html(task.studentId);
        this.studentTitle.show();

        if (task.objectiveScore != undefined) {
            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.topStatus.find('#stage-name').html('');
        this.topStatus.find('#student-number').html('');
        this.studentTitle.hide();
        this.objectiveArea.hide();
    });
    this.markControl.on('task.get.finish', this, function(event, context, status) {
        this.topStatus.find('#stage-name').html('');
        this.topStatus.find('#student-number').html('');
        this.studentTitle.hide();
        this.objectiveArea.hide();
    });
    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.blockProgress.hide();
    });
    this.markControl.on('view.sidebar.close', this, function(event, context, eventObject) {
        this.blockProgress.show();
    });
}

MarkStatus.prototype.init = function(option) {
    this.topStatus = getDom(this.status_dom, this.markControl).prependTo(this.markControl.container.header);
    this.blockProgress = getDom(this.progress_dom, this.markControl).insertAfter(this.topStatus);
    this.subjectTitle = this.topStatus.find('#subject-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.markedCount >= this.status.topCount && this.ignoreTopCount != true) {
        this.popover.show();
    } else {
        this.popover.hide();
    }
}

MarkStatus.prototype.render = function(status) {
    if (status != undefined && status.valid === true) {
        var topCount = status.topCount;
        this.topStatus.find('#mark-count').html(status.personCount);

        //大题进度信息区域
        if (this.blockProgress != undefined) {
            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';
            }
            if(topCount > 0){
                if(this.blockProgress.find('#top-count').length == 0){
                  this.blockProgress.find('#todo-count').after('<i>任务数<em id="top-count"></em></i>');
                  this.blockProgress.find('#top-count').html(topCount);
                }
            }
            this.blockProgress.find('#total-count').html(totalCount);
            this.blockProgress.find('#marked-count').html(markedCount);
            this.blockProgress.find('#todo-count').html(leftCount);
            this.blockProgress.find('#exception-count').html(exceptionCount);
            this.blockProgress.find('#marked-percent').html(markedPercent + '%');
        }

        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="stage-name"></i>\
<i id="student-title">考生编号<em id="student-number"></em></i>\
<i id="objective-area">客观得分<em id="objective-score"></em></i>\
<i>评卷数<em id="mark-count"></em></i>\
</p>';

MarkStatus.prototype.progress_dom = '<p class="text">\
<i>未评<em id="todo-count"></em></i>\
<i>进度<em id="marked-percent"></em></i>\
</p>';

MarkStatus.prototype.block_progress_bak_dom = '<p class="text">\
<i>总数<em id="total-count"></em></i>\
<i>已评<em id="marked-count"></em></i>\
<i>未评<em id="todo-count"></em></i>\
<i>异常<em id="exception-count"></em></i>\
<i>进度<em id="marked-percent"></em></i>\
</p>';

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