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

{% block title %}{% trans "Physician Ratings" %} - 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;
    }
    .rating-row {
        transition: all 0.2s ease;
        border-left: 3px solid transparent;
    }
    .rating-row:hover {
        background: #f8fafc;
        border-left-color: #007bbd;
        transform: translateX(2px);
    }
    .field-label {
        font-size: 10px;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        color: #64748b;
    }
    .badge {
        display: inline-flex;
        align-items: center;
        padding: 0.25rem 0.75rem;
        border-radius: 9999px;
        font-size: 0.75rem;
        font-weight: 600;
    }
    .badge-success {
        background-color: #dcfce7;
        color: #166534;
    }
    .badge-warning {
        background-color: #fef3c7;
        color: #92400e;
    }
    .badge-danger {
        background-color: #fee2e2;
        color: #991b1b;
    }
    .badge-light {
        background-color: #f1f5f9;
        color: #475569;
    }
</style>
{% endblock %}

{% block content %}
<div class="p-8">
    <!-- 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="star" class="w-5 h-5 text-white"></i>
                </div>
                <div>
                    <h1 class="text-2xl font-bold">{% trans "Physician Ratings" %}</h1>
                    <p class="text-white/80 text-sm">{% trans "Monthly physician performance ratings" %}</p>
                </div>
            </div>
            <div class="flex items-center gap-2">
                <a href="{% url 'physicians:physician_ratings_dashboard' %}" 
                   class="px-4 py-2.5 bg-white/10 text-white rounded-xl font-semibold hover:bg-white/20 transition flex items-center gap-2">
                    <i data-lucide="bar-chart-3" class="w-4 h-4"></i>
                    {% trans "Dashboard" %}
                </a>
                <a href="{% url 'physicians:physician_list' %}" 
                   class="px-4 py-2.5 bg-white text-[#005696] rounded-xl font-semibold hover:bg-white/90 transition flex items-center gap-2">
                    <i data-lucide="arrow-left" class="w-4 h-4"></i>
                    {% trans "Back to Physicians" %}
                </a>
            </div>
        </div>
    </div>

    <!-- Filters -->
    <section class="section-card mb-6">
        <div class="section-header">
            <div class="section-icon bg-blue/10">
                <i data-lucide="filter" class="w-5 h-5 text-blue"></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-3 lg:grid-cols-5 gap-4">
                <div>
                    <label class="field-label">{% trans "Search Physician" %}</label>
                    <div class="relative">
                        <i data-lucide="search" class="w-4 h-4 text-slate absolute left-3 top-1/2 -translate-y-1/2"></i>
                        <input type="text" name="search" 
                               placeholder="{% trans 'Name or license...' %}"
                               value="{{ filters.search }}"
                               class="w-full pl-10 pr-4 py-2.5 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy/20 focus:border-navy outline-none transition">
                    </div>
                </div>
                <div>
                    <label class="field-label">{% trans "Year" %}</label>
                    <select name="year" class="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy/20 focus:border-navy outline-none transition">
                        <option value="">{% trans "All Years" %}</option>
                        {% for y in years %}
                        <option value="{{ y }}" {% if filters.year == y|stringformat:"s" %}selected{% endif %}>
                            {{ y }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div>
                    <label class="field-label">{% trans "Month" %}</label>
                    <select name="month" class="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy/20 focus:border-navy outline-none transition">
                        <option value="">{% trans "All Months" %}</option>
                        {% for m in "123456789012"|make_list %}
                        <option value="{{ forloop.counter }}" {% if filters.month == forloop.counter|stringformat:"s" %}selected{% endif %}>
                            {{ forloop.counter|stringformat:"02d" }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div>
                    <label class="field-label">{% trans "Department" %}</label>
                    <select name="department" class="w-full px-4 py-2.5 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-navy/20 focus:border-navy outline-none transition" data-tomselect>
                        <option value="">{% trans "All Departments" %}</option>
                        {% for department in departments %}
                        <option value="{{ department.id }}" {% if filters.department == department.id|stringformat:"s" %}selected{% endif %}>
                            {{ department.name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="flex items-end gap-2">
                    <button type="submit" class="flex-1 px-6 py-2.5 bg-navy text-white rounded-xl font-semibold shadow-md hover:bg-blue transition flex items-center justify-center gap-2">
                        <i data-lucide="search" class="w-4 h-4"></i>
                        {% trans "Filter" %}
                    </button>
                    <a href="{% url 'physicians:ratings_list' %}" class="flex-1 px-6 py-2.5 border border-slate-200 text-slate rounded-xl font-semibold hover:bg-slate-50 hover:border-slate-300 transition flex items-center justify-center gap-2">
                        <i data-lucide="x-circle" class="w-4 h-4"></i>
                        {% trans "Clear" %}
                    </a>
                </div>
            </form>
        </div>
    </section>

    <!-- Ratings Table -->
    <section class="section-card">
        <div class="section-header">
            <div class="section-icon bg-yellow/10">
                <i data-lucide="star" class="w-5 h-5 text-yellow-600"></i>
            </div>
            <h2 class="text-lg font-bold text-navy">{% trans "Ratings" %}</h2>
        </div>
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead class="bg-slate-50 border-b border-slate-200">
                    <tr>
                        <th class="text-left text-xs font-bold text-navy uppercase tracking-wider py-4 px-6">{% trans "Period" %}</th>
                        <th class="text-left text-xs font-bold text-navy uppercase tracking-wider py-4 px-6">{% trans "Physician" %}</th>
                        <th class="text-left text-xs font-bold text-navy uppercase tracking-wider py-4 px-6">{% trans "Specialization" %}</th>
                        <th class="text-left text-xs font-bold text-navy uppercase tracking-wider py-4 px-6">{% trans "Department" %}</th>
                        <th class="text-left text-xs font-bold text-navy uppercase tracking-wider py-4 px-6">{% trans "Hospital" %}</th>
                        <th class="text-left text-xs font-bold text-navy uppercase tracking-wider py-4 px-6">{% trans "Rating" %}</th>
                        <th class="text-left text-xs font-bold text-navy uppercase tracking-wider py-4 px-6">{% trans "Surveys" %}</th>
                        <th class="text-left text-xs font-bold text-navy uppercase tracking-wider py-4 px-6">{% trans "Sentiment" %}</th>
                        <th class="text-left text-xs font-bold text-navy uppercase tracking-wider py-4 px-6">{% trans "Ranks" %}</th>
                        <th class="text-center text-xs font-bold text-navy uppercase tracking-wider py-4 px-6">{% trans "Actions" %}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    {% for rating in ratings %}
                    <tr class="rating-row cursor-pointer" onclick="window.location='{% url 'physicians:physician_detail' rating.staff.id %}'">
                        <td class="py-4 px-6">
                            <span class="font-bold text-navy">{{ rating.year }}-{{ rating.month|stringformat:"02d" }}</span>
                        </td>
                        <td class="py-4 px-6">
                            <div class="flex items-center gap-3">
                                <div class="w-8 h-8 rounded-lg bg-blue/10 flex items-center justify-center">
                                    <i data-lucide="user" class="w-4 h-4 text-blue"></i>
                                </div>
                                <div>
                                    <p class="font-bold text-navy text-sm">{{ rating.staff.get_full_name }}</p>
                                    <p class="text-xs text-slate">{{ rating.staff.license_number }}</p>
                                </div>
                            </div>
                        </td>
                        <td class="py-4 px-6">
                            <span class="text-sm text-slate">{{ rating.staff.specialization|default:"-" }}</span>
                        </td>
                        <td class="py-4 px-6">
                            {% if rating.staff.department %}
                            <span class="text-sm text-slate">{{ rating.staff.department.name }}</span>
                            {% else %}
                            <span class="text-sm text-slate">-</span>
                            {% endif %}
                        </td>
                        <td class="py-4 px-6">
                            <div class="flex items-center gap-2">
                                <i data-lucide="building-2" class="w-4 h-4 text-slate"></i>
                                <span class="text-sm text-slate">{{ rating.staff.hospital.name }}</span>
                            </div>
                        </td>
                        <td class="py-4 px-6">
                            <div class="flex items-center gap-1">
                                <i data-lucide="star" class="w-4 h-4 text-yellow-500 fill-yellow-500"></i>
                                <span class="font-bold text-navy">{{ rating.average_rating|floatformat:2 }}</span>
                            </div>
                        </td>
                        <td class="py-4 px-6">
                            <span class="badge badge-light">{{ rating.total_surveys }}</span>
                        </td>
                        <td class="py-4 px-6">
                            <div class="flex gap-1">
                                <span class="badge badge-success" title="{% trans 'Positive' %}">
                                    {{ rating.positive_count }}
                                </span>
                                <span class="badge badge-warning" title="{% trans 'Neutral' %}">
                                    {{ rating.neutral_count }}
                                </span>
                                <span class="badge badge-danger" title="{% trans 'Negative' %}">
                                    {{ rating.negative_count }}
                                </span>
                            </div>
                        </td>
                        <td class="py-4 px-6">
                            <div class="text-xs text-slate">
                                {% if rating.hospital_rank %}
                                <div class="flex items-center gap-1">
                                    <i data-lucide="building-2" class="w-3 h-3"></i>
                                    <span>#{{ rating.hospital_rank }}</span>
                                </div>
                                {% endif %}
                                {% if rating.department_rank %}
                                <div class="flex items-center gap-1">
                                    <i data-lucide="users" class="w-3 h-3"></i>
                                    <span>#{{ rating.department_rank }}</span>
                                </div>
                                {% endif %}
                                {% if not rating.hospital_rank and not rating.department_rank %}
                                <span>-</span>
                                {% endif %}
                            </div>
                        </td>
                        <td class="py-4 px-6 text-center" onclick="event.stopPropagation();">
                            <a href="{% url 'physicians:physician_detail' rating.staff.id %}"
                               class="w-8 h-8 flex items-center justify-center rounded-lg bg-blue/10 text-blue hover:bg-blue hover:text-white transition mx-auto"
                               title="{% trans 'View' %}">
                                <i data-lucide="eye" class="w-4 h-4"></i>
                            </a>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="10" class="py-16 text-center">
                            <div class="flex flex-col items-center justify-center">
                                <div class="w-20 h-20 bg-slate-100 rounded-full flex items-center justify-center mb-4">
                                    <i data-lucide="inbox" class="w-10 h-10 text-slate-300"></i>
                                </div>
                                <p class="text-slate font-medium mb-2">{% trans "No ratings found" %}</p>
                                <p class="text-slate text-sm">{% trans "Try adjusting your filters" %}</p>
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <!-- Pagination -->
        {% if page_obj.has_other_pages %}
        <div class="bg-slate-50 px-8 py-4 flex items-center justify-between border-t border-slate-200">
            <div class="flex items-center gap-4">
                <span class="text-xs text-slate font-medium">
                    {% trans "Showing" %} <span class="font-bold text-navy">{{ page_obj.start_index }}-{{ page_obj.end_index }}</span> {% trans "of" %} <span class="font-bold text-navy">{{ page_obj.paginator.count }}</span> {% trans "entries" %}
                </span>
            </div>
            <div class="flex 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="w-8 h-8 flex items-center justify-center rounded-lg border bg-white hover:bg-slate-50 transition">
                    <i data-lucide="chevron-left" class="w-4 h-4 text-slate"></i>
                </a>
                {% else %}
                <span class="w-8 h-8 flex items-center justify-center rounded-lg border bg-slate-100 text-slate-300 cursor-not-allowed">
                    <i data-lucide="chevron-left" class="w-4 h-4"></i>
                </span>
                {% endif %}
                
                {% for num in page_obj.paginator.page_range %}
                    {% if page_obj.number == num %}
                    <span class="w-8 h-8 flex items-center justify-center rounded-lg bg-navy text-white text-xs font-bold">{{ num }}</span>
                    {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                    <a href="?page={{ num }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}"
                       class="w-8 h-8 flex items-center justify-center rounded-lg border bg-white hover:bg-slate-50 text-xs font-bold text-slate transition">
                        {{ num }}
                    </a>
                    {% elif num == 1 or num == page_obj.paginator.num_pages %}
                    <a href="?page={{ num }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}"
                       class="w-8 h-8 flex items-center justify-center rounded-lg border bg-white hover:bg-slate-50 text-xs font-bold text-slate transition">
                        {{ num }}
                    </a>
                    {% elif num == page_obj.number|add:'-3' or num == page_obj.number|add:'3' %}
                    <span class="w-8 h-8 flex items-center justify-center text-xs text-slate">...</span>
                    {% endif %}
                {% endfor %}
                
                {% if page_obj.has_next %}
                <a href="?page={{ page_obj.next_page_number }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}"
                   class="w-8 h-8 flex items-center justify-center rounded-lg border bg-white hover:bg-slate-50 transition">
                    <i data-lucide="chevron-right" class="w-4 h-4 text-slate"></i>
                </a>
                {% else %}
                <span class="w-8 h-8 flex items-center justify-center rounded-lg border bg-slate-100 text-slate-300 cursor-not-allowed">
                    <i data-lucide="chevron-right" class="w-4 h-4"></i>
                </span>
                {% endif %}
            </div>
        </div>
        {% endif %}
    </section>
</div>
{% endblock %}

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