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

{% block content %}
    <div class="container py-4">
        <div class="row mb-3">
            <div class="col-md-8">
                <h2 class="mb-1">User: {{ cid_user.username }}</h2>
                {% help "Help" %}
                    <div class="card mt-2">
                        <div class="card-body small">
                            <p class="mb-1">This page shows the exam(s) to which you have access.</p>
                            <p class="mb-1">When an exam is available to take it will have a "Start" button next to it.</p>
                            <p class="mb-1">Once you have submitted answers you will be able to view them from the Exam results section. (Please note: although your answers will be visible as soon as they are submitted, correct answers will only be available when the exam results have been published.)</p>
                            <p class="mb-0">If you have not submitted any answer for an exam it will not appear in the Exam Results section below. In this case you can still review the published results by following the "Results Published" link in the assigned exams section.</p>
                        </div>
                    </div>
                {% endhelp %}
            </div>
        </div>

        <div class="row">
            <div class="col-lg-8">
                <div class="card mb-3">
                    <div class="card-header">
                        <h5 class="mb-0">Assigned Exams</h5>
                    </div>
                    <div class="card-body">
                        <p class="text-muted">The following exams will be available to take when active.</p>
                        {% for exam_type, exams in available_exams %}
                            {% if exams %}
                                <h6 class="mt-3">{{ exam_type|title }}</h6>
                                <ul class="list-group">
                                    {% for exam in exams %}
                                        <li class="list-group-item d-flex justify-content-between align-items-center">
                                            <div>
                                                {{ exam }}
                                                {% if exam.active %}<span class="badge bg-success ms-2">Active</span>{% endif %}
                                                {% if exam.publish_results %}<span class="badge bg-info text-dark ms-2">Results Published</span>{% endif %}
                                            </div>
                                            <div class="btn-group btn-group-sm" role="group">
                                                {% if exam.active %}
                                                    <a class="btn btn-primary" href="{{ exam.get_take_url }}" target="_blank" title="Click to take exam">Start</a>
                                                {% endif %}
                                                {% if exam.publish_results %}
                                                    <a class="btn btn-outline-secondary" href="{% url exam_type|lower|add:':exam_scores_user' pk=exam.pk %}" title="Click to view results">Results</a>
                                                {% endif %}
                                            </div>
                                        </li>
                                    {% endfor %}
                                </ul>
                            {% endif %}
                        {% endfor %}
                    </div>
                </div>

                {% if all_exams %}
                    <div class="card mb-3">
                        <div class="card-header">
                            <h5 class="mb-0">Exam Results</h5>
                        </div>
                        <div class="card-body">
                            <p class="text-muted">Click an exam to view answers (and scores when results are published).</p>
                            {% for exam_type, exams in all_exams %}
                                {% if exams %}
                                    <h6 class="mt-3">{{ exam_type|title }}</h6>
                                    <ul class="list-group">
                                        {% for exam in exams %}
                                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                                <div>
                                                    {% if admin %}
                                                        <a href="{% url exam_type|add:':exam_scores_user_admin' pk=exam.pk user_id=user.pk %}">{{ exam }}</a>
                                                    {% else %}
                                                        <a href="{% url exam_type|add:':exam_scores_user' pk=exam.pk %}">{{ exam }}</a>
                                                    {% endif %}
                                                    {% if exam.active %}<span class="badge bg-success ms-2">Active</span>{% endif %}
                                                    {% if exam.publish_results %}<span class="badge bg-info text-dark ms-2">Results Published</span>{% endif %}
                                                </div>
                                                <div class="small text-muted">&nbsp;</div>
                                            </li>
                                        {% endfor %}
                                    </ul>
                                {% endif %}
                            {% endfor %}
                        </div>
                    </div>
                {% endif %}

                {% if case_collections %}
                    <div class="card mb-3">
                        <div class="card-header">
                            <h5 class="mb-0">Case Collections</h5>
                        </div>
                        <div class="card-body">
                            <p class="text-muted">The following Case Collections have been found.</p>
                            <ul class="list-group">
                                {% for collection in case_collections %}
                                    <li class="list-group-item d-flex justify-content-between align-items-center">
                                        <a href="{{ collection.get_take_url }}?cid={{ cid }}&passcode={{ passcode }}">{{ collection.name }}</a>
                                        <div>
                                            {% if collection.active %}<span class="badge bg-success">Active</span>{% endif %}
                                            {% if collection.publish_results %}<span class="badge bg-info text-dark ms-2">Results Published</span>{% endif %}
                                        </div>
                                    </li>
                                {% endfor %}
                            </ul>
                        </div>
                    </div>
                {% endif %}
            </div>

            <div class="col-lg-4">
                <div class="card mb-3">
                    <div class="card-body">
                        <h6 class="card-title">Actions</h6>
                        <p class="small text-muted">Quick links and information.</p>
                        <!-- Placeholder for any future quick actions -->
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
