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

{% block title %}{% trans "Source User Dashboard" %} - {{ source.get_localized_name }} - PX360{% endblock %}

{% block extra_css %}
<style>
    @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">
    <!-- Page Header -->
    <div class="flex flex-wrap justify-between items-center gap-4 mb-8 animate-in">
        <div class="flex items-center gap-3">
            <div class="flex items-center justify-center w-14 h-14 bg-gradient-to-br from-blue to-navy rounded-2xl shadow-lg shadow-blue-200">
                <i data-lucide="radio" class="w-8 h-8 text-white"></i>
            </div>
            <div>
                <h1 class="text-2xl font-bold text-navy">
                    {{ source.get_localized_name }}
                </h1>
                <p class="text-slate text-sm">
                    {% trans "Welcome" %}, {{ request.user.get_full_name|default:request.user.email }}!
                </p>
            </div>
        </div>
        <div class="flex gap-3">
            <a href="{% url 'px_sources:source_user_complaint_list' %}"
               class="inline-flex items-center gap-2 px-5 py-2.5 bg-gradient-to-r from-navy to-blue text-white rounded-xl font-semibold hover:from-navy hover:to-blue transition shadow-lg shadow-blue-200">
                <i data-lucide="file-text" class="w-4 h-4"></i>
                {% trans "All Complaints" %}
            </a>
            <a href="{% url 'px_sources:source_user_inquiry_list' %}"
               class="inline-flex items-center gap-2 px-5 py-2.5 bg-white border-2 border-blue-200 text-navy rounded-xl font-semibold hover:bg-blue-50 transition">
                <i data-lucide="help-circle" class="w-4 h-4"></i>
                {% trans "All Inquiries" %}
            </a>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
        <div class="stat-card bg-navy rounded-2xl p-6 text-white shadow-xl shadow-blue-200">
            <div class="flex items-center justify-between mb-3">
                <div>
                    <p class="text-white/80 text-sm font-medium">{% trans "Total Complaints" %}</p>
                    <p class="text-4xl font-bold mt-2">{{ total_complaints }}</p>
                </div>
                <div class="w-14 h-14 bg-white/20 rounded-xl flex items-center justify-center">
                    <i data-lucide="file-text" class="w-7 h-7 text-white"></i>
                </div>
            </div>
            <div class="text-xs text-white/70">{% trans "From your source" %}</div>
        </div>

        <div class="stat-card bg-orange-500 rounded-2xl p-6 text-white shadow-xl shadow-orange-200">
            <div class="flex items-center justify-between mb-3">
                <div>
                    <p class="text-white/80 text-sm font-medium">{% trans "Open Complaints" %}</p>
                    <p class="text-4xl font-bold mt-2">{{ open_complaints }}</p>
                </div>
                <div class="w-14 h-14 bg-white/20 rounded-xl flex items-center justify-center">
                    <i data-lucide="alert-circle" class="w-7 h-7 text-white"></i>
                </div>
            </div>
            <div class="text-xs text-white/70">{% trans "Needs attention" %}</div>
        </div>

        <div class="stat-card bg-cyan-500 rounded-2xl p-6 text-white shadow-xl shadow-cyan-200">
            <div class="flex items-center justify-between mb-3">
                <div>
                    <p class="text-white/80 text-sm font-medium">{% trans "Total Inquiries" %}</p>
                    <p class="text-4xl font-bold mt-2">{{ total_inquiries }}</p>
                </div>
                <div class="w-14 h-14 bg-white/20 rounded-xl flex items-center justify-center">
                    <i data-lucide="help-circle" class="w-7 h-7 text-white"></i>
                </div>
            </div>
            <div class="text-xs text-white/70">{% trans "From your source" %}</div>
        </div>

        <div class="stat-card bg-indigo-500 rounded-2xl p-6 text-white shadow-xl shadow-indigo-200">
            <div class="flex items-center justify-between mb-3">
                <div>
                    <p class="text-white/80 text-sm font-medium">{% trans "Open Inquiries" %}</p>
                    <p class="text-4xl font-bold mt-2">{{ open_inquiries }}</p>
                </div>
                <div class="w-14 h-14 bg-white/20 rounded-xl flex items-center justify-center">
                    <i data-lucide="message-circle" class="w-7 h-7 text-white"></i>
                </div>
            </div>
            <div class="text-xs text-white/70">{% trans "Active inquiries" %}</div>
        </div>
    </div>

    <!-- Quick Actions -->
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
        {% if can_create_complaints %}
        <a href="{% url 'px_sources:source_user_create_complaint' %}" class="group">
            <div class="bg-white p-6 rounded-2xl shadow-sm border-2 border-blue-100 hover:border-blue hover:shadow-lg hover:-translate-y-1 transition-all duration-200">
                <div class="flex items-center gap-4">
                    <div class="w-14 h-14 bg-gradient-to-br from-orange-400 to-orange-500 rounded-xl flex items-center justify-center group-hover:shadow-lg transition">
                        <i data-lucide="plus-circle" class="w-7 h-7 text-white"></i>
                    </div>
                    <div>
                        <h3 class="font-bold text-navy text-lg">{% trans "New Complaint" %}</h3>
                        <p class="text-sm text-slate">{% trans "File a new complaint" %}</p>
                    </div>
                </div>
            </div>
        </a>
        {% endif %}

        {% if can_create_inquiries %}
        <a href="{% url 'px_sources:source_user_create_inquiry' %}" class="group">
            <div class="bg-white p-6 rounded-2xl shadow-sm border-2 border-blue-100 hover:border-blue hover:shadow-lg hover:-translate-y-1 transition-all duration-200">
                <div class="flex items-center gap-4">
                    <div class="w-14 h-14 bg-gradient-to-br from-cyan-500 to-blue-500 rounded-xl flex items-center justify-center group-hover:shadow-lg transition">
                        <i data-lucide="plus-circle" class="w-7 h-7 text-white"></i>
                    </div>
                    <div>
                        <h3 class="font-bold text-navy text-lg">{% trans "New Inquiry" %}</h3>
                        <p class="text-sm text-slate">{% trans "Submit a new inquiry" %}</p>
                    </div>
                </div>
            </div>
        </a>
        {% endif %}

        {% if source_user.can_create_observations %}
        <a href="{% url 'px_sources:source_user_create_observation' %}" class="group">
            <div class="bg-white p-6 rounded-2xl shadow-sm border-2 border-blue-100 hover:border-blue hover:shadow-lg hover:-translate-y-1 transition-all duration-200">
                <div class="flex items-center gap-4">
                    <div class="w-14 h-14 bg-gradient-to-br from-purple-500 to-purple-600 rounded-xl flex items-center justify-center group-hover:shadow-lg transition">
                        <i data-lucide="eye" class="w-7 h-7 text-white"></i>
                    </div>
                    <div>
                        <h3 class="font-bold text-navy text-lg">{% trans "Observation" %}</h3>
                        <p class="text-sm text-slate">{% trans "Report an observation" %}</p>
                    </div>
                </div>
            </div>
        </a>
        {% endif %}

        {% if source_user.can_create_suggestions %}
        <a href="{% url 'px_sources:source_user_create_suggestion' %}" class="group">
            <div class="bg-white p-6 rounded-2xl shadow-sm border-2 border-blue-100 hover:border-blue hover:shadow-lg hover:-translate-y-1 transition-all duration-200">
                <div class="flex items-center gap-4">
                    <div class="w-14 h-14 bg-gradient-to-br from-teal-500 to-cyan-600 rounded-xl flex items-center justify-center group-hover:shadow-lg transition">
                        <i data-lucide="lightbulb" class="w-7 h-7 text-white"></i>
                    </div>
                    <div>
                        <h3 class="font-bold text-navy text-lg">{% trans "Suggestion" %}</h3>
                        <p class="text-sm text-slate">{% trans "Share an improvement idea" %}</p>
                    </div>
                </div>
            </div>
        </a>
        {% endif %}
    </div>

    <!-- Recent Complaints -->
    <div class="bg-white rounded-2xl shadow-sm border border-blue-100 overflow-hidden mb-8">
        <div class="px-6 py-5 border-b border-blue-100 bg-gradient-to-r from-blue-50 to-transparent flex justify-between items-center">
            <h2 class="text-lg font-bold text-navy flex items-center gap-2">
                <i data-lucide="file-text" class="w-5 h-5 text-blue"></i>
                {% trans "Recent Complaints" %}
                <span class="inline-flex items-center px-2.5 py-0.5 bg-blue-100 text-blue-700 rounded-full text-xs font-bold">
                    {{ complaints|length }}
                </span>
            </h2>
            <a href="{% url 'px_sources:source_user_complaint_list' %}" class="text-blue font-semibold hover:text-navy flex items-center gap-1 text-sm">
                {% trans "View All" %}
                <i data-lucide="arrow-right" class="w-4 h-4"></i>
            </a>
        </div>
        <div class="p-6">
            {% if complaints %}
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead>
                        <tr class="border-b-2 border-blue-100">
                            <th class="text-left py-3 px-4 text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Reference" %}</th>
                            <th class="text-left py-3 px-4 text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Title" %}</th>
                            <th class="text-left py-3 px-4 text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Patient" %}</th>
                            <th class="text-center py-3 px-4 text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Status" %}</th>
                            <th class="text-center py-3 px-4 text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Priority" %}</th>
                            <th class="text-left py-3 px-4 text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Date" %}</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-blue-50">
                        {% for complaint in complaints|slice:":5" %}
                        <tr class="hover:bg-blue-50/50 transition">
                            <td class="py-3 px-4 text-sm font-mono text-slate font-medium">{{ complaint.reference_number|default:complaint.id|truncatechars:12 }}</td>
                            <td class="py-3 px-4">
                                <a href="{% url 'complaints:complaint_detail' complaint.pk %}"
                                   class="text-navy font-semibold hover:text-blue transition">
                                    {{ complaint.title|truncatechars:35 }}
                                </a>
                            </td>
                            <td class="py-3 px-4 text-sm text-slate">{{ complaint.patient_name|default:"-" }}</td>
                            <td class="py-3 px-4 text-center">
                                {% if complaint.status == 'open' %}
                                    <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="circle" class="w-2 h-2 fill-current"></i>
                                        {% trans "Open" %}
                                    </span>
                                {% elif complaint.status == 'in_progress' %}
                                    <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="loader" class="w-2 h-2"></i>
                                        {% trans "In Progress" %}
                                    </span>
                                {% elif complaint.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">{{ complaint.get_status_display }}</span>
                                {% endif %}
                            </td>
                            <td class="py-3 px-4 text-center">
                                {% if complaint.priority == 'high' %}
                                    <span class="inline-flex items-center gap-1 px-3 py-1 rounded-full text-xs font-bold bg-red-100 text-red-700">
                                        <i data-lucide="arrow-up" class="w-2 h-2"></i>
                                        {% trans "High" %}
                                    </span>
                                {% elif complaint.priority == 'medium' %}
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold bg-orange-100 text-orange-700">
                                        {% trans "Medium" %}
                                    </span>
                                {% else %}
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold bg-green-100 text-green-700">
                                        {% trans "Low" %}
                                    </span>
                                {% endif %}
                            </td>
                            <td class="py-3 px-4 text-sm text-slate">{{ complaint.created_at|date:"M d, Y" }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% else %}
            <div class="text-center py-12">
                <div class="w-16 h-16 bg-gradient-to-br from-blue-100 to-indigo-100 rounded-full flex items-center justify-center mx-auto mb-4">
                    <i data-lucide="file-text" class="w-8 h-8 text-blue-500"></i>
                </div>
                <p class="text-slate font-medium">{% trans "No complaints from this source yet." %}</p>
            </div>
            {% endif %}
        </div>
    </div>

    <!-- Recent Inquiries -->
    <div class="bg-white rounded-2xl shadow-sm border border-blue-100 overflow-hidden">
        <div class="px-6 py-5 border-b border-blue-100 bg-gradient-to-r from-blue-50 to-transparent flex justify-between items-center">
            <h2 class="text-lg font-bold text-navy flex items-center gap-2">
                <i data-lucide="help-circle" class="w-5 h-5 text-blue"></i>
                {% trans "Recent Inquiries" %}
                <span class="inline-flex items-center px-2.5 py-0.5 bg-blue-100 text-blue-700 rounded-full text-xs font-bold">
                    {{ inquiries|length }}
                </span>
            </h2>
            <a href="{% url 'px_sources:source_user_inquiry_list' %}" class="text-blue font-semibold hover:text-navy flex items-center gap-1 text-sm">
                {% trans "View All" %}
                <i data-lucide="arrow-right" class="w-4 h-4"></i>
            </a>
        </div>
        <div class="p-6">
            {% if inquiries %}
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead>
                        <tr class="border-b-2 border-blue-100">
                            <th class="text-left py-3 px-4 text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Reference" %}</th>
                            <th class="text-left py-3 px-4 text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Subject" %}</th>
                            <th class="text-left py-3 px-4 text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "From" %}</th>
                            <th class="text-center py-3 px-4 text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Status" %}</th>
                            <th class="text-left py-3 px-4 text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Date" %}</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-blue-50">
                        {% for inquiry in inquiries|slice:":5" %}
                        <tr class="hover:bg-blue-50/50 transition">
                            <td class="py-3 px-4 text-sm font-mono text-slate font-medium">{{ inquiry.reference_number|default:inquiry.id|truncatechars:12 }}</td>
                            <td class="py-3 px-4">
                                <span class="text-navy font-medium">{{ inquiry.subject|truncatechars:35 }}</span>
                            </td>
                            <td class="py-3 px-4 text-sm text-slate">{{ inquiry.contact_name|default:"-" }}</td>
                            <td class="py-3 px-4 text-center">
                                {% if inquiry.status == 'open' %}
                                    <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="circle" class="w-2 h-2 fill-current"></i>
                                        {% trans "Open" %}
                                    </span>
                                {% elif inquiry.status == 'in_progress' %}
                                    <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="loader" class="w-2 h-2"></i>
                                        {% trans "In Progress" %}
                                    </span>
                                {% elif inquiry.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">{{ inquiry.get_status_display }}</span>
                                {% endif %}
                            </td>
                            <td class="py-3 px-4 text-sm text-slate">{{ inquiry.created_at|date:"M d, Y" }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% else %}
            <div class="text-center py-12">
                <div class="w-16 h-16 bg-gradient-to-br from-cyan-100 to-blue-100 rounded-full flex items-center justify-center mx-auto mb-4">
                    <i data-lucide="help-circle" class="w-8 h-8 text-cyan-500"></i>
                </div>
                <p class="text-slate font-medium">{% trans "No inquiries from this source yet." %}</p>
            </div>
            {% endif %}
        </div>
    </div>
</div>

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