//评卷状态模块

function MarkStatus(option) {
    this.markControl = option.markControl;
    this.init();
    this.markControl.on('task.get.before', this, function(event, context, statusInfo) {
        this.scoreArray = [];
        this.task = undefined;
        this.currentStep = undefined;
        this.stepCount = 0;
        this.centerHeader.find('#type-name').html('正在领取任务');
    });
    this.markControl.on('task.get.none', this, function(event, context, statusInfo) {
        //修改页面显示
        this.render(statusInfo);
    });
    this.markControl.on('task.get.success', this, function(event, context, eventObject) {
        var task = context.task;
        this.task = context.task;
        this.stepCount = task.markStepList.length;
        this.scoreArray = [];
        //右上区域
        //this.markControl.container.sidebar.header.find('#mark-user-name').html(task.marker);
        //状态栏区域
        task.typeName = task.libraryType < this.typeName.length ? this.typeName[task.libraryType] : '';
        if (!task.previous) {
            task.totalScore = undefined;
        }
        //修改页面显示
        this.render(task);
    });
    this.markControl.on('mark.step.change', this, this.onStepChange);
    this.markControl.on('mark.score.change', this, this.onScoreChange);
}

MarkStatus.prototype.init = function() {
    this.centerHeader = getDom(this.center_header_dom, this.markControl).appendTo(this.markControl.container.center.header.find('p.text')).parent();
    this.secretTitle = this.centerHeader.find('#secret-title');
    this.scoreHolder = this.markControl.container.center.header.find('p.fraction');

    this.bottomStatus = getDom(this.bottom_status_dom, this.markControl).appendTo(this.markControl.container);
    this.bottomStatus.mouseover(this, function(event) {
        var bottomStatus = event.data.bottomStatus;
        bottomStatus.find('#bottom-status-title').hide();
        bottomStatus.find('#bottom-status-detail').show();
    });
    this.bottomStatus.mouseout(this, function(event) {
        var bottomStatus = event.data.bottomStatus;
        bottomStatus.find('#bottom-status-title').show();
        bottomStatus.find('#bottom-status-detail').hide();
    });
}

MarkStatus.prototype.render = function(task) {
    if (task.exist === true) {
        this.centerHeader.find('#type-name').html(task.typeName);
        this.centerHeader.find('#secret-number').html(task.secretNo);
        this.centerHeader.find('#mark-count').html(task.markCount);
        this.scoreHolder.empty();
        this.secretTitle.show();

        for (var i in task.markStepList) {
            var step = task.markStepList[i];
            var stepDom = getDom(this.center_score_dom, this.markControl);
            var plusDom = getDom(this.center_score_dom, this.markControl);
            if (step.markFinish === true) {
                stepDom.html(step.score);
                stepDom.show();
                plusDom.show();
            } else {
                stepDom.hide();
                plusDom.hide();
            }
            plusDom.html('+');
            if (i > 0) {
                plusDom.appendTo(this.scoreHolder);
            }
            this.scoreArray.push(stepDom.appendTo(this.scoreHolder));
        }

        this.equalDom = getDom(this.center_score_dom, this.markControl);
        this.totalDom = getDom(this.center_score_dom, this.markControl);
        if (task.markFinish === true && task.markStepList.length > 1) {
            this.equalDom.show();
            this.totalDom.show();
            this.totalDom.html(task.totalScore);
        } else {
            this.equalDom.hide();
            this.totalDom.hide();
        }
        this.equalDom.html('=');
        this.equalDom.appendTo(this.scoreHolder);
        this.totalDom.appendTo(this.scoreHolder);
    } else {
        //清空正上方显示区域
        this.centerHeader.find('#type-name').html('无评卷任务');
        this.centerHeader.find('#secret-number').html('');
        this.centerHeader.find('#mark-count').html(task.markCount);
        this.scoreHolder.empty();
        this.secretTitle.hide();
    }
    //底部详细信息区域
    var totalCount = task.blockToMarkCount;
    var markedCount = task.blockMarkedCount;
    var leftCount = totalCount > markedCount ? (totalCount - markedCount) : 0;
    var markedPercent = totalCount > 0 ? new Number((totalCount - leftCount) * 100 / totalCount).toFixed(1) : 0;
    this.bottomStatus.find('#total-count').html(totalCount);
    this.bottomStatus.find('#marked-count').html(markedCount);
    this.bottomStatus.find('#todo-count').html(leftCount);
    this.bottomStatus.find('#marked-percent').html(markedPercent.toString() + '%');
}

MarkStatus.prototype.onScoreChange = function(event, context, eventObject) {
    if (this.currentStep != undefined) {
        //修改页面显示
        var stepDom = this.scoreArray[this.currentStep.number - 1];
        if (this.currentStep.markScore != undefined || this.currentStep.score != undefined) {
            stepDom.addClass('active');
            stepDom.show();
            if (this.currentStep.number > 1) {
                stepDom.prev().show();
            }
            stepDom.html(this.currentStep.score);
        } else {
            stepDom.hide();
            if (this.currentStep.number > 1) {
                stepDom.prev().hide();
            }
        }

        if (this.task.totalScore != undefined && this.task.markStepList.length > 1) {
            this.equalDom.show();
            this.totalDom.html(this.task.totalScore);
            this.totalDom.show();
        }
    }
}

MarkStatus.prototype.onStepChange = function(event, context, stepNumber) {
    if (stepNumber > 0 && stepNumber <= this.stepCount) {
        if (this.currentStep != undefined) {
            var stepDom = this.scoreArray[this.currentStep.number - 1];
            stepDom.removeClass('active');
        }
        this.currentStep = this.task.markStepList[stepNumber - 1];
        //修改页面显示
        this.onScoreChange();
    }
}


MarkStatus.prototype.typeName = ['样卷', '试评', '正评'];

//给分板模块

function MarkBoard(option) {
    this.markControl = option.markControl;
    this.visibleCount = new Number(option.visibleCount);
    this.crossCount = Math.round(this.visibleCount / 2);
    this.autoSubmit = option.autoSubmit === false ? false : true;
    this.showScoreBoard = option.showScoreBoard === false ? false : true;

    this.init();
    this.markControl.on('task.get.before', this, function(event, context, eventObject) {
        this.task = undefined;
        this.stepList = undefined;
        this.currentStep = undefined;
        this.stepCount = undefined;
        this.scrollStart = 0;
        this.render();
    });
    this.markControl.on('task.get.success', this, function(event, context, eventObject) {
        this.task = context.task;
        this.stepList = context.task.markStepList;
        this.currentStep = undefined;
        this.stepCount = this.stepList.length;
        this.scrollStart = 1;
        this.render(context.task);
        if (this.stepCount > 0) {
            this.markControl.trigger('mark.step.change', 1);
        }
    });
    this.markControl.on('mark.step.change', this, this.onStepSelect);
    this.markControl.on('key.press', this, function(e, context, event) {
        var code = event.keyCode;
        //console.log('key press:' + code);
        if (this.currentStep != undefined && this.task != undefined) {
            if (code >= 48 && code <= 57) {
                this.onNumberInput(code - 48);
                event.preventDefault();
                return false;
            } else if (code >= 96 && code <= 105 && event.numLock) {
                this.onNumberInput(code - 96);
                event.preventDefault();
                return false;
            } else if (code == 46 || code == 110 || code == 190) {
                //小数点
                this.onDecimalInput();
                event.preventDefault();
                return false;
            } else if (code == 45) {
                //-按键，切换到上一个步骤
                if (this.currentStep.number > 1) {
                    this.resetCurrentStep();
                    this.markControl.trigger('mark.step.change', this.currentStep.number - 1);
                    event.preventDefault();
                    return false;
                }
            } else if (code == 61 || code == 43) {
                //+按键，切换到下一个步骤
                if (this.currentStep.number < this.stepCount && this.currentStep.markScore != undefined) {
                    this.resetCurrentStep();
                    this.markControl.trigger('mark.step.change', this.currentStep.number + 1);
                    event.preventDefault();
                    return false;
                }
            } else if (code == 13 || code == 108) {
                //回车键
                if (this.currentStep.markFinish === true && this.task.markFinish === true) {
                    this.onTaskSubmit();
                    event.preventDefault();
                    return false;
                } else {
                    this.onNumberSubmit();
                    event.preventDefault();
                    return false;
                }
            }
        }
    });
    this.markControl.on('key.down', this, function(e, context, event) {
        var code = event.keyCode;
        //console.log('key down:' + code);
        if (this.currentStep != undefined && this.task != undefined) {
            if (code == 8 || code == 46) {
                //删除键&回退键
                this.onNumberDelete();
                event.preventDefault();
                return false;
            } // else if (code == 32) {
            //空格键
            // this.onNumberSubmit();
            //  event.preventDefault();
            //   return false;
            //}
        }
    });

}

MarkBoard.prototype.init = function() {
    this.container = getDom(this.container_dom, this.markControl).appendTo(this.markControl.container.sidebar);
    this.container.header = getDom(this.header_dom, this.markControl).appendTo(this.container);
    this.container.stepHolder = getDom(this.stepBoard_dom, this.markControl).appendTo(this.container).find('#step-holder');
    this.scoreBoard = getDom(this.scoreBoard_dom, this.markControl).appendTo(this.markControl.container);
    this.scoreBoard.header = getDom(this.scoreBoard_header_dom, this.markControl).appendTo(this.scoreBoard).find('#score-board-header');
    this.scoreBoard.stepHolder = getDom(this.scoreBoard_stepHolder_dom, this.markControl).appendTo(this.scoreBoard);
    this.scoreBoard.footer = getDom(this.scoreBoard_footer_dom, this.markControl).appendTo(this.scoreBoard).find('#score-board-total-score');
    this.scoreBoard.draggable();

    this.scoreBoardToggle = getDom(this.scoreBoard_toggle_dom, this.markControl).appendTo(this.markControl.container.sidebar.tips);
    this.scoreBoardToggleButton = this.scoreBoardToggle.find('#score-board-toggle-button');
    this.toggleScoreBoard(this.showScoreBoard);

    this.container.find('#scroll-up-button').click(this, function(event) {
        var markBoard = event.data;
        if (markBoard.task != undefined) {
            markBoard.onStepScrollUp();
        }
    });
    this.container.find('#scroll-down-button').click(this, function(event) {
        var markBoard = event.data;
        if (markBoard.task != undefined) {
            markBoard.onStepScrollDown();
        }
    });
    this.scoreBoard.find('#task-submit-button').click(this, function(event) {
        var markBoard = event.data;
        if (markBoard.task != undefined && markBoard.currentStep.markFinish === true) {
            markBoard.onTaskSubmit();
        }
    });
    this.scoreBoard.find('#score-board-close-button').click(this, function(event) {
        var markBoard = event.data;
        markBoard.toggleScoreBoard(false);
    });
    this.scoreBoardToggleButton.click(this, function(event) {
        event.data.toggleScoreBoard(!event.data.showScoreBoard);
    });
}

MarkBoard.prototype.onDecimalInput = function() {
    var score = this.currentStep.score;
    if (this.currentStep.interval < 1 && !score.endWith('.')) {
        score = score + '.';
    }
    this.currentStep.score = score;
    this.currentStep.markFinish = false;
    this.markControl.trigger('mark.score.change');
}

MarkBoard.prototype.onNumberInput = function(number) {
    this.currentStep.score = this.currentStep.score + '' + number;
    this.currentStep.markFinish = false;
    this.markControl.trigger('mark.score.change');
}

MarkBoard.prototype.onNumberDelete = function(number) {
    var score = this.currentStep.score;
    if (score.length > 1) {
        score = score.substring(0, score.length - 1);
    } else {
        score = '';
    }
    this.currentStep.score = score;
    this.currentStep.markFinish = false;
    this.markControl.trigger('mark.score.change');
}

MarkBoard.prototype.onNumberSubmit = function() {
    var score = this.currentStep.score;
    var interval = this.currentStep.interval;
    if (score.length > 0) {
        if (interval < 1 && score.startWith('.')) {
            score = '0'.concat(score);
        } else if (interval < 1 && score.endWith('.')) {
            score = score.concat(interval.toString().substring(2));
        }
    }
    if (this.validateScore(this.currentStep, score)) {
        this.onScoreChange(true);
    }
}

//切换步骤时，重置当前步骤的状态
//若当前分数未提交，则还原之前已给的分数；若之前还未给分，则清空分数显示
MarkBoard.prototype.resetCurrentStep = function() {
    var step = this.currentStep;
    if (step != undefined) {
        if (step.markScore != undefined) {
            step.markFinish = true;
            step.score = new String(step.markScore);
        } else {
            step.score = undefined;
        }
        this.markControl.trigger('mark.score.change');
    }
}

MarkBoard.prototype.onStepSelect = function(event, context, stepNumber) {
    if (stepNumber <= this.stepCount && stepNumber > 0) {
        //修改原step状态
        if (this.currentStep != undefined) {
            var dom = this.stepArray[this.currentStep.number - 1];
            dom.removeClass('current');
            if (this.currentStep.markScore != undefined) {
                dom.addClass('done');
            } else {
                dom.addClass('todo');
            }
        }

        this.currentStep = this.stepList[stepNumber - 1];
        if (this.currentStep.score == undefined) {
            this.currentStep.score = '';
        }
        dom = this.stepArray[stepNumber - 1];
        dom.removeClass('done');
        dom.removeClass('todo');
        dom.addClass('current');
        //step列表自动滚动
        var crossNumber = this.scrollStart + this.crossCount - 1;
        if (this.currentStep.number > crossNumber) {
            for (var i = 1; i <= (this.currentStep.number - crossNumber); i++) {
                this.onStepScrollDown();
            }
        } else if (this.currentStep.number < this.scrollStart) {
            for (var i = 1; i <= (this.scrollStart - this.currentStep.number); i++) {
                this.onStepScrollUp();
            }
        }
    }
}

MarkBoard.prototype.onScoreChange = function(autoNext) {
    if (this.currentStep != undefined) {
        this.currentStep.markScore = new Number(this.currentStep.score);
        this.currentStep.markFinish = true;
        this.updateTotalScore();

        this.scoreBoard.footer.html(this.task.totalScore);
        var currentScore = this.currentStep.score;
        this.scoreBoard.stepArray[this.currentStep.number - 1].find('.score-select').each(function(index, obj) {
            if ($(obj).attr('data-score') == currentScore) {
                $(obj).addClass('active');
            } else {
                $(obj).removeClass('active');
            }
        });
        this.markControl.trigger('mark.score.change');
        if (autoNext === true && this.currentStep.number < this.stepCount) {
            this.markControl.trigger('mark.step.change', this.currentStep.number + 1);
        } else if (this.task.markFinish === true && this.autoSubmit === true) {
            this.onTaskSubmit();
        }
    }
}

MarkBoard.prototype.updateTotalScore = function() {
    if (this.task != undefined) {
        var totalScore = 0;
        var finish = true;
        for (var i in this.stepList) {
            if (this.stepList[i].markScore != undefined) {
                totalScore = totalScore + this.stepList[i].markScore;
            } else {
                finish = false;
            }
        }
        this.task.totalScore = totalScore;
        this.task.markFinish = finish;
    }
}

MarkBoard.prototype.onTaskSubmit = function() {
    var totalScore = 0;
    var scoreList = [];
    var finish = true;
    for (var i in this.stepList) {
        var score = this.stepList[i].markScore;
        if (score != undefined) {
            totalScore = totalScore + score;
            scoreList.push(score);
        } else {
            finish = false;
        }
    }
    if (!finish) {
        alert('当前任务还有未给分的步骤，请继续给分');
    } else if (confirm('总分为' + totalScore + ', 确认要提交吗？')) {
        this.task.totalScore = totalScore;
        this.task.scoreList = scoreList.join(',');
        this.markControl.submitTask();
    }
}

MarkBoard.prototype.validateScore = function(step, scoreString) {
    if (!$.isNumeric(scoreString)) {
        alert('当前分数不是合法数字');
        return false;
    }
    var score = new Number(scoreString);
    if (score < step.min) {
        alert('当前分数不应小于' + step.min);
        return false;
    }
    if (score > step.max) {
        alert('当前分数不应大于' + step.max);
        return false;
    }
    var find = false;
    if (step.scoreList.length > 0) {
        for (var i in step.scoreList) {
            if (score == step.scoreList[i]) {
                find = true;
            }
        }
    }
    if (!find) {
        alert('当前分数不符合分值间隔要求');
        return false;
    }
    step.score = scoreString;
    return true;
}

MarkBoard.prototype.render = function(task) {
    if (task != undefined) {
        var title = task.blockTitle;
        this.container.header.html(title);
        this.container.stepHolder.empty();
        this.scoreBoard.header.html(title);
        this.scoreBoard.stepHolder.empty();
        this.stepArray = [];
        this.scoreBoard.stepArray = [];
        for (var i in task.markStepList) {
            var step = task.markStepList[i];
            //初始化步骤列表
            var dom = getDom(this.step_dom, this.markControl);
            dom.find('.number').html(step.title);
            dom.find('.interval').html('间隔' + step.interval + '分');
            dom.find('.content').html(step.min + '~' + step.max);
            if (step.markFinish === true) {
                dom.addClass('done');
            } else {
                dom.addClass('todo');
            }
            if (i >= this.visibleCount) {
                dom.addClass('hide');
            }
            this.stepArray.push(dom.appendTo(this.container.stepHolder));
            //初始化分值表
            var dom2 = getDom(this.scoreBoard_step_dom, this.markControl);
            dom2.attr('data-number', step.number);
            dom2.find('.number').html(step.title);
            var scoreHolder = dom2.find('.fraction');
            for (var j in step.scoreList) {
                var dom3 = getDom(this.scoreBoard_score_dom, this.markControl);
                var score = step.scoreList[j];
                dom3.attr('data-step-number', step.number);
                dom3.attr('data-score', score);
                dom3.html(score);
                dom3.appendTo(scoreHolder);

                if (step.markFinish === true && step.markScore == score) {
                    dom3.addClass('active');
                }
            }
            this.scoreBoard.stepArray.push(dom2.appendTo(this.scoreBoard.stepHolder));
        }
        this.toggleScoreBoard(this.showScoreBoard);
        this.container.show();
        this.scoreBoardToggle.show();

        this.scoreBoard.find('.score-select').click(this, function(event) {
            var markBoard = event.data;
            if (markBoard.task != undefined) {
                var object = $(event.target);
                markBoard.markControl.trigger('mark.step.change', object.attr('data-step-number'));
                markBoard.currentStep.score = object.attr('data-score');
                markBoard.onScoreChange();
            }
        });
    } else {
        this.container.hide();
        this.container.header.html('');
        this.container.stepHolder.empty();
        this.scoreBoard.header.html('');
        this.scoreBoard.stepHolder.empty();
        this.stepArray = [];
        this.scoreBoard.stepArray = [];
        this.scoreBoard.hide();
        this.scoreBoardToggle.hide();
    }
}

MarkBoard.prototype.onStepScrollDown = function() {
    if ((this.scrollStart + this.visibleCount - 1) < this.stepCount) {
        this.stepArray[this.scrollStart + this.visibleCount - 1].show();
        this.stepArray[this.scrollStart - 1].hide();
        this.scrollStart = this.scrollStart + 1;
    }
}

MarkBoard.prototype.onStepScrollUp = function() {
    if (this.scrollStart > 1) {
        this.stepArray[this.scrollStart + this.visibleCount - 2].hide();
        this.stepArray[this.scrollStart - 2].show();
        this.scrollStart = this.scrollStart - 1;
    }
}

MarkBoard.prototype.toggleScoreBoard = function(show) {
    if (show) {
        this.showScoreBoard = true;
        this.scoreBoard.show();
        this.scoreBoardToggleButton.removeClass('off');
        this.scoreBoardToggleButton.addClass('on');
    } else {
        this.showScoreBoard = false;
        this.scoreBoard.hide();
        this.scoreBoardToggleButton.removeClass('on');
        this.scoreBoardToggleButton.addClass('off');
    }
}

//查看全卷模块

function PaperView(option) {
    this.noPaperUrl = option.markControl.context.staticServer + '/images/no-allpaper.jpg';
    this.images = [];
    this.enable = false;

    option.markControl.on('task.get.success', this, function(event, context, eventObject) {
        var context = context;
        this.toggle(context.task.canViewTotal);
        if (this.enable) {
            var urls = context.task.pictureUrls;
            this.images = [];
            if (isArray(urls) && urls.length > 0) {
                for (var i in urls) {
                    this.images.push(context.imageServer + urls[i]);
                }
            } else {
                this.images.push(this.noPaperUrl);
            }
        }
    });
    option.markControl.on('task.submit.success', this, function(event, context, eventObject) {
        this.images = [];
    });
}

PaperView.prototype.toggle = function(canViewTotal) {
    this.enable = canViewTotal;
    if (this.enable) {
        //show
    } else {
        //hide
    }
}

PaperView.prototype.show = function() {
    if (this.images.length > 0) {
        //render image
    } else {

    }
}

//查看答案模块

function AnswerView(option) {
    this.markControl = option.markControl;
    this.noAnswerUrl = this.markControl.context.staticServer + '/images/no-answer.jpg';
    this.images = [];
    option.markControl.on('task.get.success', this, function(event, context, eventObject) {
        var urls = context.task.answerUrls;
        this.images = [];
        if (isArray(urls) && urls.length > 0) {
            for (var i in urls) {
                this.images.push(context.imageServer + urls[i]);
            }
        } else {
            this.images.push(this.noAnswerUrl);
        }
    });
    option.markControl.on('task.submit.success', this, function(event, context, eventObject) {
        this.images = [];
    });
}

AnswerView.prototype.show = function() {
    if (this.images.length > 0) {

    } else {

    }
}

//回评模块

function MarkHistory(option) {
    this.markControl = option.markControl;
    this.url = option.url;
    this.pageSize = option.pageSize;
    this.init();
    this.taskList = [];

    this.markControl.on('history.get.success', this, function(event, context, data) {
        var taskList = data.result;
        this.taskList = [];
        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.secretNo);

                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 = new Number(task.totalScore);
                var scoreList = 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);
        }
    });
}

MarkHistory.prototype.init = function() {
    this.container = this.markControl.container.history;
    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.markControl.initMarkFunction();
    this.container.toggleButton = getDom(this.toggle_button_dom, this.markControl).appendTo(this.markControl.container.sidebar.assistant.functionList);
    this.container.toggleButton.click(this, function(event) {
        event.data.toggle(true);
    });
    this.container.header.find('#close-history-button').click(this, function(event) {
        event.data.toggle(false);
    })
    this.container.paginator.find('#last-page-button').click(this, function(event) {
        var markHistory = event.data;
        if (markHistory.pageNumber > 1) {
            markHistory.onSearch(markHistory.pageNumber - 1);
        }
    });
    this.container.paginator.find('#next-page-button').click(this, function(event) {
        var markHistory = event.data;
        markHistory.onSearch(markHistory.pageNumber + 1);
    });
}

MarkHistory.prototype.toggle = function(enable) {
    this.enable = enable;
    if (enable) {
        this.pageNumber = 1;
        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.history.close');
        this.markControl.context.task = undefined;
        this.markControl.getTask();
    }
}

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) {
    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);
    }
}

//问题卷处理模块

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.markControl.on('task.get.success', this, function(event, context, eventObject) {
        this.task = context.task;
        this.container.show();
    });
    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() {
    var assistant = this.markControl.container.sidebar.assistant;
    this.container = getDom(this.container_dom, this.markControl).appendTo(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.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.html(), button);
    });
}

ProblemProcess.prototype.onReasonSelect = function(reason, button) {
    if (this.task != undefined) {
        button.addClass('curr');
        if (this.autoSubmit) {
            this.task.problem = true;
            this.task.reason = reason;
            this.markControl.submitTask();
        } else if (confirm('确认要将本任务置为问题卷吗？')) {
            this.task.problem = true;
            this.task.reason = reason;
            this.markControl.submitTask();
        }
        button.removeClass('curr');
    }
}

//简单多张图片排列显示模块

function SimpleImageView(option) {
    this.markControl = option.markControl;
    this.defaultZoom = 'fit';
    this.init();
    this.markControl.on('task.get.before', this, function(event, context, eventObject) {
        this.task = undefined;
        this.imageCount = 0;
        this.render();
    });
    this.markControl.on('task.get.success', this, function(event, context, eventObject) {
        this.task = context.task;
        this.imageCount = this.task.pictureUrls.length;
        this.imageServer = context.imageServer;
        this.render();
    });
    this.markControl.on('mark.history.open', this, function(event, context, eventObject) {
        if (this.container != undefined) {
            this.container.perfectScrollbar('update');
        }
        if (this.iviewer != undefined) {
            this.iviewer.iviewer('update');
        }
    });
    this.markControl.on('mark.history.close', this, function(event, context, eventObject) {
        if (this.container != undefined) {
            this.container.perfectScrollbar('update');
        }
        if (this.iviewer != undefined) {
            this.iviewer.iviewer('update');
        }
    });
    this.markControl.on('key.press', this, function(e, context, event) {
        var code = event.keyCode;
        if (this.iviewer != undefined && this.currentConfig != undefined && this.imageCount > 0) {
            if (code == 91) {
                // [按键，切换上一张图片
                var number = this.currentConfig.number;
                if (number > 1) {
                    this.change(number - 1);
                    event.preventDefault();
                    return false;
                }
            } else if (code == 93) {
                // ]按键，切换下一张图片
                var number = this.currentConfig.number;
                if (number < this.imageCount) {
                    this.change(number + 1);
                    event.preventDefault();
                    return false;
                }
            }
        }
    });
    this.markControl.on('key.up', this, function(e, context, event) {
        var code = event.keyCode;
        if (this.iviewer != undefined && this.currentConfig != undefined && this.imageCount > 0) {
            if (code == 37) {
                // left按键，切换上一张图片
                var number = this.currentConfig.number;
                if (number > 1) {
                    this.change(number - 1);
                    event.preventDefault();
                    return false;
                }
            } else if (code == 39) {
                // right按键，切换下一张图片
                var number = this.currentConfig.number;
                if (number < this.imageCount) {
                    this.change(number + 1);
                    event.preventDefault();
                    return false;
                }
            }
        }

    });
}

SimpleImageView.prototype.init = function() {
    this.configCache = {};
    this.container = getDom(this.container_dom, this.markControl).appendTo(this.markControl.container.center);
    this.container.perfectScrollbar({
        wheelSpeed: 20
    });
    this.container.width('100%');
    this.container.height(this.markControl.container.height() - 41);

    this.imageHolder = getDom(this.imageHolder_dom, this.markControl).appendTo(this.container);
    this.imageHolder.width('100%');

    this.centerHeader = getDom(this.center_header_dom, this.markControl).appendTo(this.markControl.container.center.header.find('p.pictures')).parent();
    this.centerHeader.hide();
    this.imageControl = getDom(this.image_control_dom, this.markControl).appendTo(this.markControl.container);
    this.imageControl.css('position', 'absolute');
    this.imageSwitchList = this.imageControl.find('#image-switch-list');

    this.centerHeader.click(this, function(event) {
        var obj = event.data.centerHeader;
        var position = obj.offset();
        var left = position.left;
        var top = position.top + obj.height();
        event.data.imageControl.css('top', top);
        event.data.imageControl.css('left', left);
        event.data.imageControl.toggle();
    });
    this.imageControl.find('a.image-popover-close').click(this, function(event) {
        event.data.imageControl.hide();
    });
    this.imageControl.find('#zoom-out-button').click(this, function(event) {
        var iviewer = event.data.iviewer;
        if (iviewer != undefined) {
            iviewer.iviewer('zoom_by', 1);
        }
    });
    this.imageControl.find('#zoom-in-button').click(this, function(event) {
        var iviewer = event.data.iviewer;
        if (iviewer != undefined) {
            iviewer.iviewer('zoom_by', -1);
        }
    });
    this.imageControl.find('#zoom-fit-button').click(this, function(event) {
        var iviewer = event.data.iviewer;
        if (iviewer != undefined) {
            iviewer.iviewer('fit');
        }
    });
    this.imageControl.find('#zoom-origin-button').click(this, function(event) {
        var iviewer = event.data.iviewer;
        if (iviewer != undefined) {
            iviewer.iviewer('set_zoom', 100);
        }
    });
}

SimpleImageView.prototype.render = function() {
    if (this.task != undefined) {
        var config = this.configCache[this.task.blockId];
        if (config == undefined) {
            config = {
                number: 1,
                zoom: this.defaultZoom
            };
            this.configCache[this.task.blockId] = config;
        }
        if (config.number == undefined || config.number < 0 || config.number > this.imageCount) {
            config.number = 1;
        }
        this.currentConfig = config;

        for (var i = 1; i <= this.imageCount; i++) {
            var switchButton = getDom(this.image_switch_button_dom, this.markControl).appendTo(this.imageSwitchList);
            switchButton.html(i);
            switchButton.attr('data-number', i);
            if (i == config.number) {
                switchButton.addClass('curr');
            }
        }
        this.imageSwitchList.find('.image-switch-button').click(this, function(event) {
            var number = $(event.target).attr('data-number');
            event.data.change(number, config.zoom, config);
            event.data.imageControl.hide();
        });

        if (this.iviewer == undefined) {
            var simpleImageView = this;
            this.iviewer = this.imageHolder.iviewer({
                src: this.imageServer + this.task.pictureUrls[config.number - 1],
                zoom: config.zoom,
                ui_disabled: true,
                mousewheel: false,
                zoom_animation: false,
                onZoom: function(ev, new_zoom) {
                    simpleImageView.onZoomSet(new_zoom);
                },
                onStopDrag: function(ev, point) {
                    simpleImageView.onStopDrag(point);
                },
                onFinishLoad: function(ev, url) {
                    simpleImageView.onImageLoad();
                }
            });
            this.centerHeader.find('#image-number').html(config.number);
        } else {
            this.change(config.number, config.zoom, config);
        }
        this.imageHolder.show();
        this.centerHeader.show();

        this.imageHolder.trigger('mouseenter');
    } else {
        this.centerHeader.hide();
        this.imageHolder.hide();
        this.currentConfig = undefined;
        this.imageSwitchList.empty();
    }
}

SimpleImageView.prototype.change = function(number) {
    if (this.task != undefined && number > 0 && number <= this.imageCount && this.currentConfig != undefined && this.currentConfig.loading == false) {
        this.currentConfig.loading = true;
        this.currentConfig.number = number;

        this.iviewer.iviewer('loadImage', this.imageServer + this.task.pictureUrls[number - 1]);
        this.centerHeader.find('#image-number').html(number);
        this.imageSwitchList.find('.image-switch-button').each(function(index, obj) {
            if ($(obj).attr('data-number') == number) {
                $(obj).addClass('curr');
            } else {
                $(obj).removeClass('curr');
            }
        });
    }
}

SimpleImageView.prototype.onImageLoad = function() {
    this.currentConfig.loading = false;
    this.imageHolder.find('img').css('top', '0');
    this.iviewer.iviewer('set_zoom', this.currentConfig.zoom);
    if (this.currentConfig.dx != undefined && this.currentConfig.dy != undefined) {
        this.iviewer.iviewer('moveTo', point.dx, point.dy);
    }
}

SimpleImageView.prototype.onZoomSet = function(zoom) {
    if (this.currentConfig.loading == false) {
        this.currentConfig.zoom = zoom;
    }
}

SimpleImageView.prototype.onStopDrag = function(point) {
    if (this.currentConfig.loading == false) {
        this.currentConfig.dx = point.x;
        this.currentConfig.dy = point.y;
    }
}

//提示警告信息
function WarningInfo(option) {
    this.markControl = option.markControl;
    this.init();

    this.markControl.on('task.get.success', this, function(event, context, eventObject) {
        this.toggle(false);
    });
    this.markControl.on('task.get.none', this, function(event, context, message) {
        this.toggle(true, '当前无评卷任务');
    });
    this.markControl.on('task.get.error', this, function(event, context, eventObject) {
        this.toggle(true, '领取评卷任务出错');
    });
}

WarningInfo.prototype.init = function() {
    this.container = getDom(this.container_dom, this.markControl);
    this.container.hide();
    this.container.appendTo(this.markControl.container.sidebar);

    this.container.message = this.container.find('p');
    this.container.refreshButton = this.container.find('a');
    this.container.refreshButton.click(this, function(event) {
        var refreshButton = event.data.container.refreshButton;
        refreshButton.addClass('disabled');
        refreshButton.html('正在领取');

        event.data.markControl.getTask();
    });
}

WarningInfo.prototype.toggle = function(enable, message) {
    if (enable) {
        this.container.message.html(message);
        this.container.refreshButton.removeClass('disabled');
        this.container.refreshButton.html('请点击重试');
        this.container.show();
    } else {
        this.container.hide();
    }
}