//问题卷处理模块
var problem_process = function(option, success) {
    var object = new ProblemProcess(option);
    success();
    return object;
}

function ProblemProcess(option) {
    this.markControl = option.markControl;
    this.autoSubmit = false;
    this.reasons = option.reasons;
    this.init();
    this.markControl.on('task.get.before', this, function(event, context, eventObject) {
        this.task = undefined;
        this.reasonType = undefined;
    });
    this.markControl.on('task.get.success', this, function(event, context, eventObject) {
        this.task = context.task;
        if (this.task.previous != true && this.task.back != true) {
            this.container.show();
        } else {
            this.container.hide();
        }
    });
    this.markControl.on('task.get.none', this, function(event, context, eventObject) {
        this.container.hide();
    });
    this.markControl.on('task.get.error', this, function(event, context, eventObject) {
        this.container.hide();
    });
}

ProblemProcess.prototype.init = function() {
    this.container = getDom(this.container_dom, this.markControl).appendTo(this.markControl.container.assistant);
    this.container.list = this.container.find('#problem-list');
    this.container.title = this.container.find('.popover-title');
    for (var i in this.reasons) {
        var button = getDom(this.button_dom, this.markControl).appendTo(this.container.list);
        button.attr('data-reason', i);
        button.html(this.reasons[i]);
    }
    this.container.list.find('.problem-select-button').click(this, function(event) {
        var problemProcess = event.data;
        var button = $(event.target);

        problemProcess.container.list.find('.problem-select-button').each(function(index, obj) {
            $(obj).removeClass('curr');
        });
        problemProcess.onReasonSelect(button);
    });

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

ProblemProcess.prototype.reset = function() {
    this.popover.hide();
    this.container.list.find('.problem-select-button').each(function(index, obj) {
        $(obj).removeClass('curr');
    });
}

ProblemProcess.prototype.onReasonSelect = function(button) {
    var self = this;
    if (this.task != undefined && this.task.problem != true) {
        button.addClass('curr');
        this.reasonType = button.attr('data-reason');
        if (this.autoSubmit) {
            this.submit();
        } else {
            this.popover.show();
        }
    }
    return false;
}

ProblemProcess.prototype.submit = function() {
    if (this.task != undefined && this.reasonType != undefined) {
        this.task.problem = true;
        this.task.reason = this.reasonType;
        this.markControl.submitTask();
    }
}


ProblemProcess.prototype.container_dom = '<h3 class="popover-title">问题卷</h3>\
<div class="popover-content"><p id="problem-list" class="popover-list">\
</p></div>';

ProblemProcess.prototype.button_dom = '<a class="problem-select-button" href="#"></a>';

ProblemProcess.prototype.popover_dom = '<div class="warning-popover">\
<p>确认要将本试卷置为问题卷吗？</p>\
<a href="#" class="btn btn-large btn-primary text-c" id="submit-button">确定</a>\
<a href="#" class="btn btn-large text-c" id="cancel-button">取消</a>\
</div>';