{% extends "base.html" %}

{% block content %}
<style>
    /* Styling to make rating radio buttons render horizontally and look neat */
    .rating-container ul {
        list-style-type: none;
        padding-left: 0;
        margin-bottom: 0;
        display: flex;
        gap: 1.5rem;
        flex-wrap: wrap;
    }
    .rating-container li {
        display: inline-flex;
        align-items: center;
        gap: 0.35rem;
        cursor: pointer;
    }
    .rating-container input[type="radio"] {
        margin-top: 0;
    }
    /* Add bootstrap styling to standard fields */
    .survey-field-input select {
        display: block;
        width: 100%;
        padding: 0.375rem 2.25rem 0.375rem 0.75rem;
        font-size: 1rem;
        font-weight: 400;
        line-height: 1.5;
        color: #fff;
        background-color: #000;
        background-clip: padding-box;
        border: 1px solid #495057;
        border-radius: 0.25rem;
    }
    .survey-field-input textarea {
        display: block;
        width: 100%;
        padding: 0.375rem 0.75rem;
        font-size: 1rem;
        font-weight: 400;
        line-height: 1.5;
        color: #fff;
        background-color: #000;
        background-clip: padding-box;
        border: 1px solid #495057;
        border-radius: 0.25rem;
    }
</style>

<div class="container my-5">
    <div class="row justify-content-center">
        <div class="col-lg-8">
            <div class="card bg-dark border-secondary text-white shadow">
                <div class="card-header border-secondary py-4 text-center">
                    <h1 class="h3 text-white mb-2">{{ survey.name }}</h1>
                    {% if survey.description %}
                        <p class="text-muted mb-0">{{ survey.description|linebreaksbr }}</p>
                    {% endif %}
                    {% if pre_or_post == 'PRE' %}
                        <div class="badge bg-primary mt-2">Pre-exam survey</div>
                    {% elif pre_or_post == 'POST' %}
                        <div class="badge bg-success mt-2">Post-exam survey</div>
                    {% endif %}
                </div>
                <div class="card-body p-4">
                    {% if survey.before_text %}
                        <div class="alert alert-info border-secondary text-white mb-4" style="background-color: #1a202c;">
                            {{ survey.before_text|linebreaksbr }}
                        </div>
                    {% endif %}

                    {% if form.errors %}
                        <div class="alert alert-danger" role="alert">
                            <i class="bi bi-exclamation-triangle-fill me-2"></i>Please correct the errors below before submitting.
                        </div>
                    {% endif %}

                    <form method="post">
                        {% csrf_token %}
                        <!-- Context and routing parameters -->
                        <input type="hidden" name="pre_or_post" value="{{ pre_or_post }}">
                        <input type="hidden" name="content_type_id" value="{{ content_type_id|default:'' }}">
                        <input type="hidden" name="object_id" value="{{ object_id|default:'' }}">
                        <input type="hidden" name="cid" value="{{ cid|default:'' }}">
                        <input type="hidden" name="passcode" value="{{ passcode|default:'' }}">
                        <input type="hidden" name="next" value="{{ next|default:'' }}">

                        {% for field in form %}
                            <div class="mb-4 pb-4 border-bottom border-secondary">
                                <label class="form-label fw-semibold text-white d-block mb-2">
                                    {{ forloop.counter }}. {{ field.label }}
                                    {% if field.field.required %}<span class="text-danger">*</span>{% endif %}
                                </label>
                                
                                <div class="survey-field-input">
                                    {% if field.field.widget.input_type == 'radio' %}
                                        <div class="rating-container bg-black p-3 rounded border border-secondary d-inline-block">
                                            {{ field }}
                                            <div class="d-flex justify-content-between mt-1 small text-muted px-1" style="min-width: 200px;">
                                                <span>Poor</span>
                                                <span>Excellent</span>
                                            </div>
                                        </div>
                                    {% else %}
                                        {{ field }}
                                    {% endif %}
                                </div>
                                
                                {% if field.errors %}
                                    <div class="text-danger small mt-1">
                                        {{ field.errors }}
                                    </div>
                                {% endif %}
                            </div>
                        {% endfor %}

                        <div class="d-flex align-items-center justify-content-between mt-4">
                            <button type="submit" class="btn btn-primary btn-lg">Submit Feedback</button>
                            {% if mandatory == 'false' and skip_key %}
                                <a href="{% url 'survey:survey_skip' %}?next={{ next|urlencode }}&key={{ skip_key|urlencode }}" class="btn btn-outline-secondary btn-lg">Skip Survey</a>
                            {% endif %}
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
