{% extends 'sbas/exams.html' %}

{% block navigation %}
    {{ block.super }}
    {% include 'generic/exam_overview_headers.html' %}
{% endblock navigation %}

{% block content %}

    {% load thumbnail %}
    <div class="container sbas">
        <div class="row mb-3">
            <div class="col-12">
                <p class="lead">This exam will be available to take <a href="{% url 'sbas:exam_start' pk=exam.pk %}">here</a> (when active).</p>
            </div>
        </div>

        <h3 class="mb-3">Questions in this Exam</h3>

        <div class="row g-3">
            {% for question in questions.all %}
                <div class="col-12">
                    <div class="card">
                        <div class="card-body">
                            <div class="d-flex justify-content-between align-items-start mb-2">
                                <div class="flex-grow-1">
                                    <span><span class="badge bg-secondary text-white me-2">Q{{ forloop.counter }}</span>: {{ question.title }}</span>
                                    <div class="question-stem">{{ question.stem|safe }}</div>
                                </div>
                                <div class="ms-3 text-end">
                                    <div class="small text-muted">Category: {{ question.category }}</div>
                                    <div class="mt-1">
                                        <a class="btn btn-sm btn-outline-primary" href="{% url 'sbas:question_detail' pk=question.pk %}">View</a>
                                        <a class="btn btn-sm btn-outline-secondary" href="{% url 'admin:sbas_question_change' question.id %}">Edit</a>
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-12 col-md-9">
                                    <ol type="a" class="abcde mb-0">
                                        <li> {{ question.a_answer|safe }} </li>
                                        <li> {{ question.b_answer|safe }} </li>
                                        <li> {{ question.c_answer|safe }} </li>
                                        <li> {{ question.d_answer|safe }} </li>
                                        <li> {{ question.e_answer|safe }} </li>
                                    </ol>
                                </div>
                                <div class="col-12 col-md-3 text-md-end mt-2 mt-md-0">
                                    <div class="small text-muted">Best answer</div>
                                    <div class="mt-1">
                                        {% if question.best_answer %}
                                            <span class="badge bg-primary fs-5">{{ question.best_answer|stringformat:'s'|slice:":1"|upper }}</span>
                                        {% else %}
                                            <span class="text-muted">-</span>
                                        {% endif %}
                                    </div>
                                    <div class="small text-muted mt-2">({{ question.get_correct_answer_stripped }})</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            {% endfor %}
        </div>
    </div>
    <script type="text/javascript">
        $(document).ready(function () {
            // send request to change the is_private state on customSwitches toggle
            $("#exam-active-switch").on("change", function () {
                $.ajax({
                    url: "{% url 'sbas:exam_toggle_active' pk=exam.pk %}",
                    data: {
                        csrfmiddlewaretoken: "{{ csrf_token }}",
                        active: this.checked // true if checked else false
                    },
                    type: "POST",
                    dataType: "json",
                })
                // $.ajax().done(), $.ajax().fail(), $ajax().always() are upto you. Add/change accordingly
                    .done(function (data) {
                        console.log(data);

                        if (data.status == "success") {
                            toastr.info('Exam state changed.')
                        }
                    // show some message according to the response.
                    // For eg. A message box showing that the status has been changed
                    })
                    .always(function () {
                        console.log('[Done]');
                    })
            })
            $("#exam-publish-results-switch").on("change", function () {
                $.ajax({
                    url: "{% url 'sbas:exam_toggle_results_published' pk=exam.pk %}",
                    data: {
                        csrfmiddlewaretoken: "{{ csrf_token }}",
                        publish_results: this.checked // true if checked else false
                    },
                    type: "POST",
                    dataType: "json",
                })
                // $.ajax().done(), $.ajax().fail(), $ajax().always() are upto you. Add/change accordingly
                    .done(function (data) {
                        console.log(data);

                        if (data.status == "success") {
                            toastr.info('Publish results state changed.')
                        }
                    // show some message according to the response.
                    // For eg. A message box showing that the status has been changed
                    })
                    .always(function () {
                        console.log('[Done]');
                    })
            })
        });
    </script>
{% endblock %}
