<%@ 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"/>
    <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>
    
    <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>
    
    <script type="text/javascript" src="${ctxStatic}/viewer/viewer.js"></script>
    <link href="${ctxStatic}/viewer/viewer.css" rel="stylesheet">

</head>
<body id="index">
<div id="json-view-content" class="rich-text">
</div>
</body>
<script type="text/javascript">
    var answerUrl = "${fileServer}${answerUrl}";
    var paperUrl = "${fileServer}${paperUrl}";
    var questionNumbers = "${questionNumbers}";

    $(document).ready(function () {
        initJsonPopover(answerUrl,paperUrl, questionNumbers);
    });

    function initJsonPopover(answerUrl, paperUrl,questionNumbers) {
        questionNumbers = questionNumbers.split(",");
        let questionArr = {};
        questionNumbers.forEach(questionNumber => {
            questionArr[questionNumber] = true;
        });
        let paperqQuestions=[];
        $('#json-view-content').empty();
        $.ajax({
            type: "GET",
            url: paperUrl,
            dataType: "json",
            success: function (data) {
            	paperqQuestions = data;
                $.ajax({
                    type: "GET",
                    url: answerUrl,
                    dataType: "json",
                    success: function (data) {
                        //var data ='[{"mainNumber": 6,"subNumber": 1,"body": {"sections": [{ "blocks":[{"type": "text","value": "我是题目我是题目我是题目"}]}]},"answer": {"sections": [{"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"}]}]}}]';
                        //data = JSON.parse(data);
                        let questions = data || [];
                        for (var j in questionArr) {
                        	var mainNumber = j.split(".")[0];
                        	var subNumber = j.split(".")[1];
                        	let jsons = findJson(mainNumber, subNumber, questions)
                        	let paperQuestion = findQuestion(mainNumber, subNumber, paperqQuestions)
                            // 按题号过滤
                            if (questionArr[mainNumber + "." + subNumber] === true) {
                                
                            }
                        	let numberTitle = {
                                    "blocks": [{
                                        "type": "text",
                                        "param": {"bold": true},
                                        "value": "题号：" + mainNumber + "-" + 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(jsons) + ")"
                                    }]
                                };
                                let standardTitle = {
                                    "blocks": [{
                                        "type": "text",
                                        "param": {"bold": true},
                                        "value": "标准答案："
                                    }]
                                };
                                this.holder = $('#json-view-content');
                                $(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);
                                if(jsons.length > 0){
                                	jsons.forEach(node => {
                                		node.answer.forEach(node => {
                                			$(renderRichText(node)).appendTo(this.holder);
                                		});
                                	})
                                }
                                $(renderSection(standardTitle)).appendTo(this.holder);
                                let nodes =renderRichText(paperQuestion.answer);
                                nodes.forEach(node => {
                                    $(node).appendTo(this.holder);
                                })
                       		}
                        if (this.viewer != undefined) {
                            this.viewer.destroy();
                        }
                        const viewer = new Viewer($('#json-view-content')[0], {});
                    }
                });
            }
        })
    }

    
    function textCount(jsons) {
        jsons = Array.isArray(jsons) ? jsons : []
        let count = 0
        jsons.forEach(data => {
            count += this.jsonTextCount(data.answer)
        })
        return count;
    }
    function jsonTextCount(jsonData) {
        let count = 0
        if (Array.isArray(jsonData)) {
            for (let i in jsonData) {
                count += this.jsonTextCount(jsonData[i])
            }
        } else {
            let sections = Array.isArray(jsonData.sections) ? jsonData.sections : [];
            sections.forEach(section => {
                let blocks = Array.isArray(section.blocks) ? section.blocks : [];
                blocks.forEach(block => {
                    if (block.type === 'text' && block.value != undefined) {
                        count += block.value.length;
                    }
                });
            });
        }
        return count
    }
    function findJson (mainNumber, subNumber, jsonData) {
        jsonData = Array.isArray(jsonData) ? jsonData : {}
        let result = []
        for (let i in jsonData) {
            if (jsonData[i].mainNumber == mainNumber && jsonData[i].subNumber == subNumber) {
                result.push(jsonData[i])
            }
        }
        return result
    }
    
    function findQuestion (mainNumber, subNumber, paperData) {
        paperData = paperData || {}
        if (Array.isArray(paperData.details)) {
            for (let i in paperData.details) {
                let detail = paperData.details[i]
                if (detail.number == mainNumber && Array.isArray(detail.questions)) {
                    for (let j in detail.questions) {
                        if (detail.questions[j].number == subNumber) {
                            return detail.questions[j]
                        }
                        if(detail.questions[j].subQuestions!=undefined){
                        	for (let k in detail.questions[j].subQuestions) {
                        		var sub = detail.questions[j].number+'-'+detail.questions[j].subQuestions[k].number;
                        		if (sub == subNumber) {
                        			var q =detail.questions[j].subQuestions[k];
                        			q.parentBody = detail.questions[j].body;
                                    return q;
                                }
                        	}
                        }
                    }
                }
            }
        } else if (Array.isArray(paperData.questions)) {
            for (let j in paperData.questions) {
                if (paperData.questions[j].mainNumber == mainNumber && paperData.questions[j].subNumber == subNumber) {
                    return paperData.questions[j]
                }
            }
        }
        return undefined
    }
</script>



