<%@ page contentType="text/html;charset=UTF-8" %>
<link rel="stylesheet" type="text/css" href="${ctxStatic}/jBox/Source/jBox.css">
<script type="text/javascript" src="${ctxStatic}/jBox/Source/jBox.min.js"></script>
<link rel="stylesheet" type="text/css" href="${ctxStatic}/rich-text/css/rich-text.css">
<script type="text/javascript" src="${ctxStatic}/rich-text/js/render.js"></script>

<div id="trial-detail-content" class="container-fluid" style="display:none">
    <div class="row-fluid">
        <div id="trial-left-div" class="span7" style="overflow-y:scroll">
        </div>
        <div id="trial-right-div" class="span5">
            <table id="contentTable" class="table table-striped table-bordered table-condensed">
                <thead>
                <tr>
                    <th>评卷员</th>
                    <th>姓名</th>
                    <th>给分</th>
                    <th>时间</th>
                </tr>
                </thead>
                <tbody id="trial-history">
                </tbody>
            </table>
        </div>
    </div>
</div>

<script type="text/javascript">
    var trialDetailModal = new jBox('Modal', {isolateScroll: false});

    $(document).ready(function () {
        $('#trial-left-div').height($(window).height() * 0.83);
    });

    function initTrialDetailPopover(title, libraryId) {
        initTrialDetailContent(libraryId);
        trialDetailModal.setWidth($(window).width() * 0.9);
        trialDetailModal.setHeight($(window).height() * 0.85);
        trialDetailModal.setTitle(title);
        trialDetailModal.open();
    }

    function initTrialDetailContent(libraryId) {
        $('#trial-history').empty();
        $('#trial-left-div').empty();
        $.ajax({
            type: "GET",
            url: '${ctx}/admin/exam/trial/detail?libraryId=' + libraryId,
            dataType: "json",
            success: function (data) {
            	if (data.success == true) {
                    for (var i = 0; i < data.historyList.length; i++) {
                        var history = data.historyList[i];
                        $('#trial-history').append('<tr><td>' + history.loginName + '</td><td>' + history.name + '</td><td>' + history.score + '</td><td>' + history.time + '</td></tr>');
                    }
                    var pane = $('<canvas id="trial-left-canvas"></canvas></div>').appendTo($('#trial-left-div'));
                    var canvas = document.getElementById('trial-left-canvas');
                    if (data.fileServer != undefined && data.pictureConfig != undefined) {
                    	new ImageLoader({
                            server: data.fileServer,
                            flush: true,
                            strict: false,
                            split: [${properties['slice.split.config']}]
                        }).combine(data.urls,canvas,data.pictureConfig, function () {
                            trialDetailModal.setContent($('#trial-detail-content'));
                        }, function (error) {
                            trialDetailModal.setTitle('加载失败');
                        })
                    } 
                    //以下为多媒体
                    else if (data.fileServer != undefined && data.questionNumbers != undefined) {
                        var url = data.fileServer + data.urls;
                        var paperUrl = data.fileServer + data.paperUrl;
                        var answerUrl = data.fileServer + data.urls;
                        var questionNumbers = data.questionNumbers.split(",");
                        let questionArr = {};
                        questionNumbers.forEach(questionNumber => {
                            questionArr[questionNumber] = true;
                        });
                        let paperqQuestions = [];
                        $.ajax({
                            type: "GET",
                            url: paperUrl,
                            dataType: "json",
                            success: function (data) {
                                paperqQuestions = data.questions;
                            }
                        })
                        $.ajax({
                            type: "GET",
                            url: answerUrl,
                            dataType: "json",
                            success: function (data) {
                               	let questions = data || [];
                                //var data = '[{"mainNumber": 6,"subNumber": 19,"body": {"sections": [{ "blocks":[{"type": "text","value": "我是题目我是题目我是题目"}]}]},"answer": {"sections": [{"blocks":[{"type": "image","value": "https://ecs-test-static.qmth.com.cn/oe-answer-file/3/8/20/3_8_20_15748452889591137.jpeg"}]},{"blocks":[{"type": "audio","value": "https://ecs-test-static.qmth.com.cn/oe-answer-file/3/8/19/3_8_19_15748452552644264.mp3"}]}]},"studentAnswer": {"sections": [{"blocks":[{"type": "image","value": "https://ecs-test-static.qmth.com.cn/oe-answer-file/3/8/20/3_8_20_15748452889591137.jpeg"},{"type": "image","value": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1582709869784&di=39683b1330bd09bebd93c95fe925ca70&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn10108%2F170%2Fw600h370%2F20190222%2Fd6de-htknpmh2595255.jpg"},{"type": "text","value": "我是答案答案daan"}]}]}}]';
                                //let questions = JSON.parse(data);
                                for (var j in questions) {
                                	  var question = questions[j];
                                      var paperQuestion = paperqQuestions[j];
                                      // 按题号过滤
                                      if (questionArr[question.mainNumber + "." + question.subNumber] === true) {
                                          let numberTitle = {
                                              "blocks": [{
                                                  "type": "text",
                                                  "param": {"bold": true},
                                                  "value": "题号：" + question.mainNumber + "-" + question.subNumber
                                              }]
                                          };
                                          let bodyTitle = {
                                              "blocks": [{
                                                  "type": "text",
                                                  "param": {"bold": true},
                                                  "value": "题干："
                                              }]
                                          };
                                          let answerTitle = {
                                              "blocks": [{
                                                  "type": "text",
                                                  "param": {"bold": true},
                                                  "value": "考生答案："
                                              }, {
                                                  "type": "text",
                                                  "param": {
                                                      "italic": true,
                                                      "danger": true
                                                  },
                                                  "value": "(字数统计：" + textCount(question.answer) + ")"
                                              }]
                                          };
                                          let standardTitle = {
                                              "blocks": [{
                                                  "type": "text",
                                                  "param": {"bold": true},
                                                  "value": "标准答案："
                                              }]
                                          };
                                          this.holder = $('#trial-left-div');
                                          $(renderSection(numberTitle)).appendTo(this.holder);
                                          $(renderSection(bodyTitle)).appendTo(this.holder);
                                          if (paperQuestion.parentBody != null && paperQuestion.parentBody != undefined) {
                                              $(renderRichText(paperQuestion.parentBody)).appendTo(this.holder);
                                          }
                                          $(renderRichText(paperQuestion.body)).appendTo(this.holder);
                                          $(renderSection(answerTitle)).appendTo(this.holder);
                                          $(renderRichText(question.answer)).appendTo(this.holder);
                                          $(renderSection(standardTitle)).appendTo(this.holder);
                                          $(renderRichText(paperQuestion.answer)).appendTo(this.holder);
                                          trialDetailModal.setContent($('#trial-detail-content'));
                                      }
                                }
                         
                            }
                        }).error(function () {
                            trialDetailModal.setTitle('加载失败');
                        });
                    }   
            	} else {
                    trialDetailModal.setTitle('加载失败');
                }
            }
        });
    }

    function textCount(jsonBody) {
        let sections = jsonBody.sections || [];
        let count = 0;
        sections.forEach(section => {
            let blocks = section.blocks || [];
            blocks.forEach(block => {
                if (block.type === 'text' && block.value != undefined) {
                    count += block.value.length;
                }
            });
        });
        return count;
    }
</script>



