<%@ page language="java" pageEncoding="utf-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>阅卷平台</title>
    <meta name="viewport" content="width=device-width,initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"/>
    <link rel="stylesheet" href="${ctxStatic}/inspected/css/style.css"/>
    <script type="text/javascript" src="${ctxStatic}/mark-new/js/jquery.min.js"></script>
    <script type="text/javascript" src="${ctxStatic}/mark-new/js/jquery-ui.min.js "></script>
    <script type="text/javascript" src="${ctxStatic}/mark-new/js/jquery.mousewheel.min.js"></script>
    
    <script type="text/javascript" src="${ctxStatic}/answer-check/js/common.js"></script>
    
    <link rel="stylesheet" href="${ctxStatic}/iviewer/jquery.iviewer.css"/>
    <script type="text/javascript" src="${ctxStatic}/iviewer/jquery.iviewer.js"></script>
    
    <script type="text/javascript" src="${ctxStatic}/mark-new/js/jquery-ui.min.js"></script>
    <script type="text/javascript" src="${ctxStatic}/mark-new/js/jquery.mousewheel.min.js"></script>
    
    <script type="text/javascript" src="${ctxStatic}/utils/image-utils.js"></script>

</head>
<body>
<div style="height: 100vh;">
    <div class="c-top cl">
        <div class="z">
            <span><b>编号：</b><span id="studentId"></span></span>
            <span><b>科目：</b><span id="subject"></span></span>
            <span><b>待复核：</b><span id="progress">${inspectCount}</span></span>
        </div>
        <div class="y">
            <span><a class="user" href="">${web_user.name}</a></span>
            <span><a class="close" href="javascript:window.close()">关闭</a></span>
        </div>
    </div>
    <div class="c-cont cl">
        <div class="mn">
            <div id="image-holder-track" class="image-content" style="position: relative; "></div>
        </div>
        <div class="sd">
            <div class="c-button"><a href="#" id="save-button">复核</a></div>
            <div class="c-theadlist">
                <h1>试卷总分：<span id="totalScore"></span></h1>
                <div id="question-content">
                    <div class="mt">
                        <table cellpadding="0" cellspacing="0" width="100%">
                            <!-- <thead>
                                <tr>
                                    <td>题号</td>
                                    <td>给分</td>
                                    <td class="td"><a href="" id="back-button">打回</a></td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>1</td>
                                    <td>2</td>
                                    <td class="td"></td>
                                </tr>
                            </tbody> -->
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">
    var message = '${message}';
    var ids = [${ids}];
    var studentId = '${studentId}';
    var current;
    var student;
    var iviewer;
    var regex = /^[a-z]+$/ig;
    $(document).ready(function () {
        if (message != undefined && message != '') {
            alert(message);
        }

        $('#save-button').click(save);

        if (ids.length == 0) {
            $(".cont cl").css("display", "none");
            return;
        }
        $(".tipsbox").css("display", "none");

        window.onbeforeunload = function () {
            $.post('${ctx}/admin/exam/inspected/clear', {
                studentId: student.id
            }, function (result) {
            });
        }
		if(studentId){
			$.post('${ctx}/admin/exam/inspected/clear', {
                studentId: studentId
            }, function (result) {
            });
		}
		
        $('#progress').html(ids.length);
        process(1);
    });

    function process(index) {

        if (index < 1) {
            return;
        }
        if (index > ids.length) {
            alert('所有考生已处理完毕，请返回重新搜索');
            window.close();
            return;
        }
        current = index;
        $('#answer-content').empty();
        $('#image-holder-track').hide();
        $.post('${ctx}/admin/exam/inspected/info', {
            studentId: ids[index - 1]
        }, function (result) {
            if (result.success == true) {
                student = result;
                render();
            } else {
                //$('#progress').html(ids.length-current);
                process(current + 1);
            }
        }).error(function () {
            alert('获取考生信息出错');
            onProcessFinish(true);
        });
    }

    function onProcessFinish(error) {
        if (!error) {
            $('#save-button').removeAttr("disabled");
        }
    }

    function render() {
        $('#studentId').html(student.secretNumber);
        $('#subject').html(student.subjectCode + '-' + student.subjectName);
        $('#totalScore').html(student.totalScore);
        $("#question-content").empty();

        for (var i = 0; i < student.groups.length; i++) {
            var group = student.groups[i];
            $('<div class="mt" id="div_' + group.groupNumber + '"></div>').appendTo("#question-content");

            $('<table id="table_' + group.groupNumber + '" cellpadding="0" cellspacing="0" width="100%"></table>').appendTo("#div_" + group.groupNumber);

            $("<thead><tr><td>分组" + group.groupNumber + "</td><td>给分 &nbsp;<a href='#' onclick='back("+group.groupNumber+")'>打回</a></td></tr></thead>").appendTo("#table_" + group.groupNumber);
            $('<tbody id="tbody_' + group.groupNumber + '"></tbody>').appendTo("#table_" + group.groupNumber);
            for (var j = 0; j < group.questions.length; j++) {
                var question = group.questions[j];
                $("<tr><td>" +question.mainTitle+":"+ question.questionNumber + "</td><td>" + question.score + "</td></tr>").appendTo("#tbody_" + group.groupNumber);
            }
        }

        var pane = $('<canvas id="track-builder-canvas"></canvas>').appendTo($('#image-holder-track'));
        var canvas = document.getElementById('track-builder-canvas');
        var ctx = canvas.getContext('2d');
        buildImages('${fileServer}', student.picUrls, null, canvas, ctx, null, null);
        $('#image-holder-track').show();
        onProcessFinish(false);
    }

    function save() {
        if (student == undefined) {
            return;
        }
        $.post('${ctx}/admin/exam/inspected/save', {
            studentId: student.id
        }, function (result) {
            if (result == true) {
                $('#progress').html(ids.length - current);
                process(current + 1);
            } else {
                alert('提交失败，请稍后重试');
                window.location.reload();
            }
        });
    }

    function back(groupNumber) {
        if (student == undefined) {
            return;
        }
        if (!confirm('确定要打回该评卷任务吗?')) {
            return;
        }
        $.post('${ctx}/admin/exam/inspected/back', {
        	studentId: student.id,
            groupNumber: groupNumber
        }, function (result) {
            if (result.success == true) {
                $('#progress').html(ids.length - current);
                process(current + 1);
            } else {
                alert(result.message);
            }
        });
    }

    function buildImages(imageServer, picUrls, config, canvas, ctx, markTracks, markSpecialTagList) {
        new ImageLoader({
            server: imageServer,
            flush: true,
            strict: false,
            split: [0,1]
        }).combine(picUrls, canvas, config, function () {

        })
    }

</script>
</html>