{% extends 'layouts/base.html' %}
{% load i18n %}

{% block title %}{% trans "Inquiries Console" %} - 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);
    }
    .stat-card {
        background: white;
        border-radius: 1rem;
        border: 2px solid #e2e8f0;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
        transition: all 0.3s ease;
    }
    .stat-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
        border-color: #005696;
    }
    .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;
    }
    .filter-btn.active { background-color: #005696; color: white; }
    .filter-btn:not(.active) { background-color: transparent; border: 1px solid #e2e8f0; color: #64748b; }
    .filter-btn:not(.active):hover { background-color: #f8fafc; }
</style>
{% endblock %}

{% block content %}
<div class="page-header-gradient">
    <div class="flex justify-between items-center">
        <div>
            <h1 class="text-2xl font-bold mb-1">{% trans "Inquiries Console" %}</h1>
            <p class="text-blue-100 text-sm">{% trans "Manage patient inquiries and requests" %}</p>
        </div>
        {% if user.is_px_admin or user.is_hospital_admin or user.is_px_employee %}
        <a href="{% url 'inquiries:inquiry_create' %}"
           class="inline-flex items-center px-4 py-2.5 bg-white text-navy font-medium rounded-xl hover:bg-blue-50 transition">
            <i data-lucide="plus" class="w-4 h-4 me-2"></i>{% trans "New Inquiry" %}
        </a>
        {% endif %}
    </div>
</div>

<div class="grid grid-cols-4 gap-6 mb-6">
    <div class="stat-card bg-white rounded-2xl border-2 border-slate-200 p-6 flex items-center gap-4">
        <div class="flex-shrink-0">
            <div class="w-14 h-14 bg-navy/10 rounded-xl flex items-center justify-center">
                <i data-lucide="inbox" class="w-7 h-7 text-navy"></i>
            </div>
        </div>
        <div>
            <h6 class="text-slate-500 text-sm font-medium mb-1">{% trans "Total Inquiries" %}</h6>
            <h2 class="text-3xl font-bold text-gray-800">{{ stats.total }}</h2>
        </div>
    </div>
    <div class="stat-card bg-white rounded-2xl border-2 border-slate-200 p-6 flex items-center gap-4">
        <div class="flex-shrink-0">
            <div class="w-14 h-14 bg-green-500/10 rounded-xl flex items-center justify-center">
                <i data-lucide="check-circle" class="w-7 h-7 text-green-600"></i>
            </div>
        </div>
        <div>
            <h6 class="text-slate-500 text-sm font-medium mb-1">{% trans "Resolved" %}</h6>
            <h2 class="text-3xl font-bold text-gray-800">{{ stats.resolved }} <span class="text-sm text-green-600">({{ stats.resolved_percentage|floatformat:1 }}%)</span></h2>
        </div>
    </div>
    <div class="stat-card bg-white rounded-2xl border-2 border-slate-200 p-6 flex items-center gap-4">
        <div class="flex-shrink-0">
            <div class="w-14 h-14 bg-orange-500/10 rounded-xl flex items-center justify-center">
                <i data-lucide="clock" class="w-7 h-7 text-orange-600"></i>
            </div>
        </div>
        <div>
            <h6 class="text-slate-500 text-sm font-medium mb-1">{% trans "In Progress" %}</h6>
            <h2 class="text-3xl font-bold text-gray-800">{{ stats.in_progress }}</h2>
        </div>
    </div>
    <div class="stat-card bg-white rounded-2xl border-2 border-slate-200 p-6 flex items-center gap-4">
        <div class="flex-shrink-0">
            <div class="w-14 h-14 bg-red-500/10 rounded-xl flex items-center justify-center">
                <i data-lucide="alert-triangle" class="w-7 h-7 text-red-500"></i>
            </div>
        </div>
        <div>
            <h6 class="text-slate-500 text-sm font-medium mb-1">{% trans "Overdue" %}</h6>
            <h2 class="text-3xl font-bold text-gray-800">{{ stats.overdue }}</h2>
        </div>
    </div>
</div>

<div class="section-card mb-6">
    <div class="section-header">
        <div class="section-icon bg-slate-500/10">
            <i data-lucide="sliders-horizontal" class="w-5 h-5 text-slate-600"></i>
        </div>
        <h5 class="text-lg font-semibold text-gray-800">{% trans "Filters" %}</h5>
    </div>
    <div class="p-6">
        <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 "Search" %}</label>
                <input type="text" name="search" value="{{ filters.search }}"
                       placeholder="{% trans 'Subject, contact name...' %}"
                       class="px-3 py-1.5 bg-white border border-slate-200 rounded-lg text-xs">
            </div>
            <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">
                    <option value="">{% trans "All Status" %}</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 class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "Category" %}</label>
                <select name="category" class="px-3 py-1.5 bg-white border border-slate-200 rounded-lg text-xs">
                    <option value="">{% trans "All Categories" %}</option>
                    <option value="general" {% if filters.category == 'general' %}selected{% endif %}>{% trans "General" %}</option>
                    <option value="appointment" {% if filters.category == 'appointment' %}selected{% endif %}>{% trans "Appointments" %}</option>
                    <option value="billing" {% if filters.category == 'billing' %}selected{% endif %}>{% trans "Billing" %}</option>
                    <option value="medical_records" {% if filters.category == 'medical_records' %}selected{% endif %}>{% trans "Medical Records" %}</option>
                    <option value="pharmacy" {% if filters.category == 'pharmacy' %}selected{% endif %}>{% trans "Pharmacy" %}</option>
                    <option value="insurance" {% if filters.category == 'insurance' %}selected{% endif %}>{% trans "Insurance" %}</option>
                    <option value="feedback" {% if filters.category == 'feedback' %}selected{% endif %}>{% trans "Feedback" %}</option>
                    <option value="other" {% if filters.category == 'other' %}selected{% endif %}>{% trans "Other" %}</option>
                </select>
            </div>
            <button type="submit" class="px-4 py-1.5 bg-navy text-white rounded-lg text-xs font-bold">
                <i data-lucide="search" class="w-3 h-3 inline-block me-1"></i>{% trans "Filter" %}
            </button>
        </form>
    </div>
</div>

<div class="section-card">
    <div class="section-header flex items-center justify-between">
        <div class="flex items-center gap-3">
            <div class="section-icon bg-cyan-500/10">
                <i data-lucide="help-circle" class="w-5 h-5 text-cyan-600"></i>
            </div>
            <h5 class="text-lg font-semibold text-gray-800">{% trans "All Inquiries" %}</h5>
        </div>
        <span class="text-xs font-bold text-slate uppercase">{% trans "Total:" %} <span class="text-navy">{{ inquiries.paginator.count }}</span></span>
    </div>
    <p class="px-6 py-2 text-xs font-bold text-slate uppercase bg-slate-50 border-b-2 border-slate-200">
        {% trans "Showing:" %} <span class="text-navy">{{ inquiries.start_index|default:0 }}-{{ inquiries.end_index|default:0 }} {% trans "of" %} {{ inquiries.paginator.count|default:0 }}</span>
    </p>
    <div class="p-0">
        {% if inquiries %}
        <table class="w-full text-left border-collapse">
            <thead class="bg-slate-50 border-b uppercase text-[10px] font-bold text-slate tracking-wider">
                <tr>
                    <th class="px-6 py-4">{% trans "Reference" %}</th>
                    <th class="px-6 py-4">{% trans "Subject" %}</th>
                    <th class="px-6 py-4">{% trans "Contact" %}</th>
                    <th class="px-6 py-4">{% trans "Category" %}</th>
                    <th class="px-6 py-4 text-center">{% trans "Status" %}</th>
                    <th class="px-6 py-4 text-center">{% trans "Priority" %}</th>
                    <th class="px-6 py-4">{% trans "Created" %}</th>
                    <th class="px-6 py-4">{% trans "Assigned" %}</th>
                </tr>
            </thead>
            <tbody class="text-sm divide-y divide-slate-100">
                {% for inquiry in inquiries %}
                <tr class="hover:bg-slate-50/50 transition-colors cursor-pointer group" onclick="window.location='{% url 'inquiries:inquiry_detail' inquiry.pk %}'">
                    <td class="px-6 py-4"><code class="bg-slate-100 px-2 py-0.5 rounded text-xs">{{ inquiry.reference_number|truncatechars:15 }}</code></td>
                    <td class="px-6 py-4">
                        <a href="{% url 'inquiries:inquiry_detail' inquiry.pk %}" class="font-semibold text-navy hover:text-blue transition" onclick="event.stopPropagation()">
                            {{ inquiry.subject|truncatechars:40 }}
                        </a>
                    </td>
                    <td class="px-6 py-4">
                        <div class="font-semibold text-slate-800">{{ inquiry.contact_name|default:"-" }}</div>
                        <div class="text-[10px] text-slate font-medium">{{ inquiry.contact_phone|default:inquiry.contact_email|default:"-" }}</div>
                    </td>
                    <td class="px-6 py-4 text-slate-600">{{ inquiry.get_category_display|default:"-" }}</td>
                    <td class="px-6 py-4 text-center">
                        <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase
                            {% if inquiry.status == 'open' %}bg-yellow-100 text-yellow-700
                            {% elif inquiry.status == 'in_progress' %}bg-blue-100 text-blue-700
                            {% elif inquiry.status == 'resolved' %}bg-green-100 text-green-700
                            {% elif inquiry.status == 'closed' %}bg-slate-100 text-slate-600
                            {% else %}bg-slate-100 text-slate-600{% endif %}">
                            {{ inquiry.get_status_display }}
                        </span>
                    </td>
                    <td class="px-6 py-4 text-center">
                        <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase
                            {% if inquiry.priority == 'critical' %}bg-red-500 text-white
                            {% elif inquiry.priority == 'high' %}bg-orange-100 text-orange-700
                            {% elif inquiry.priority == 'medium' %}bg-yellow-100 text-yellow-700
                            {% elif inquiry.priority == 'low' %}bg-green-100 text-green-700
                            {% else %}bg-slate-100 text-slate-600{% endif %}">
                            {{ inquiry.get_priority_display }}
                        </span>
                    </td>
                    <td class="px-6 py-4 text-slate-500">{{ inquiry.created_at|date:"M d, Y" }}</td>
                    <td class="px-6 py-4">
                        {% if inquiry.assigned_to %}
                        <span class="text-slate-600">{{ inquiry.assigned_to.get_full_name }}</span>
                        {% else %}
                        <span class="text-slate-400 italic">{% trans "Unassigned" %}</span>
                        {% endif %}
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>

        {% include "partials/pagination.html" %}
        {% else %}
        <div class="px-6 py-12 text-center">
            <i data-lucide="help-circle" class="w-12 h-12 mx-auto text-slate-300 mb-3"></i>
            <p class="text-slate">{% trans "No inquiries found" %}</p>
            <p class="text-slate text-sm mt-1">{% trans "Adjust your filters or create a new inquiry" %}</p>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}
