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

{% block title %}{% trans "Complaints" %} - {% trans "Call Center" %} - 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;
    }
    
    .badge-status-open { background: #e0f2fe; color: #075985; }
    .badge-status-in_progress { background: #fef9c3; color: #854d0e; }
    .badge-status-resolved { background: #dcfce7; color: #166534; }
    .badge-status-closed { background: #f3f4f6; color: #6b7280; }
    .badge-severity-critical { background: #fee2e2; color: #991b1b; }
    .badge-severity-high { background: #ffedd5; color: #9a3412; }
    .badge-severity-medium { background: #fef9c3; color: #854d0e; }
    .badge-severity-low { background: #dcfce7; color: #166534; }
</style>
{% endblock %}

{% block content %}
<!-- Page Header -->
<div class="page-header-gradient">
    <div class="flex items-center justify-between">
        <div class="flex items-center gap-3">
            <div class="section-icon bg-white/20">
                <i data-lucide="phone" class="w-6 h-6"></i>
            </div>
            <div>
                <h1 class="text-2xl font-bold mb-1">{% trans "Call Center Complaints" %}</h1>
                <p class="opacity-90 mb-0">{% trans "Complaints created via call center" %}</p>
            </div>
        </div>
        <a href="{% url 'callcenter:create_complaint' %}" class="bg-white text-blue-600 px-5 py-2.5 rounded-xl text-sm font-bold hover:bg-blue-50 flex items-center gap-2 transition">
            <i data-lucide="plus" class="w-4 h-4"></i> {% trans "Create Complaint" %}
        </a>
    </div>
</div>

<!-- Statistics -->
<div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-6">
    <div class="bg-blue-500 text-white p-5 rounded-2xl shadow-sm">
        <div class="flex justify-between items-center">
            <div>
                <p class="text-blue-100 text-xs font-bold uppercase tracking-wider mb-1">{% trans "Total Complaints" %}</p>
                <p class="text-2xl font-black">{{ stats.total }}</p>
            </div>
            <i data-lucide="file-text" class="w-8 h-8 opacity-50"></i>
        </div>
    </div>
    <div class="bg-cyan-500 text-white p-5 rounded-2xl shadow-sm">
        <div class="flex justify-between items-center">
            <div>
                <p class="text-cyan-100 text-xs font-bold uppercase tracking-wider mb-1">{% trans "Open" %}</p>
                <p class="text-2xl font-black">{{ stats.open }}</p>
            </div>
            <i data-lucide="inbox" class="w-8 h-8 opacity-50"></i>
        </div>
    </div>
    <div class="bg-amber-500 text-white p-5 rounded-2xl shadow-sm">
        <div class="flex justify-between items-center">
            <div>
                <p class="text-amber-100 text-xs font-bold uppercase tracking-wider mb-1">{% trans "In Progress" %}</p>
                <p class="text-2xl font-black">{{ stats.in_progress }}</p>
            </div>
            <i data-lucide="clock" class="w-8 h-8 opacity-50"></i>
        </div>
    </div>
    <div class="bg-emerald-500 text-white p-5 rounded-2xl shadow-sm">
        <div class="flex justify-between items-center">
            <div>
                <p class="text-emerald-100 text-xs font-bold uppercase tracking-wider mb-1">{% trans "Resolved" %}</p>
                <p class="text-2xl font-black">{{ stats.resolved }}</p>
            </div>
            <i data-lucide="check-circle" class="w-8 h-8 opacity-50"></i>
        </div>
    </div>
</div>

<!-- Filters -->
<div class="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="p-5">
        <form method="get" class="grid grid-cols-1 md:grid-cols-5 gap-4">
            <div class="md:col-span-1">
                <label class="block text-xs font-bold text-slate uppercase mb-2">{% trans "Search" %}</label>
                <input type="text" name="search" 
                       class="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy focus:border-transparent transition" 
                       placeholder="{% trans 'Search...' %}" value="{{ filters.search }}">
            </div>
            <div>
                <label class="block text-xs font-bold text-slate uppercase mb-2">{% trans "Status" %}</label>
                <select name="status" class="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy focus:border-transparent transition">
                    <option value="">{% trans "All" %}</option>
                    <option value="open" {% if filters.status == 'open' %}selected{% endif %}>{% trans "Open" %}</option>
                    <option value="in_progress" {% if filters.status == 'in_progress' %}selected{% endif %}>{% trans "In Progress" %}</option>
                    <option value="resolved" {% if filters.status == 'resolved' %}selected{% endif %}>{% trans "Resolved" %}</option>
                    <option value="closed" {% if filters.status == 'closed' %}selected{% endif %}>{% trans "Closed" %}</option>
                </select>
            </div>
            <div>
                <label class="block text-xs font-bold text-slate uppercase mb-2">{% trans "Severity" %}</label>
                <select name="severity" class="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy focus:border-transparent transition">
                    <option value="">{% trans "All" %}</option>
                    <option value="critical" {% if filters.severity == 'critical' %}selected{% endif %}>{% trans "Critical" %}</option>
                    <option value="high" {% if filters.severity == 'high' %}selected{% endif %}>{% trans "High" %}</option>
                    <option value="medium" {% if filters.severity == 'medium' %}selected{% endif %}>{% trans "Medium" %}</option>
                    <option value="low" {% if filters.severity == 'low' %}selected{% endif %}>{% trans "Low" %}</option>
                </select>
            </div>
            <div class="flex items-end">
                <button type="submit" class="w-full bg-navy text-white px-4 py-2.5 rounded-xl text-sm font-bold hover:bg-blue transition flex items-center justify-center gap-2">
                    <i data-lucide="filter" class="w-4 h-4"></i> {% trans "Filter" %}
                </button>
            </div>
        </form>
    </div>
</div>

<!-- Complaints Table -->
<div class="section-card">
    <div class="section-header">
        <div class="section-icon bg-red-100">
            <i data-lucide="message-square-warning" class="w-5 h-5 text-red-600"></i>
        </div>
        <h3 class="font-bold text-navy">{% trans "Complaints" %}</h3>
    </div>
    <div class="overflow-x-auto">
        <table class="w-full">
            <thead class="bg-gray-50">
                <tr>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "ID" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Title" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Patient" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Hospital" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Category" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Severity" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Status" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Created" %}</th>
                    <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Actions" %}</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-100">
                {% for complaint in complaints %}
                <tr class="hover:bg-gray-50 transition">
                    <td class="px-6 py-4">
                        <small class="text-gray-400">#{{ complaint.id|slice:":8" }}</small>
                    </td>
                    <td class="px-6 py-4">
                        <strong class="text-gray-800">{{ complaint.title|truncatewords:8 }}</strong>
                    </td>
                    <td class="px-6 py-4">
                        {% if complaint.patient %}
                        <span class="text-gray-800">{{ complaint.patient.get_full_name }}</span><br>
                        <small class="text-gray-400">MRN: {{ complaint.patient.mrn }}</small>
                        {% else %}
                        <span class="text-gray-400">{% trans "N/A" %}</span>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4 text-gray-600">{{ complaint.hospital.get_localized_name }}</td>
                    <td class="px-6 py-4">
                        <span class="inline-flex items-center px-2.5 py-1 rounded-lg text-xs font-bold bg-slate-100 text-slate">
                            {{ complaint.get_category_display }}
                        </span>
                    </td>
                    <td class="px-6 py-4">
                        <span class="inline-flex items-center px-2.5 py-1 rounded-lg text-xs font-bold badge-severity-{{ complaint.severity }}">
                            {{ complaint.get_severity_display }}
                        </span>
                    </td>
                    <td class="px-6 py-4">
                        <span class="inline-flex items-center px-2.5 py-1 rounded-lg text-xs font-bold badge-status-{{ complaint.status }}">
                            {{ complaint.get_status_display }}
                        </span>
                    </td>
                    <td class="px-6 py-4">
                        <small class="text-gray-500">{{ complaint.created_at|date:"Y-m-d H:i" }}</small>
                    </td>
                    <td class="px-6 py-4">
                        <a href="{% url 'complaints:complaint_detail' complaint.id %}" 
                           class="inline-flex items-center gap-1 px-3 py-2 text-navy bg-light rounded-lg hover:bg-light transition font-medium text-sm">
                            <i data-lucide="eye" class="w-4 h-4"></i>
                        </a>
                    </td>
                </tr>
                {% empty %}
                <tr>
                    <td colspan="9" class="text-center py-8 text-gray-500">
                        <i data-lucide="inbox" class="w-12 h-12 mx-auto mb-3 text-gray-300"></i>
                        <p>{% trans "No complaints found" %}</p>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <div class="px-6 py-4 border-t border-gray-100 flex justify-center">
        <div class="flex items-center gap-2">
            {% if page_obj.has_previous %}
            <a href="?page={{ page_obj.previous_page_number }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}" 
               class="px-3 py-2 rounded-lg border border-slate-200 text-slate hover:bg-light transition">
                {% trans "Previous" %}
            </a>
            {% endif %}
            
            <span class="px-4 py-2 rounded-lg bg-navy text-white text-sm font-bold">
                {% trans "Page" %} {{ page_obj.number }} {% trans "of" %} {{ page_obj.paginator.num_pages }}
            </span>
            
            {% if page_obj.has_next %}
            <a href="?page={{ page_obj.next_page_number }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}" 
               class="px-3 py-2 rounded-lg border border-slate-200 text-slate hover:bg-light transition">
                {% trans "Next" %}
            </a>
            {% endif %}
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}
