{% load crispy_forms_tags %}

<div class="p-3">
    <div class="card border-secondary shadow-sm mb-2">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h2 class="h6 mb-0">Self Review</h2>
            <span class="badge bg-primary-subtle text-primary-emphasis">{{ user_exam.exam }}</span>
        </div>
        <div class="card-body">
            <p class="text-muted small mb-2">
                Record a quick reflection while reviewing this case.
            </p>
            <div class="small text-muted mb-3">
                Case: <strong>{{ case }}</strong>
            </div>

            <form method="post" class="self-review-embed-form" action="">
                {% csrf_token %}
                {{ form.non_field_errors }}
                {{ form.user_exam }}
                {{ form.case }}

                <div class="mb-3">
                    {{ form.comments|as_crispy_field }}
                </div>

                <div class="mb-3">
                    <label class="form-label fw-semibold d-block">{{ form.findings.label }}</label>
                    <div class="btn-group" role="group" aria-label="Findings self rating">
                        {% for value, label in form.fields.findings.choices %}
                            <input
                                type="radio"
                                class="btn-check"
                                name="{{ form.findings.html_name }}"
                                id="id_embed_findings_{{ forloop.counter0 }}"
                                value="{{ value }}"
                                {% if form.findings.value|stringformat:'s' == value|stringformat:'s' %}checked{% endif %}
                            >
                            <label class="btn btn-outline-info" for="id_embed_findings_{{ forloop.counter0 }}">{{ label }}</label>
                        {% endfor %}
                    </div>
                    {% if form.findings.help_text %}
                        <div class="form-text">{{ form.findings.help_text }}</div>
                    {% endif %}
                    {% if form.findings.errors %}
                        <div class="text-danger small mt-1">{{ form.findings.errors|striptags }}</div>
                    {% endif %}
                </div>

                <div class="mb-4">
                    <label class="form-label fw-semibold d-block">{{ form.interpretation.label }}</label>
                    <div class="btn-group" role="group" aria-label="Interpretation self rating">
                        {% for value, label in form.fields.interpretation.choices %}
                            <input
                                type="radio"
                                class="btn-check"
                                name="{{ form.interpretation.html_name }}"
                                id="id_embed_interpretation_{{ forloop.counter0 }}"
                                value="{{ value }}"
                                {% if form.interpretation.value|stringformat:'s' == value|stringformat:'s' %}checked{% endif %}
                            >
                            <label class="btn btn-outline-warning" for="id_embed_interpretation_{{ forloop.counter0 }}">{{ label }}</label>
                        {% endfor %}
                    </div>
                    {% if form.interpretation.help_text %}
                        <div class="form-text">{{ form.interpretation.help_text }}</div>
                    {% endif %}
                    {% if form.interpretation.errors %}
                        <div class="text-danger small mt-1">{{ form.interpretation.errors|striptags }}</div>
                    {% endif %}
                </div>

                <div class="d-flex gap-2 flex-wrap">
                    <button type="submit" class="btn btn-primary btn-sm">Save Self Review</button>
                    {% if review %}
                        <a href="{% url 'atlas:self_review_delete' user_exam.id case.id review.id %}" class="btn btn-outline-danger btn-sm">Delete</a>
                    {% endif %}
                </div>
            </form>
            <div class="self-review-embed-message mt-2"></div>
        </div>
    </div>
</div>
