{% extends 'base.html' %}
{% load help_tags %}

{% block content %}
    <div class="question-display-block">
        <div class="hide-show">HIDE/SHOW</div>
        <div class="inner-display-block">
            Question <span class="question-number">1</span>
            <div id="single-dicom-viewer" class="dicom-viewer" data-images="" data-annotations=''>
            </div>
            <div class="feedback"></div>
            <div>
                <details>
                    <summary>Answers:</summary><span class="answers"></span>
                </details>
            </div>
        </div>
    </div>
    <div class="rapids">
        {% include 'user_score_header.html' %}
        <ul class="score-answer-list">
            {% for ans, score, correct_answer in answers_and_marks %}
                <li class="user-answer-li" data-question-number="{{forloop.counter}}">Question {{forloop.counter}} - Correct answer: {% if exam.publish_results %}<span class="correct-answer">{{correct_answer}}</span>{% endif %}
                    <span class="view-question-link" data-qn={{forloop.counter0}}>View</span>
                    {% if request.user.is_staff or view_all_results or request.user in exam.author.all %}
                        {% with q=questions|get_item:forloop.counter0 %}
                            {% with ua=user_answers_map|get_item:q.pk %}
                                {% if ua %}
                                    <a href="{% url 'generic:view_answer_history' app_name='rapids' model_name='UserAnswer' pk=ua.pk %}" class="btn btn-sm btn-outline-info ms-2" style="font-size: 11px; padding: 2px 6px; vertical-align: middle;">History</a>
                                {% endif %}
                            {% endwith %}
                        {% endwith %}
                    {% endif %}
                    <br/>
                    <span class="user-answer-score user-answer-score-{{score}}">
                        <span class="submitted-user-answer">
                            <pre>{{ans}}</pre>
                        </span>
                        {% if exam.publish_results or view_all_results%}
                            <span class="answer-score">Score: {{score}}</span>
                        {% endif %}
                    </span>
                </li>
            {% endfor %}
        </ul>
        {% include 'user_scores_footer.html' %}
    </div>
{% endblock %}
{% block js %}
    <script>

        $(document).ready(function () {
            $(".hide-show").click((evt, el) => {
                $(".inner-display-block").toggle();
            })

            $(".view-question-link").on("click", function (e) {
                console.log("click", e)
                question_number = e.currentTarget.dataset.qn;
                $("#single-dicom-viewer").empty();
                $.ajax({
                    url: `{% url 'rapids:question_review' pk=exam.pk %}`,
                    data: {
                        csrfmiddlewaretoken: "{{ csrf_token }}",
                        question_number: question_number
                    },
                    type: "POST",
                    dataType: "json",
                })
                // $.ajax().done(), $.ajax().fail(), $ajax().always() are upto you. Add/change accordingly
                    .done(function (data) {
                    console.log(data);

                    let images = data.images

                    if ("feedback_images" in data) {
                        images.push(...data.feedback_images)
                    }

                    let event = new CustomEvent('loadDicomViewerUrls', {
                        "detail": { "images": images, "annotations": data.annotations }
                    });

                    window.dispatchEvent(event);
                    $(".question-display-block .answers").empty().append(data.answers.toString());
                    n = parseInt(question_number) + 1
                    $(".question-display-block .question-number").empty().append(n);
                    $(".question-display-block .feedback").empty().append(`Feedback: ${data.feedback}<br />`);
                    // show some message according to the response.
                    // For eg. A message box showing that the status has been changed
                    $(".inner-display-block").show();
                })
                    .always(function () {
                    console.log('[Done]');
                })
            })

            $(".view-question-link").first().click();

        });
    </script>
{% endblock %}


{% block css %}
<style>
    .answer-score {
        float: right;
    }
    
</style>
{% endblock css %}
    