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

{% block title %}{% trans "My Complaints" %} - {{ source.get_localized_name }}{% 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;
}

@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="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="alert-triangle" class="w-6 h-6 text-white"></i>
                </div>
                <div>
                    <h1 class="text-2xl font-bold">
                        {% trans "My Complaints" %}
                    </h1>
                    <p class="text-white/80 text-sm">
                        {% trans "View all complaints from your source" %}
                        <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">{{ complaints_count }}</span>
                    </p>
                </div>
            </div>
            {% if source_user.can_create_complaints %}
            <a href="{% url 'px_sources:source_user_create_complaint' %}"
               class="inline-flex items-center gap-2 px-5 py-2.5 bg-white text-navy rounded-xl font-bold hover:bg-gray-100 transition shadow-lg">
                <i data-lucide="plus-circle" class="w-4 h-4"></i>
                {% trans "Create Complaint" %}
            </a>
            {% endif %}
        </div>
    </div>

    <!-- Filter Panel -->
    <div class="section-card mb-8 animate-in">
        <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>
            <h2 class="text-lg font-bold text-navy">{% trans "Filters" %}</h2>
        </div>
        <div class="p-6">
            <form method="get" class="grid grid-cols-1 md:grid-cols-12 gap-5">
                <!-- Search -->
                <div class="md:col-span-4">
                    <label class="block text-sm font-bold text-navy mb-2">{% trans "Search" %}</label>
                    <div class="relative">
                        <input type="text"
                               class="w-full pl-10 pr-4 py-2.5 border-2 border-blue-100 rounded-xl text-navy focus:ring-2 focus:ring-blue focus:border-transparent transition"
                               name="search"
                               placeholder="{% trans 'Title, patient name...' %}"
                               value="{{ search|default:'' }}">
                        <i data-lucide="search" class="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-slate"></i>
                    </div>
                </div>

                <!-- Status -->
                <div class="md:col-span-2">
                    <label class="block text-sm font-bold text-navy mb-2">{% trans "Status" %}</label>
                    <select class="w-full px-3 py-2.5 border-2 border-blue-100 rounded-xl text-navy focus:ring-2 focus:ring-blue focus:border-transparent transition bg-white">
                        <option value="">{% trans "All Statuses" %}</option>
                        <option value="open" {% if status_filter == 'open' %}selected{% endif %}>{% trans "Open" %}</option>
                        <option value="in_progress" {% if status_filter == 'in_progress' %}selected{% endif %}>{% trans "In Progress" %}</option>
                        <option value="resolved" {% if status_filter == 'resolved' %}selected{% endif %}>{% trans "Resolved" %}</option>
                        <option value="closed" {% if status_filter == 'closed' %}selected{% endif %}>{% trans "Closed" %}</option>
                    </select>
                </div>

                <!-- Priority -->
                <div class="md:col-span-2">
                    <label class="block text-sm font-bold text-navy mb-2">{% trans "Priority" %}</label>
                    <select class="w-full px-3 py-2.5 border-2 border-blue-100 rounded-xl text-navy focus:ring-2 focus:ring-blue focus:border-transparent transition bg-white">
                        <option value="">{% trans "All Priorities" %}</option>
                        <option value="low" {% if priority_filter == 'low' %}selected{% endif %}>{% trans "Low" %}</option>
                        <option value="medium" {% if priority_filter == 'medium' %}selected{% endif %}>{% trans "Medium" %}</option>
                        <option value="high" {% if priority_filter == 'high' %}selected{% endif %}>{% trans "High" %}</option>
                    </select>
                </div>

                <!-- Category -->
                <div class="md:col-span-2">
                    <label class="block text-sm font-bold text-navy mb-2">{% trans "Category" %}</label>
                    <select class="w-full px-3 py-2.5 border-2 border-blue-100 rounded-xl text-navy focus:ring-2 focus:ring-blue focus:border-transparent transition bg-white">
                        <option value="">{% trans "All Categories" %}</option>
                        <option value="clinical_care" {% if category_filter == 'clinical_care' %}selected{% endif %}>{% trans "Clinical Care" %}</option>
                        <option value="staff_behavior" {% if category_filter == 'staff_behavior' %}selected{% endif %}>{% trans "Staff Behavior" %}</option>
                        <option value="facility" {% if category_filter == 'facility' %}selected{% endif %}>{% trans "Facility" %}</option>
                        <option value="wait_time" {% if category_filter == 'wait_time' %}selected{% endif %}>{% trans "Wait Time" %}</option>
                        <option value="billing" {% if category_filter == 'billing' %}selected{% endif %}>{% trans "Billing" %}</option>
                        <option value="other" {% if category_filter == 'other' %}selected{% endif %}>{% trans "Other" %}</option>
                    </select>
                </div>

                <!-- Actions -->
                <div class="md:col-span-2 flex items-end">
                    <div class="flex gap-2 w-full">
                        <button type="submit"
                                class="inline-flex items-center justify-center gap-2 px-4 py-2.5 bg-navy text-white rounded-xl font-bold hover:bg-blue transition flex-grow shadow-lg">
                            <i data-lucide="search" class="w-4 h-4"></i>
                            {% trans "Filter" %}
                        </button>
                        <a href="{% url 'px_sources:source_user_complaint_list' %}"
                           class="inline-flex items-center justify-center p-2.5 border-2 border-slate-200 text-slate rounded-xl hover:bg-slate-50 transition"
                           title="{% trans 'Clear filters' %}">
                            <i data-lucide="x-circle" class="w-5 h-5"></i>
                        </a>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- Complaints Table -->
    <div class="section-card animate-in">
        <div class="section-header">
            <div class="section-icon bg-orange-100">
                <i data-lucide="file-text" class="w-5 h-5 text-orange-600"></i>
            </div>
            <h2 class="text-lg font-bold text-navy">
                {% trans "All 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 ml-2">{{ complaints_count }}</span>
            </h2>
        </div>
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead class="bg-blue-50">
                    <tr>
                        <th class="px-6 py-4 text-left text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Reference" %}</th>
                        <th class="text-left py-4 px-6 text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Title" %}</th>
                        <th class="text-left py-4 px-6 text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Patient" %}</th>
                        <th class="text-left py-4 px-6 text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Department" %}</th>
                        <th class="text-center py-4 px-6 text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Status" %}</th>
                        <th class="text-center py-4 px-6 text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Priority" %}</th>
                        <th class="text-left py-4 px-6 text-xs font-bold text-blue-800 uppercase tracking-wider">{% trans "Created" %}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-blue-50">
                    {% for complaint in complaints %}
                    <tr class="hover:bg-blue-50/50 transition">
                        <td class="px-6 py-4 text-sm font-mono text-slate font-medium">{{ complaint.reference_number|default:complaint.id|truncatechars:12 }}</td>
                        <td class="px-6 py-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="px-6 py-4 text-sm text-slate">{{ complaint.patient_name|default:"-" }}</td>
                        <td class="px-6 py-4 text-sm text-slate">{{ complaint.department|default:"-" }}</td>
                        <td class="px-6 py-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="px-6 py-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="px-6 py-4 text-sm text-slate">{{ complaint.created_at|date:"M d, Y" }}</td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="7" 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-blue-100 to-indigo-100 rounded-full flex items-center justify-center 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 found" %}</p>
                                {% if source_user.can_create_complaints %}
                                <a href="{% url 'complaints:complaint_create' %}" class="mt-3 text-blue font-semibold hover:underline">
                                    {% trans "Create your first complaint" %}
                                </a>
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <!-- Pagination -->
        {% if complaints.has_other_pages %}
        <div class="px-6 py-4 border-t border-slate-100">
            <div class="flex items-center justify-between">
                <p class="text-sm text-slate">
                    {% blocktrans with start=complaints.start_index end=complaints.end_index total=complaints.paginator.count %}
                    Showing {{ start }} to {{ end }} of {{ total }} complaints
                    {% endblocktrans %}
                </p>
                <div class="flex gap-2">
                    {% if complaints.has_previous %}
                    <a href="?page={{ complaints.previous_page_number }}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if priority_filter %}&priority={{ priority_filter }}{% endif %}{% if category_filter %}&category={{ category_filter }}{% endif %}{% if search %}&search={{ search }}{% endif %}"
                       class="px-4 py-2 border-2 border-blue-200 text-blue rounded-xl font-semibold hover:bg-blue-50 transition">
                        {% trans "Previous" %}
                    </a>
                    {% endif %}

                    <span class="px-4 py-2 bg-navy text-white rounded-xl font-semibold">
                        {% blocktrans with num=complaints.number total=complaints.paginator.num_pages %}Page {{ num }} of {{ total }}{% endblocktrans %}
                    </span>

                    {% if complaints.has_next %}
                    <a href="?page={{ complaints.next_page_number }}{% if status_filter %}&status={{ status_filter }}{% endif %}{% if priority_filter %}&priority={{ priority_filter }}{% endif %}{% if category_filter %}&category={{ category_filter }}{% endif %}{% if search %}&search={{ search }}{% endif %}"
                       class="px-4 py-2 border-2 border-blue-200 text-blue rounded-xl font-semibold hover:bg-blue-50 transition">
                        {% trans "Next" %}
                    </a>
                    {% endif %}
                </div>
            </div>
        </div>
        {% endif %}
    </div>
</div>

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