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

{% block title %}{% trans "Complaints" %} - {% trans "Call Center" %} - PX360{% endblock %}

{% block extra_css %}
<style>
    .stats-card {
        border-radius: 8px;
        padding: 20px;
        margin-bottom: 20px;
        border: none;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    .badge-status-open { background: #17a2b8; }
    .badge-status-in_progress { background: #ffc107; color: #000; }
    .badge-status-resolved { background: #28a745; }
    .badge-status-closed { background: #6c757d; }
    .badge-severity-critical { background: #dc3545; }
    .badge-severity-high { background: #fd7e14; }
    .badge-severity-medium { background: #ffc107; color: #000; }
    .badge-severity-low { background: #28a745; }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Page Header -->
    <div class="mb-4">
        <div class="d-flex justify-content-between align-items-center">
            <div>
                <h2 class="mb-1">
                    <i class="bi bi-telephone-fill text-primary me-2"></i>
                    {% trans "Call Center Complaints" %}
                </h2>
                <p class="text-muted mb-0">{% trans "Complaints created via call center" %}</p>
            </div>
            <a href="{% url 'callcenter:create_complaint' %}" class="btn btn-primary">
                <i class="bi bi-plus-circle me-1"></i> {% trans "Create Complaint" %}
            </a>
        </div>
    </div>

    <!-- Statistics -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="stats-card bg-primary text-white">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h3 class="mb-0">{{ stats.total }}</h3>
                        <small>{% trans "Total Complaints" %}</small>
                    </div>
                    <i class="bi bi-file-earmark-text" style="font-size: 2rem; opacity: 0.5;"></i>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stats-card bg-info text-white">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h3 class="mb-0">{{ stats.open }}</h3>
                        <small>{% trans "Open" %}</small>
                    </div>
                    <i class="bi bi-inbox" style="font-size: 2rem; opacity: 0.5;"></i>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stats-card bg-warning text-dark">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h3 class="mb-0">{{ stats.in_progress }}</h3>
                        <small>{% trans "In Progress" %}</small>
                    </div>
                    <i class="bi bi-hourglass-split" style="font-size: 2rem; opacity: 0.5;"></i>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stats-card bg-success text-white">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h3 class="mb-0">{{ stats.resolved }}</h3>
                        <small>{% trans "Resolved" %}</small>
                    </div>
                    <i class="bi bi-check-circle" style="font-size: 2rem; opacity: 0.5;"></i>
                </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 'Search...' %}" value="{{ filters.search }}">
                </div>
                
                <div class="col-md-2">
                    <label class="form-label">{% trans "Status" %}</label>
                    <select name="status" class="form-select">
                        <option value="">{% trans "All" %}</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="col-md-2">
                    <label class="form-label">{% trans "Severity" %}</label>
                    <select name="severity" class="form-select">
                        <option value="">{% trans "All" %}</option>
                        <option value="critical" {% if filters.severity == 'critical' %}selected{% endif %}>{% trans "Critical" %}</option>
                        <option value="high" {% if filters.severity == 'high' %}selected{% endif %}>{% trans "High" %}</option>
                        <option value="medium" {% if filters.severity == 'medium' %}selected{% endif %}>{% trans "Medium" %}</option>
                        <option value="low" {% if filters.severity == 'low' %}selected{% endif %}>{% trans "Low" %}</option>
                    </select>
                </div>
                
                <div class="col-md-3">
                    <label class="form-label">{% trans "Hospital" %}</label>
                    <select name="hospital" class="form-select">
                        <option value="">{% trans "All" %}</option>
                        {% for hospital in hospitals %}
                        <option value="{{ hospital.id }}" {% if filters.hospital == hospital.id|stringformat:"s" %}selected{% endif %}>
                            {{ hospital.name_en }}
                        </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-funnel me-1"></i> {% trans "Filter" %}
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- Complaints Table -->
    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>{% trans "ID" %}</th>
                            <th>{% trans "Title" %}</th>
                            <th>{% trans "Patient" %}</th>
                            <th>{% trans "Hospital" %}</th>
                            <th>{% trans "Category" %}</th>
                            <th>{% trans "Severity" %}</th>
                            <th>{% trans "Status" %}</th>
                            <th>{% trans "Created" %}</th>
                            <th>{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for complaint in complaints %}
                        <tr>
                            <td>
                                <small class="text-muted">#{{ complaint.id|slice:":8" }}</small>
                            </td>
                            <td>
                                <strong>{{ complaint.title|truncatewords:8 }}</strong>
                            </td>
                            <td>
                                {% if complaint.patient %}
                                    {{ complaint.patient.get_full_name }}<br>
                                    <small class="text-muted">MRN: {{ complaint.patient.mrn }}</small>
                                {% else %}
                                    <span class="text-muted">{% trans "N/A" %}</span>
                                {% endif %}
                            </td>
                            <td>{{ complaint.hospital.name_en }}</td>
                            <td>
                                <span class="badge bg-secondary">
                                    {{ complaint.get_category_display }}
                                </span>
                            </td>
                            <td>
                                <span class="badge badge-severity-{{ complaint.severity }}">
                                    {{ complaint.get_severity_display }}
                                </span>
                            </td>
                            <td>
                                <span class="badge badge-status-{{ complaint.status }}">
                                    {{ complaint.get_status_display }}
                                </span>
                            </td>
                            <td>
                                <small>{{ complaint.created_at|date:"Y-m-d H:i" }}</small>
                            </td>
                            <td>
                                <a href="{% url 'complaints:complaint_detail' complaint.id %}" 
                                   class="btn btn-sm btn-outline-primary">
                                    <i class="bi bi-eye"></i>
                                </a>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="9" class="text-center text-muted py-4">
                                <i class="bi bi-inbox" style="font-size: 3rem;"></i>
                                <p class="mt-2">{% trans "No complaints found" %}</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <!-- Pagination -->
            {% if page_obj.has_other_pages %}
            <nav aria-label="Page navigation" 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 %}">
                            {% trans "Previous" %}
                        </a>
                    </li>
                    {% endif %}
                    
                    <li class="page-item active">
                        <span class="page-link">
                            {% trans "Page" %} {{ page_obj.number }} {% trans "of" %} {{ page_obj.paginator.num_pages }}
                        </span>
                    </li>
                    
                    {% 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 %}">
                            {% trans "Next" %}
                        </a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
