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

{% block title %}{% trans "Survey Instances" %} - PX360{% endblock %}

{% block extra_css %}
<style>
.page-header-gradient {
    background: linear-gradient(135deg, #005696 0%, #0069a8 50%, #007bbd 100%);
    color: white;
    padding: 1.5rem 2rem;
    border-radius: 1rem;
    margin-bottom: 1.5rem;
    box-shadow: 0 10px 15px -3px rgba(0, 86, 150, 0.2);
}

.section-card {
    background: white;
    border-radius: 1rem;
    border: 2px solid #e2e8f0;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    transition: all 0.3s ease;
}
.section-card:hover {
    border-color: #005696;
    box-shadow: 0 10px 25px -5px rgba(0, 86, 150, 0.15);
}

.section-header {
    padding: 1rem 1.5rem;
    border-bottom: 2px solid #e2e8f0;
    background: linear-gradient(to right, #f8fafc, #f1f5f9);
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.section-icon {
    width: 40px;
    height: 40px;
    border-radius: 0.75rem;
    display: flex;
    align-items: center;
    justify-content: center;
}
</style>
{% endblock %}

{% block content %}
<!-- Page Header -->
<div class="page-header-gradient">
    <div class="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4">
        <div>
            <h1 class="text-2xl font-bold flex items-center gap-3">
                <div class="section-icon bg-white/20">
                    <i data-lucide="clipboard-check" class="w-6 h-6 text-white"></i>
                </div>
                {% trans "Survey Instances" %}
            </h1>
            <p class="text-white/80 mt-2">{% trans "Monitor survey responses and scores" %}</p>
        </div>
        <div class="flex items-center gap-3">
            <div class="relative group">
                <i data-lucide="search" class="w-4 h-4 absolute left-3 top-1/2 -translate-y-1/2 text-white/70 group-focus-within:text-white"></i>
                <input type="text" id="searchInput" placeholder="{% trans 'Search MRN, Name...' %}" 
                       class="pl-10 pr-4 py-2.5 bg-white/20 border-white/30 border focus:border-white/50 focus:bg-white/30 rounded-xl text-sm outline-none w-64 transition-all text-white placeholder-white/70"
                       value="{{ filters.search|default:'' }}">
            </div>
            <button onclick="toggleFilters()" class="bg-white/20 text-white px-5 py-2.5 rounded-xl text-sm font-bold hover:bg-white/30 flex items-center gap-2 transition border border-white/30">
                <i data-lucide="filter" class="w-4 h-4"></i> {% trans "Filters" %}
            </button>
        </div>
    </div>
</div>

<!-- Filters Panel -->
<div id="filtersPanel" class="hidden section-card mb-6">
    <div class="section-header">
        <div class="section-icon bg-blue-100">
            <i data-lucide="filter" class="w-5 h-5 text-blue-600"></i>
        </div>
        <h3 class="font-bold text-navy">{% trans "Filters" %}</h3>
    </div>
    <div class="px-6 py-4 bg-slate-50">
        <form method="get" class="flex flex-wrap gap-4 items-end">
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "Status" %}</label>
                <select name="status" class="px-3 py-1.5 bg-white border border-slate-200 rounded-lg text-xs focus:outline-none focus:ring-2 focus:ring-navy">
                    <option value="">{% trans "All" %}</option>
                    <option value="sent" {% if filters.status == 'sent' %}selected{% endif %}>{% trans "Sent" %}</option>
                    <option value="completed" {% if filters.status == 'completed' %}selected{% endif %}>{% trans "Completed" %}</option>
                    <option value="pending" {% if filters.status == 'pending' %}selected{% endif %}>{% trans "Pending" %}</option>
                </select>
            </div>
            
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "Type" %}</label>
                <select name="survey_type" class="px-3 py-1.5 bg-white border border-slate-200 rounded-lg text-xs focus:outline-none focus:ring-2 focus:ring-navy">
                    <option value="">{% trans "All" %}</option>
                    <option value="stage" {% if filters.survey_type == 'stage' %}selected{% endif %}>{% trans "Journey Stage" %}</option>
                    <option value="complaint_resolution" {% if filters.survey_type == 'complaint_resolution' %}selected{% endif %}>{% trans "Complaint Resolution" %}</option>
                    <option value="general" {% if filters.survey_type == 'general' %}selected{% endif %}>{% trans "General" %}</option>
                    <option value="nps" {% if filters.survey_type == 'nps' %}selected{% endif %}>{% trans "NPS" %}</option>
                </select>
            </div>
            
            <div class="flex items-center gap-2">
                <button type="submit" class="px-4 py-1.5 bg-navy text-white rounded-lg text-xs font-bold hover:bg-blue transition">{% trans "Apply" %}</button>
                <a href="{% url 'surveys:instance_list' %}" class="px-4 py-1.5 border border-slate-200 rounded-lg text-xs font-semibold text-slate hover:bg-white transition">{% trans "Clear" %}</a>
            </div>
        </form>
    </div>
</div>

<!-- Surveys Table Section Card -->
<div class="section-card">
    <div class="section-header">
        <div class="section-icon bg-purple-100">
            <i data-lucide="table" class="w-5 h-5 text-purple-600"></i>
        </div>
        <h3 class="font-bold text-navy">{% trans "Survey List" %}</h3>
        <div class="ml-auto text-xs text-slate font-medium">
            {% trans "Showing" %} <span class="font-bold text-navy">{{ surveys.start_index|default:0 }}-{{ surveys.end_index|default:0 }}</span> {% trans "of" %} <span class="font-bold text-navy">{{ surveys.paginator.count|default:0 }}</span>
        </div>
    </div>
    
    <div class="overflow-x-auto">
        <table class="w-full">
            <thead class="bg-slate-50 border-b border-slate-100">
                <tr class="text-xs font-bold text-slate uppercase tracking-wider">
                    <th class="px-5 py-3 text-left">{% trans "Patient" %}</th>
                    <th class="px-5 py-3 text-left">{% trans "Survey Template" %}</th>
                    <th class="px-5 py-3 text-left">{% trans "Type" %}</th>
                    <th class="px-5 py-3 text-left">{% trans "Status" %}</th>
                    <th class="px-5 py-3 text-left">{% trans "Score" %}</th>
                    <th class="px-5 py-3 text-left">{% trans "Sent" %}</th>
                    <th class="px-5 py-3 text-left">{% trans "Completed" %}</th>
                    <th class="px-5 py-3 text-left">{% trans "Actions" %}</th>
                </tr>
            </thead>
            <tbody class="text-sm divide-y divide-slate-100">
                {% for survey in surveys.object_list %}
                <tr class="hover:bg-light/30 transition cursor-pointer" onclick="window.location.href='{% url 'surveys:instance_detail' survey.id %}'">
                    <td class="px-5 py-4">
                        <div class="font-semibold text-navy">{{ survey.patient.get_full_name }}</div>
                        <div class="text-xs text-slate">MRN: {{ survey.patient.mrn }}</div>
                    </td>
                    <td class="px-5 py-4">
                        <div class="font-medium text-slate-800">{{ survey.survey_template.name }}</div>
                        {% if survey.survey_template.hospital %}
                        <div class="text-xs text-slate">{{ survey.survey_template.hospital.name }}</div>
                        {% endif %}
                    </td>
                    <td class="px-5 py-4">
                        <span class="px-2.5 py-1 rounded-lg text-xs font-bold bg-light text-navy">{{ survey.survey_template.get_survey_type_display }}</span>
                    </td>
                    <td class="px-5 py-4">
                        {% if survey.status == 'completed' %}
                        <span class="px-2.5 py-1 rounded-lg text-xs font-bold bg-green-100 text-green-600">{{ survey.get_status_display }}</span>
                        {% elif survey.status == 'sent' %}
                        <span class="px-2.5 py-1 rounded-lg text-xs font-bold bg-yellow-100 text-yellow-600">{{ survey.get_status_display }}</span>
                        {% elif survey.status == 'active' %}
                        <span class="px-2.5 py-1 rounded-lg text-xs font-bold bg-blue-100 text-blue-600">{{ survey.get_status_display }}</span>
                        {% elif survey.status == 'cancelled' %}
                        <span class="px-2.5 py-1 rounded-lg text-xs font-bold bg-red-100 text-red-600">{{ survey.get_status_display }}</span>
                        {% else %}
                        <span class="px-2.5 py-1 rounded-lg text-xs font-bold bg-slate-100 text-slate-600">{{ survey.get_status_display }}</span>
                        {% endif %}
                    </td>
                    <td class="px-5 py-4">
                        {% if survey.total_score %}
                        <div class="flex items-center gap-2">
                            <span class="{% if survey.is_negative %}text-red-500{% else %}text-green-500{% endif %} font-bold">
                                {{ survey.total_score|floatformat:1 }}/5.0
                            </span>
                            {% if survey.is_negative %}
                            <i data-lucide="alert-circle" class="w-4 h-4 text-red-500" title="{% trans 'Negative survey' %}"></i>
                            {% endif %}
                        </div>
                        {% else %}
                        <span class="text-slate text-xs">-</span>
                        {% endif %}
                    </td>
                    <td class="px-5 py-4 text-slate text-xs">
                        {% if survey.sent_at %}
                        {{ survey.sent_at|date:"M d, Y H:i" }}
                        {% else %}
                        -
                        {% endif %}
                    </td>
                    <td class="px-5 py-4 text-slate text-xs">
                        {% if survey.completed_at %}
                        {{ survey.completed_at|date:"M d, Y H:i" }}
                        {% else %}
                        -
                        {% endif %}
                    </td>
                    <td class="px-5 py-4" onclick="event.stopPropagation();">
                        <a href="{% url 'surveys:instance_detail' survey.id %}" 
                           class="p-2 text-navy hover:bg-light rounded-lg transition inline-flex items-center gap-1"
                           title="{% trans 'View details' %}">
                            <i data-lucide="eye" class="w-4 h-4"></i>
                        </a>
                    </td>
                </tr>
                {% empty %}
                <tr>
                    <td colspan="8" class="text-center py-12">
                        <i data-lucide="inbox" class="w-12 h-12 mx-auto mb-3 text-slate-300"></i>
                        <p class="text-slate font-medium">{% trans "No surveys found" %}</p>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
    
    <!-- Pagination -->
    {% if surveys.has_other_pages %}
    <div class="bg-slate-50 px-6 py-4 flex items-center justify-between border-t border-slate-100">
        <div class="flex items-center gap-4">
            <span class="text-xs text-slate font-medium">
                {% trans "Showing" %} <span class="font-bold text-navy">{{ surveys.start_index }}-{{ surveys.end_index }}</span> {% trans "of" %} <span class="font-bold text-navy">{{ surveys.paginator.count }}</span> {% trans "entries" %}
            </span>
            <!-- Page Size Selector -->
            <form method="get" class="flex items-center gap-2" id="pageSizeForm">
                {% for key, value in request.GET.items %}
                    {% if key != 'page_size' and key != 'page' %}
                    <input type="hidden" name="{{ key }}" value="{{ value }}">
                    {% endif %}
                {% endfor %}
                <label class="text-xs text-slate">{% trans "Show" %}</label>
                <select name="page_size" onchange="document.getElementById('pageSizeForm').submit()" class="px-2 py-1 bg-white border border-slate-200 rounded-lg text-xs focus:outline-none focus:ring-2 focus:ring-navy">
                    <option value="10" {% if surveys.paginator.per_page == 10 %}selected{% endif %}>10</option>
                    <option value="25" {% if surveys.paginator.per_page == 25 %}selected{% endif %}>25</option>
                    <option value="50" {% if surveys.paginator.per_page == 50 %}selected{% endif %}>50</option>
                    <option value="100" {% if surveys.paginator.per_page == 100 %}selected{% endif %}>100</option>
                </select>
            </form>
        </div>
        <div class="flex gap-1">
            {% if surveys.has_previous %}
            <a href="?page={{ surveys.previous_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}" 
               class="w-8 h-8 flex items-center justify-center rounded-lg border border-slate-200 bg-white hover:bg-slate-50 transition">
                <i data-lucide="chevron-left" class="w-4 h-4 text-slate"></i>
            </a>
            {% else %}
            <span class="w-8 h-8 flex items-center justify-center rounded-lg border border-slate-200 bg-slate-100 text-slate-300 cursor-not-allowed">
                <i data-lucide="chevron-left" class="w-4 h-4"></i>
            </span>
            {% endif %}
            
            {% for num in surveys.paginator.page_range %}
                {% if num == surveys.number %}
                <span class="w-8 h-8 flex items-center justify-center rounded-lg bg-navy text-white text-xs font-bold">{{ num }}</span>
                {% elif num > surveys.number|add:'-3' and num < surveys.number|add:'3' %}
                <a href="?page={{ num }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}" 
                   class="w-8 h-8 flex items-center justify-center rounded-lg border border-slate-200 bg-white hover:bg-slate-50 text-xs font-bold text-slate transition">
                    {{ num }}
                </a>
                {% elif num == 1 or num == surveys.paginator.num_pages %}
                <a href="?page={{ num }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}" 
                   class="w-8 h-8 flex items-center justify-center rounded-lg border border-slate-200 bg-white hover:bg-slate-50 text-xs font-bold text-slate transition">
                    {{ num }}
                </a>
                {% elif num == surveys.number|add:'-3' or num == surveys.number|add:'3' %}
                <span class="w-8 h-8 flex items-center justify-center text-xs text-slate">...</span>
                {% endif %}
            {% endfor %}
            
            {% if surveys.has_next %}
            <a href="?page={{ surveys.next_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}" 
               class="w-8 h-8 flex items-center justify-center rounded-lg border border-slate-200 bg-white hover:bg-slate-50 transition">
                <i data-lucide="chevron-right" class="w-4 h-4 text-slate"></i>
            </a>
            {% else %}
            <span class="w-8 h-8 flex items-center justify-center rounded-lg border border-slate-200 bg-slate-100 text-slate-300 cursor-not-allowed">
                <i data-lucide="chevron-right" class="w-4 h-4"></i>
            </span>
            {% endif %}
        </div>
    </div>
    {% endif %}
</div>

<script>
function toggleFilters() {
    const filters = document.getElementById('filtersPanel');
    filters.classList.toggle('hidden');
}

// Search functionality
document.getElementById('searchInput')?.addEventListener('keypress', function(e) {
    if (e.key === 'Enter') {
        const value = this.value;
        const url = new URL(window.location.href);
        if (value) {
            url.searchParams.set('search', value);
        } else {
            url.searchParams.delete('search');
        }
        url.searchParams.delete('page');
        window.location.href = url.toString();
    }
});
</script>
{% endblock %}
