{% extends "layouts/source_user_base.html" %}
{% load i18n %}

{% block title %}{% trans "Communication Requests" %} - PX360{% endblock %}

{% block extra_css %}
<style>
.page-header-gradient {
    background: linear-gradient(135deg, #0d9488 0%, #14b8a6 50%, #2dd4bf 100%);
    color: white; padding: 1.5rem 2rem; border-radius: 1rem; margin-bottom: 1.5rem;
    box-shadow: 0 10px 15px -3px rgba(13, 148, 136, 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;
}
.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;
}
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}
.animate-in { animation: fadeIn 0.5s ease-out forwards; }
</style>
{% endblock %}

{% block content %}
<div class="p-6 md:p-8 bg-gradient-to-br from-light to-blue-50 min-h-screen">
    <div class="page-header-gradient animate-in">
        <div class="flex flex-col md:flex-row justify-between items-start md:items-center gap-4">
            <div class="flex items-center gap-3">
                <div class="section-icon bg-white/20">
                    <i data-lucide="message-square" class="w-6 h-6 text-white"></i>
                </div>
                <div>
                    <h1 class="text-2xl font-bold">{% trans "Communication Requests" %}</h1>
                    <p class="text-white/80 text-sm">
                        {% trans "Your requests to the PX team" %}
                        <span class="inline-flex items-center px-2.5 py-0.5 bg-white/20 text-white rounded-full text-xs font-bold ml-2">{{ total_count }}</span>
                    </p>
                </div>
            </div>
            <a href="{% url 'px_sources:source_user_create_communication_request' %}"
               class="inline-flex items-center gap-2 px-5 py-2.5 bg-white text-teal-700 rounded-xl font-bold hover:bg-gray-100 transition shadow-lg">
                <i data-lucide="plus-circle" class="w-4 h-4"></i>
                {% trans "New Request" %}
            </a>
        </div>
    </div>

    {% if pending_count > 0 %}
    <div class="bg-amber-50 border border-amber-200 rounded-xl p-4 mb-6 flex items-center gap-3">
        <i data-lucide="clock" class="w-5 h-5 text-amber-600"></i>
        <span class="text-amber-800 font-medium text-sm">{% trans "Pending requests:" %} {{ pending_count }}</span>
    </div>
    {% endif %}

    <div class="section-card animate-in">
        <div class="section-header">
            <div class="section-icon bg-teal-100">
                <i data-lucide="message-square" class="w-5 h-5 text-teal-600"></i>
            </div>
            <h2 class="text-lg font-bold text-navy">
                {% trans "All Requests" %}
                <span class="inline-flex items-center px-2.5 py-0.5 bg-teal-100 text-teal-700 rounded-full text-xs font-bold ml-2">{{ total_count }}</span>
            </h2>
        </div>
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead class="bg-teal-50">
                    <tr>
                        <th class="px-6 py-4 text-left text-xs font-bold text-teal-800 uppercase tracking-wider">{% trans "Reason" %}</th>
                        <th class="text-left py-4 px-6 text-xs font-bold text-teal-800 uppercase tracking-wider">{% trans "Patient" %}</th>
                        <th class="text-left py-4 px-6 text-xs font-bold text-teal-800 uppercase tracking-wider">{% trans "Message" %}</th>
                        <th class="text-center py-4 px-6 text-xs font-bold text-teal-800 uppercase tracking-wider">{% trans "Status" %}</th>
                        <th class="text-left py-4 px-6 text-xs font-bold text-teal-800 uppercase tracking-wider">{% trans "Created" %}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-teal-50">
                    {% for req in comm_requests %}
                    <tr class="hover:bg-teal-50/50 transition">
                        <td class="px-6 py-4">
                            <span class="text-navy font-semibold text-sm">{{ req.get_reason_display }}</span>
                        </td>
                        <td class="px-6 py-4 text-sm text-slate">
                            {% if req.patient_name %}
                            {{ req.patient_name }}
                            {% if req.patient_mrn %}<br><span class="text-xs text-slate-400">MRN: {{ req.patient_mrn }}</span>{% endif %}
                            {% else %}-{% endif %}
                        </td>
                        <td class="px-6 py-4 text-sm text-slate">{{ req.message|truncatechars:60 }}</td>
                        <td class="px-6 py-4 text-center">
                            {% if req.status == 'pending' %}
                                <span class="inline-flex items-center gap-1 px-3 py-1 rounded-full text-xs font-bold bg-yellow-100 text-yellow-700">
                                    <i data-lucide="clock" class="w-2 h-2"></i> {% trans "Pending" %}
                                </span>
                            {% elif req.status == 'contacted' %}
                                <span class="inline-flex items-center gap-1 px-3 py-1 rounded-full text-xs font-bold bg-blue-100 text-blue-700">
                                    <i data-lucide="phone" class="w-2 h-2"></i> {% trans "Contacted" %}
                                </span>
                            {% elif req.status == 'resolved' %}
                                <span class="inline-flex items-center gap-1 px-3 py-1 rounded-full text-xs font-bold bg-emerald-100 text-emerald-700">
                                    <i data-lucide="check-circle" class="w-2 h-2"></i> {% trans "Resolved" %}
                                </span>
                            {% else %}
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold bg-slate-100 text-slate-700">{{ req.get_status_display }}</span>
                            {% endif %}
                        </td>
                        <td class="px-6 py-4 text-sm text-slate">{{ req.created_at|date:"M d, Y H:i" }}</td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="5" class="px-6 py-12 text-center">
                            <div class="flex flex-col items-center">
                                <div class="w-16 h-16 bg-gradient-to-br from-teal-100 to-cyan-100 rounded-full flex items-center justify-center mb-4">
                                    <i data-lucide="message-square" class="w-8 h-8 text-teal-500"></i>
                                </div>
                                <p class="text-slate font-medium">{% trans "No communication requests yet" %}</p>
                                <a href="{% url 'px_sources:source_user_create_communication_request' %}" class="mt-3 text-teal-600 font-semibold hover:underline">
                                    {% trans "Create your first request" %}
                                </a>
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    lucide.createIcons();
});
</script>
{% endblock %}
