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

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

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="mb-1">
                <i class="bi bi-person-badge text-primary me-2"></i>
                {% trans "Physicians" %}
            </h2>
            <p class="text-muted mb-0">{% trans "Manage physician profiles and performance" %}</p>
        </div>
        <div>
            <div class="btn-group me-2">
                <a href="{% url 'physicians:specialization_overview' %}" class="btn btn-outline-secondary">
                    <i class="bi bi-diagram-3 me-2"></i>{% trans "By Specialization" %}
                </a>
                <a href="{% url 'physicians:department_overview' %}" class="btn btn-outline-secondary">
                    <i class="bi bi-building me-2"></i>{% trans "By Department" %}
                </a>
            </div>
            <a href="{% url 'physicians:leaderboard' %}" class="btn btn-outline-primary">
                <i class="bi bi-trophy me-2"></i>{% trans "Leaderboard" %}
            </a>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-md-6">
            <div class="card border-left-primary">
                <div class="card-body">
                    <h6 class="text-muted mb-1">{% trans "Total Physicians" %}</h6>
                    <h3 class="mb-0">{{ stats.total }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="card border-left-success">
                <div class="card-body">
                    <h6 class="text-muted mb-1">{% trans "Active Physicians" %}</h6>
                    <h3 class="mb-0">{{ stats.active }}</h3>
                </div>
            </div>
        </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" %}</label>
                    <input type="text" name="search" class="form-control" 
                           placeholder="{% trans 'Name, license, specialization...' %}"
                           value="{{ filters.search }}">
                </div>
                <div class="col-md-3">
                    <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-md-3">
                    <label class="form-label">{% trans "Status" %}</label>
                    <select name="status" class="form-select">
                        <option value="">{% trans "All Status" %}</option>
                        <option value="active" {% if filters.status == "active" %}selected{% endif %}>{% trans "Active" %}</option>
                        <option value="inactive" {% if filters.status == "inactive" %}selected{% endif %}>{% trans "Inactive" %}</option>
                    </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:physician_list' %}" class="btn btn-outline-secondary">
                        <i class="bi bi-x-circle me-2"></i>{% trans "Clear" %}
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Physicians 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 "Physician" %}</th>
                            <th>{% trans "License" %}</th>
                            <th>{% trans "Specialization" %}</th>
                            <th>{% trans "Department" %}</th>
                            <th>{% trans "Hospital" %}</th>
                            <th>{% trans "Current Rating" %}</th>
                            <th>{% trans "Status" %}</th>
                            <th>{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for physician in physicians %}
                        <tr onclick="window.location='{% url 'physicians:physician_detail' physician.id %}'" style="cursor: pointer;">
                            <td>
                                <strong>{{ physician.get_full_name }}</strong><br>
                                <small class="text-muted">{{ physician.email }}</small>
                            </td>
                            <td>
                                <span class="badge bg-secondary">{{ physician.license_number }}</span>
                            </td>
                            <td>{{ physician.specialization }}</td>
                            <td>
                                {% if physician.department %}
                                {{ physician.department.name }}
                                {% else %}
                                <span class="text-muted">-</span>
                                {% endif %}
                            </td>
                            <td>{{ physician.hospital.name }}</td>
                            <td>
                                {% if physician.current_rating %}
                                <div class="d-flex align-items-center">
                                    <strong class="me-2">{{ physician.current_rating.average_rating|floatformat:2 }}</strong>
                                    <span class="badge bg-light text-dark">
                                        {{ physician.current_rating.total_surveys }} {% trans "surveys" %}
                                    </span>
                                </div>
                                {% else %}
                                <span class="text-muted">{% trans "No data" %}</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if physician.status == 'active' %}
                                <span class="badge bg-success">{% trans "Active" %}</span>
                                {% else %}
                                <span class="badge bg-secondary">{% trans "Inactive" %}</span>
                                {% endif %}
                            </td>
                            <td onclick="event.stopPropagation();">
                                <a href="{% url 'physicians:physician_detail' physician.id %}" 
                                   class="btn btn-sm btn-outline-primary">
                                    <i class="bi bi-eye"></i>
                                </a>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="8" 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 physicians found" %}</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <nav aria-label="Physicians 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 %}
