{% extends "layouts/base.html" %}
{% load i18n %}
{% load static %}

{% block title %}{{ _("Survey Instances")}} - PX360{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="mb-1">
                <i class="bi bi-clipboard-data text-info me-2"></i>
                {{ _("Survey Instances")}}
            </h2>
            <p class="text-muted mb-0">{{ _("Monitor survey responses and scores")}}</p>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card border-left-primary">
                <div class="card-body">
                    <h6 class="text-muted mb-1">{% trans "Total Surveys" %}</h6>
                    <h3 class="mb-0">{{ stats.total }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-left-warning">
                <div class="card-body">
                    <h6 class="text-muted mb-1">{% trans "Sent" %}</h6>
                    <h3 class="mb-0">{{ stats.sent }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-left-success">
                <div class="card-body">
                    <h6 class="text-muted mb-1">{% trans "Completed" %}</h6>
                    <h3 class="mb-0">{{ stats.completed }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-left-danger">
                <div class="card-body">
                    <h6 class="text-muted mb-1">{% trans "Negative" %}</h6>
                    <h3 class="mb-0 text-danger">{{ stats.negative }}</h3>
                </div>
            </div>
        </div>
    </div>

    <!-- Surveys Table -->
    <div class="card">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>{% trans "Patient" %}</th>
                            <th>{% trans "Survey Template" %}</th>
                            <th>{% trans "Journey Stage" %}</th>
                            <th>{% trans "Status" %}</th>
                            <th>{% trans "Score" %}</th>
                            <th>{% trans "Sent" %}</th>
                            <th>{% trans "Completed" %}</th>
                            <th>{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for survey in surveys %}
                        <tr onclick="window.location='{% url 'surveys:instance_detail' survey.id %}'" style="cursor: pointer;">
                            <td>
                                <strong>{{ survey.patient.get_full_name }}</strong><br>
                                <small class="text-muted">MRN: {{ survey.patient.mrn }}</small>
                            </td>
                            <td>{{ survey.survey_template.name }}</td>
                            <td>
                                {% if survey.journey_stage_instance %}
                                <small>{{ survey.journey_stage_instance.stage_template.name }}</small>
                                {% else %}
                                <span class="text-muted">-</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if survey.status == 'completed' %}
                                <span class="badge bg-success">{{ survey.get_status_display }}</span>
                                {% elif survey.status == 'pending' %}
                                <span class="badge bg-warning">{{ survey.get_status_display }}</span>
                                {% elif survey.status == 'active' %}
                                <span class="badge bg-info">{{ survey.get_status_display }}</span>
                                {% elif survey.status == 'cancelled' %}
                                <span class="badge bg-danger">{{ survey.get_status_display }}</span>
                                {% else %}
                                <span class="badge bg-secondary">{{ survey.get_status_display }}</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if survey.total_score %}
                                <strong class="{% if survey.is_negative %}text-danger{% else %}text-success{% endif %}">
                                    {{ survey.total_score|floatformat:1 }}/5.0
                                </strong>
                                {% else %}
                                <span class="text-muted">-</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if survey.sent_at %}
                                <small>{{ survey.sent_at|date:"M d, Y" }}</small>
                                {% else %}
                                <span class="text-muted">-</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if survey.completed_at %}
                                <small>{{ survey.completed_at|date:"M d, Y" }}</small>
                                {% else %}
                                <span class="text-muted">-</span>
                                {% endif %}
                            </td>
                            <td onclick="event.stopPropagation();">
                                <a href="{% url 'surveys:instance_detail' survey.id %}" 
                                   class="btn btn-sm btn-outline-primary">
                                    <i class="bi bi-eye"></i>
                                </a>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="8" class="text-center py-5">
                                <i class="bi bi-inbox" style="font-size: 3rem; color: #ccc;"></i>
                                <p class="text-muted mt-3">{{ _("No surveys found")}}</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <nav aria-label="Surveys pagination" class="mt-4">
        <ul class="pagination justify-content-center">
            {% if page_obj.has_previous %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                    <i class="bi bi-chevron-left"></i>
                </a>
            </li>
            {% endif %}
            
            {% for num in page_obj.paginator.page_range %}
                {% if page_obj.number == num %}
                <li class="page-item active"><span class="page-link">{{ num }}</span></li>
                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                <li class="page-item">
                    <a class="page-link" href="?page={{ num }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                        {{ num }}
                    </a>
                </li>
                {% endif %}
            {% endfor %}
            
            {% if page_obj.has_next %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.next_page_number }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                    <i class="bi bi-chevron-right"></i>
                </a>
            </li>
            {% endif %}
        </ul>
    </nav>
    {% endif %}
</div>
{% endblock %}
