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

{% block title %}{% trans "Specialization Overview" %} - {% trans "Physicians" %} - PX360{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{% url 'physicians:physician_list' %}">{% trans "Physicians" %}</a></li>
                    <li class="breadcrumb-item active">{% trans "Specialization Overview" %}</li>
                </ol>
            </nav>
            <h2 class="mb-1">
                <i class="bi bi-diagram-3 text-primary me-2"></i>
                {% trans "Specialization Overview" %}
            </h2>
            <p class="text-muted mb-0">{% trans "Performance by medical specialization for" %} {{ year }}-{{ month|stringformat:"02d" }}</p>
        </div>
        <div>
            <a href="{% url 'physicians:department_overview' %}" class="btn btn-outline-secondary me-2">
                <i class="bi bi-building me-2"></i>{% trans "Department View" %}
            </a>
            <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 "Year" %}</label>
                    <input type="number" name="year" class="form-control" 
                           value="{{ year }}" min="2020" max="2030">
                </div>
                <div class="col-md-3">
                    <label class="form-label">{% trans "Month" %}</label>
                    <select name="month" class="form-select">
                        {% for m in "123456789012"|make_list %}
                        <option value="{{ forloop.counter }}" {% if month == forloop.counter %}selected{% endif %}>
                            {{ forloop.counter|stringformat:"02d" }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2 d-flex align-items-end">
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="bi bi-search me-2"></i>{% trans "Filter" %}
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- Specializations -->
    {% if specializations %}
    {% for spec_data in specializations %}
    <div class="card mb-4">
        <div class="card-header bg-light">
            <div class="row align-items-center">
                <div class="col-md-4">
                    <h5 class="mb-0">
                        <i class="bi bi-stethoscope text-primary me-2"></i>
                        {{ spec_data.specialization }}
                    </h5>
                </div>
                <div class="col-md-8">
                    <div class="row text-center">
                        <div class="col-3">
                            <strong class="text-success fs-4">{{ spec_data.average_rating|floatformat:2 }}</strong>
                            <br><small class="text-muted">{% trans "Avg Rating" %}</small>
                        </div>
                        <div class="col-3">
                            <strong class="fs-4">{{ spec_data.total_physicians }}</strong>
                            <br><small class="text-muted">{% trans "Physicians" %}</small>
                        </div>
                        <div class="col-3">
                            <strong class="fs-4">{{ spec_data.total_surveys }}</strong>
                            <br><small class="text-muted">{% trans "Surveys" %}</small>
                        </div>
                        <div class="col-3">
                            <div class="d-flex gap-1 justify-content-center">
                                <span class="badge bg-success">{{ spec_data.positive_count }}</span>
                                <span class="badge bg-warning">{{ spec_data.neutral_count }}</span>
                                <span class="badge bg-danger">{{ spec_data.negative_count }}</span>
                            </div>
                            <small class="text-muted">{% trans "Sentiment" %}</small>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover table-sm mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>{% trans "Rank" %}</th>
                            <th>{% trans "Physician" %}</th>
                            <th>{% trans "Department" %}</th>
                            <th>{% trans "Hospital" %}</th>
                            <th>{% trans "Rating" %}</th>
                            <th>{% trans "Surveys" %}</th>
                            <th>{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for rating in spec_data.physicians %}
                        <tr onclick="window.location='{% url 'physicians:physician_detail' rating.staff.id %}'" style="cursor: pointer;">
                            <td>
                                {% if forloop.counter <= 3 %}
                                <strong class="text-primary">#{{ forloop.counter }}</strong>
                                {% else %}
                                <span class="text-muted">#{{ forloop.counter }}</span>
                                {% endif %}
                            </td>
                            <td>
                                <strong>{{ rating.staff.get_full_name }}</strong><br>
                                <small class="text-muted">{{ rating.staff.license_number }}</small>
                            </td>
                            <td>
                                {% if rating.staff.department %}
                                {{ rating.staff.department.name }}
                                {% else %}
                                <span class="text-muted">-</span>
                                {% endif %}
                            </td>
                            <td>{{ rating.staff.hospital.name }}</td>
                            <td>
                                <strong class="text-success">{{ rating.average_rating|floatformat:2 }}</strong>
                            </td>
                            <td>
                                <span class="badge bg-light text-dark">{{ rating.total_surveys }}</span>
                            </td>
                            <td onclick="event.stopPropagation();">
                                <a href="{% url 'physicians:physician_detail' rating.staff.id %}" 
                                   class="btn btn-sm btn-outline-primary">
                                    <i class="bi bi-eye"></i>
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    {% endfor %}
    {% else %}
    <div class="card">
        <div class="card-body text-center py-5">
            <i class="bi bi-inbox" style="font-size: 3rem; color: #ccc;"></i>
            <p class="text-muted mt-3">{% trans "No specialization data available for this period" %}</p>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}
