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

{% block title %}{% trans "Physician Ratings" %} - PX360{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="mb-1">
                <i class="bi bi-star text-warning me-2"></i>
                {% trans "Physician Ratings" %}
            </h2>
            <p class="text-muted mb-0">{% trans "Monthly physician performance ratings" %}</p>
        </div>
        <div>
            <a href="{% url 'physicians:physician_list' %}" class="btn btn-outline-primary">
                <i class="bi bi-arrow-left me-2"></i>{% trans "Back to Physicians" %}
            </a>
        </div>
    </div>

    <!-- Filters -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-3">
                    <label class="form-label">{% trans "Search Physician" %}</label>
                    <input type="text" name="search" class="form-control" 
                           placeholder="{% trans 'Name or license...' %}"
                           value="{{ filters.search }}">
                </div>
                <div class="col-md-2">
                    <label class="form-label">{% trans "Year" %}</label>
                    <select name="year" class="form-select">
                        <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 class="col-md-2">
                    <label class="form-label">{% trans "Month" %}</label>
                    <select name="month" class="form-select">
                        <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 class="col-md-2">
                    <label class="form-label">{% trans "Hospital" %}</label>
                    <select name="hospital" class="form-select">
                        <option value="">{% trans "All Hospitals" %}</option>
                        {% for hospital in hospitals %}
                        <option value="{{ hospital.id }}" {% if filters.hospital == hospital.id|stringformat:"s" %}selected{% endif %}>
                            {{ hospital.name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">{% trans "Department" %}</label>
                    <select name="department" class="form-select">
                        <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="col-12">
                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-search me-2"></i>{% trans "Filter" %}
                    </button>
                    <a href="{% url 'physicians:ratings_list' %}" class="btn btn-outline-secondary">
                        <i class="bi bi-x-circle me-2"></i>{% trans "Clear" %}
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Ratings Table -->
    <div class="card">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>{% trans "Period" %}</th>
                            <th>{% trans "Physician" %}</th>
                            <th>{% trans "Specialization" %}</th>
                            <th>{% trans "Department" %}</th>
                            <th>{% trans "Hospital" %}</th>
                            <th>{% trans "Rating" %}</th>
                            <th>{% trans "Surveys" %}</th>
                            <th>{% trans "Sentiment" %}</th>
                            <th>{% trans "Ranks" %}</th>
                            <th>{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for rating in ratings %}
                        <tr onclick="window.location='{% url 'physicians:physician_detail' rating.physician.id %}'" style="cursor: pointer;">
                            <td>
                                <strong>{{ rating.year }}-{{ rating.month|stringformat:"02d" }}</strong>
                            </td>
                            <td>
                                <strong>{{ rating.physician.get_full_name }}</strong><br>
                                <small class="text-muted">{{ rating.physician.license_number }}</small>
                            </td>
                            <td>{{ rating.physician.specialization }}</td>
                            <td>
                                {% if rating.physician.department %}
                                {{ rating.physician.department.name }}
                                {% else %}
                                <span class="text-muted">-</span>
                                {% endif %}
                            </td>
                            <td>{{ rating.physician.hospital.name }}</td>
                            <td>
                                <h5 class="mb-0">{{ rating.average_rating|floatformat:2 }}</h5>
                            </td>
                            <td>
                                <span class="badge bg-light text-dark">{{ rating.total_surveys }}</span>
                            </td>
                            <td>
                                <div class="d-flex gap-1">
                                    <span class="badge bg-success" title="{% trans 'Positive' %}">
                                        {{ rating.positive_count }}
                                    </span>
                                    <span class="badge bg-warning" title="{% trans 'Neutral' %}">
                                        {{ rating.neutral_count }}
                                    </span>
                                    <span class="badge bg-danger" title="{% trans 'Negative' %}">
                                        {{ rating.negative_count }}
                                    </span>
                                </div>
                            </td>
                            <td>
                                <div>
                                    {% if rating.hospital_rank %}
                                    <small class="text-muted">H: #{{ rating.hospital_rank }}</small>
                                    {% endif %}
                                    {% if rating.department_rank %}
                                    <br><small class="text-muted">D: #{{ rating.department_rank }}</small>
                                    {% endif %}
                                    {% if not rating.hospital_rank and not rating.department_rank %}
                                    <span class="text-muted">-</span>
                                    {% endif %}
                                </div>
                            </td>
                            <td onclick="event.stopPropagation();">
                                <a href="{% url 'physicians:physician_detail' rating.physician.id %}" 
                                   class="btn btn-sm btn-outline-primary">
                                    <i class="bi bi-eye"></i>
                                </a>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="10" class="text-center py-5">
                                <i class="bi bi-inbox" style="font-size: 3rem; color: #ccc;"></i>
                                <p class="text-muted mt-3">{% trans "No ratings found" %}</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <nav aria-label="Ratings pagination" class="mt-4">
        <ul class="pagination justify-content-center">
            {% if page_obj.has_previous %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                    <i class="bi bi-chevron-left"></i>
                </a>
            </li>
            {% endif %}
            
            {% for num in page_obj.paginator.page_range %}
                {% if page_obj.number == num %}
                <li class="page-item active"><span class="page-link">{{ num }}</span></li>
                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                <li class="page-item">
                    <a class="page-link" href="?page={{ num }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                        {{ num }}
                    </a>
                </li>
                {% endif %}
            {% endfor %}
            
            {% if page_obj.has_next %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.next_page_number }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                    <i class="bi bi-chevron-right"></i>
                </a>
            </li>
            {% endif %}
        </ul>
    </nav>
    {% endif %}
</div>
{% endblock %}
