//回评模块
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.order = "time"
    this.sort = "desc";
    var isTag = 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];
                task.totalScore = parseNumber(task.totalScore);
                var row = getDom(this.history_row_dom, this.markControl).appendTo(this.container.list);
                row.find('.history-secret-number').html(task.studentNumber);
                if (task.tags != undefined && task.tags != null) {
                    row.find('.history-secret-number').addClass('tips');
                }
                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.updatePage();
        this.updateSort(this.order, this.sort);

        if (this.taskList.length > 0) {
            this.onTaskSelect(0);
        }
    });
    this.markControl.on('history.get.error', this, function (event, context, data) {
        alert(getMessage("mark.history.error"));
    });
    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;
    });
    this.markControl.on('history.submit.success', this, function (event, context, task) {
        if (task.previous == true) {
            this.updateTaskScore(task.totalScore);
        }
        if (task.problem == true) {
            var self = this;
            self.toggle(false);
            self.markControl.context.task = undefined;
            self.markControl.getTask();
        }
    });
}

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

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

    var self = this;
    this.container.header.find('#close-history-button').click(this, function (event) {
        event.data.toggle(false);
        self.markControl.context.task = undefined;
        self.markControl.getTask();
    })
    this.container.paginator.find('#last-page-button').click(this, function (event) {
        var markHistory = event.data;
        if (markHistory.taskList.length < markHistory.pageSize) {
            return;
        }
        if (self.container.search.find('#history-isTag').prop("checked")) {
            markHistory.isTag = true;
        } else {
            markHistory.isTag = false;
        }
        markHistory.onSearch(markHistory.pageNumber + 1, markHistory.order, markHistory.sort, markHistory.isTag);
    });
    this.container.paginator.find('#next-page-button').click(this, function (event) {
        var markHistory = event.data;
        if (markHistory.pageNumber == 1) {
            return;
        }
        if (markHistory.pageNumber > 1) {
            if (self.container.search.find('#history-isTag').prop("checked")) {
                markHistory.isTag = true;
            } else {
                markHistory.isTag = false;
            }
            markHistory.onSearch(markHistory.pageNumber - 1, markHistory.order, markHistory.sort, markHistory.isTag);
        }
    });
    this.container.find('#number-search').click(this, function (event) {
        var markHistory = event.data;
        var number = self.container.find('#number-in').val();
        var re = /^[1-9]+[0-9]*]*$/;
        if (!re.test(number)) {
            alert(getMessage("mark.history.number.error"));
            return;
        }
        if (self.container.search.find('#history-isTag').prop("checked")) {
            markHistory.isTag = true;
        } else {
            markHistory.isTag = false;
        }
        markHistory.onSearch(0, markHistory.order, markHistory.sort, markHistory.isTag, number);
    });
    this.container.find('#time-sort-th').click(this, function (event) {
        self.orderSearch("time");
    });
    this.container.find('#number-sort-th').click(this, function (event) {
        self.orderSearch("number");
    });
    this.container.find('#score-sort-th').click(this, function (event) {
        self.orderSearch("score");
    });

    self.updateSort(self.order, self.sort);
    self.updateLoading();

    this.container.find('#number-in').click(this, function (event) {
        self.markControl.trigger('mark.focus.change');
    });
}
MarkHistory.prototype.orderSearch = function (order) {
    this.order = order;
    if (this.sort == "asc") {
        this.sort = "desc";
    } else if (this.sort == "desc") {
        this.sort = "asc";
    }
    if (this.container.search.find('#history-isTag').prop("checked")) {
        this.isTag = true;
    } else {
        this.isTag = false;
    }
    this.onSearch(this.pageNumber, this.order, this.sort, this.isTag);
}

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.updatePage();
        this.container.list.empty();
        this.container.show();
        this.onSearch();
    } else {
        this.container.hide();
        this.markControl.trigger('mark.sidebar.close');
        this.markControl.trigger('mark.history.close');
    }
}

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

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

}
MarkHistory.prototype.updateLoading = function () {
    if (this.loading) {
        this.container.find('#history-loading').show();
    } else {
        this.container.find('#history-loading').hide();
    }
}

MarkHistory.prototype.updateSort = function (order, sort) {
    this.container.find('#time-sort').removeClass();
    this.container.find('#number-sort').removeClass();
    this.container.find('#score-sort').removeClass();
    if (sort == 'desc') {
        this.container.find('#' + order + '-sort').addClass("down");
    } else {
        this.container.find('#' + order + '-sort').addClass("up");
    }


}

MarkHistory.prototype.onSearch = function (pageNumber, order, sort, isTag, number) {
    if (pageNumber == undefined || pageNumber < 1) {
        pageNumber = 1;
    }
    if (order == undefined) {
        order = "time";
    }
    if (sort == undefined) {
        sort = "desc";
    }
    if (isTag == undefined) {
        isTag = false;
    }
    if (number == undefined || number == "") {
        number = null;
    }
    this.markControl.getHistory({
        pageNumber: pageNumber,
        pageSize: this.pageSize,
        order: order,
        sort: sort,
        isTag: isTag,
        secretNumber: number
    });
}

MarkHistory.prototype.onTaskSelect = function (index) {
    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');
            }
        });

        var task = this.taskList[index];
        this.markControl.setTask(task);
        this.loading = true;
    }
}

MarkHistory.prototype.updateTaskScore = function (score) {
    this.container.list.find('tr').each(function (index, obj) {
        if ($(obj).hasClass("active")) {
            $(obj).find('td').last().html(score);
        }
    });
}

MarkHistory.prototype.toggle_button_dom = '<a href="#" data-i18n-text="mark.history.title">回评</a>';

MarkHistory.prototype.header_dom = '<div class="header">\
<p class="fl" data-i18n-text="mark.history.title">回评</p>\
<a href="#" id="close-history-button"><img src="{staticServer}/mark-new/images/hp-close.png" /></a>\
</div>';

MarkHistory.prototype.paginator_dom = '<div class="c-page">\
<a class="back" id="last-page-button" href="#"></a>\
<a class="next" id="next-page-button" href="#"></a>\
<span><span data-i18n-text="mark.history.pre">前</span><i class="yellow" id="history-start"></i>-<span data-i18n-text="mark.history.pre">前</span><i class="yellow" id="history-end"></i></span>\
</div>';

MarkHistory.prototype.history_search_dom = '<div class="c-scbar cl">\
<table id="history-search" cellpadding="0" cellspacing="0" width="100%">\
<tr><td class="sc"><span><input type="text" placeholder="查找试卷" id="number-in" maxlength="10" data-i18n-placeholder="mark.history.search"/><em id="number-search"></em></span></td>\
</tr></table></div>';

MarkHistory.prototype.history_list_dom = '<div class="sublist"><div class="c-table">\
<table class="table table-hover" cellpadding="0" cellspacing="0" width="100%"><thead><tr>\
<th id="number-sort-th"><span data-i18n-text="mark.history.number">编号</span><em class="up" id="number-sort"></th>\
<th id="time-sort-th"><span data-i18n-text="mark.history.time">时间</span><em class="up" id="time-sort"></em></th>\
<th id="score-sort-th"><span data-i18n-text="mark.history.score">总分</span><em class="up" id="score-sort"></th></tr></thead>\
<tbody class="loding" id="history-loading"><tr><td colspan="3"><div><img src="{staticServer}/mark-new/images/loding.gif"/></div><p data-i18n-text="mark.history.loading">正在加载请稍候</p></td></tr></tbody>\
<tbody id="history-list"></tbody></table></div></div>';

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