{% extends "layouts/source_user_base.html" %}
{% load i18n %}

{% block title %}{% trans "My Complaints" %} - {{ source.name_en }}{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="mb-1">
                <i class="bi bi-exclamation-triangle-fill text-warning me-2"></i>
                {% trans "My Complaints" %}
                <span class="badge bg-primary">{{ complaints_count }}</span>
            </h2>
            <p class="text-muted mb-0">
                {% trans "View all complaints from your source" %}
            </p>
        </div>
        {% if source_user.can_create_complaints %}
        <a href="{% url 'complaints:complaint_create' %}" class="btn btn-primary">
            <i class="bi bi-plus-circle me-1"></i> {% trans "Create Complaint" %}
        </a>
        {% endif %}
    </div>

    <!-- Filter Panel -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="row g-3">
                    <!-- Search -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Search" %}</label>
                        <input type="text" class="form-control" name="search"
                               placeholder="{% trans 'Title, patient name...' %}"
                               value="{{ search|default:'' }}">
                    </div>

                <!-- Status -->
                <div class="col-md-2">
                    <label class="form-label">{% trans "Status" %}</label>
                    <select class="form-select" name="status">
                        <option value="">{% trans "All Statuses" %}</option>
                        <option value="open" {% if status_filter == 'open' %}selected{% endif %}>{% trans "Open" %}</option>
                        <option value="in_progress" {% if status_filter == 'in_progress' %}selected{% endif %}>{% trans "In Progress" %}</option>
                        <option value="resolved" {% if status_filter == 'resolved' %}selected{% endif %}>{% trans "Resolved" %}</option>
                        <option value="closed" {% if status_filter == 'closed' %}selected{% endif %}>{% trans "Closed" %}</option>
                    </select>
                </div>

                <!-- Priority -->
                <div class="col-md-2">
                    <label class="form-label">{% trans "Priority" %}</label>
                    <select class="form-select" name="priority">
                        <option value="">{% trans "All Priorities" %}</option>
                        <option value="low" {% if priority_filter == 'low' %}selected{% endif %}>{% trans "Low" %}</option>
                        <option value="medium" {% if priority_filter == 'medium' %}selected{% endif %}>{% trans "Medium" %}</option>
                        <option value="high" {% if priority_filter == 'high' %}selected{% endif %}>{% trans "High" %}</option>
                    </select>
                </div>

                <!-- Category -->
                <div class="col-md-2">
                    <label class="form-label">{% trans "Category" %}</label>
                    <select class="form-select" name="category">
                        <option value="">{% trans "All Categories" %}</option>
                        <option value="clinical_care" {% if category_filter == 'clinical_care' %}selected{% endif %}>{% trans "Clinical Care" %}</option>
                        <option value="staff_behavior" {% if category_filter == 'staff_behavior' %}selected{% endif %}>{% trans "Staff Behavior" %}</option>
                        <option value="facility" {% if category_filter == 'facility' %}selected{% endif %}>{% trans "Facility & Environment" %}</option>
                        <option value="wait_time" {% if category_filter == 'wait_time' %}selected{% endif %}>{% trans "Wait Time" %}</option>
                        <option value="billing" {% if category_filter == 'billing' %}selected{% endif %}>{% trans "Billing" %}</option>
                        <option value="communication" {% if category_filter == 'communication' %}selected{% endif %}>{% trans "Communication" %}</option>
                        <option value="other" {% if category_filter == 'other' %}selected{% endif %}>{% trans "Other" %}</option>
                    </select>
                </div>

                <!-- Actions -->
                <div class="col-md-2 d-flex align-items-end">
                    <div class="d-flex gap-2 w-100">
                        <button type="submit" class="btn btn-primary flex-grow-1">
                            <i class="bi bi-search me-1"></i> {% trans "Filter" %}
                        </button>
                        <a href="{% url 'px_sources:source_user_complaint_list' %}" 
                           class="btn btn-outline-secondary">
                            <i class="bi bi-x-circle"></i>
                        </a>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- Complaints 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 "ID" %}</th>
                            <th>{% trans "Title" %}</th>
                            <th>{% trans "Patient" %}</th>
                            <th>{% trans "Category" %}</th>
                            <th>{% trans "Status" %}</th>
                            <th>{% trans "Priority" %}</th>
                            <th>{% trans "Assigned To" %}</th>
                            <th>{% trans "Created" %}</th>
                            <th>{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for complaint in complaints %}
                        <tr>
                            <td><code>{{ complaint.id|slice:":8" }}</code></td>
                            <td>{{ complaint.title|truncatewords:8 }}</td>
                            <td>
                                {% if complaint.patient %}
                                    <strong>{{ complaint.patient.get_full_name }}</strong><br>
                                    <small class="text-muted">{% trans "MRN" %}: {{ complaint.patient.mrn }}</small>
                                {% else %}
                                    <em class="text-muted">{% trans "Not specified" %}</em>
                                {% endif %}
                            </td>
                            <td><span class="badge bg-secondary">{{ complaint.get_category_display }}</span></td>
                            <td>
                                {% if complaint.status == 'open' %}
                                    <span class="badge bg-danger">{% trans "Open" %}</span>
                                {% elif complaint.status == 'in_progress' %}
                                    <span class="badge bg-warning text-dark">{% trans "In Progress" %}</span>
                                {% elif complaint.status == 'resolved' %}
                                    <span class="badge bg-success">{% trans "Resolved" %}</span>
                                {% else %}
                                    <span class="badge bg-secondary">{% trans "Closed" %}</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if complaint.priority == 'high' %}
                                    <span class="badge bg-danger">{% trans "High" %}</span>
                                {% elif complaint.priority == 'medium' %}
                                    <span class="badge bg-warning text-dark">{% trans "Medium" %}</span>
                                {% else %}
                                    <span class="badge bg-success">{% trans "Low" %}</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if complaint.assigned_to %}
                                    {{ complaint.assigned_to.get_full_name }}
                                {% else %}
                                    <span class="text-muted"><em>{% trans "Unassigned" %}</em></span>
                                {% endif %}
                            </td>
                            <td><small class="text-muted">{{ complaint.created_at|date:"Y-m-d" }}</small></td>
                            <td>
                                <a href="{% url 'complaints:complaint_detail' complaint.pk %}" 
                                   class="btn btn-sm btn-info" 
                                   title="{% trans 'View' %}">
                                    <i class="bi bi-eye"></i>
                                </a>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="9" class="text-center py-5">
                                <i class="bi bi-inbox text-muted" style="font-size: 3rem;"></i>
                                <p class="text-muted mt-3">
                                    {% trans "No complaints found for your source." %}
                                </p>
                                {% if source_user.can_create_complaints %}
                                <a href="{% url 'complaints:complaint_create' %}" class="btn btn-primary">
                                    <i class="bi bi-plus-circle me-1"></i> {% trans "Create Complaint" %}
                                </a>
                                {% endif %}
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Pagination -->
    {% if complaints.has_other_pages %}
    <nav aria-label="Complaints pagination" class="mt-4">
        <ul class="pagination justify-content-center">
            {% if complaints.has_previous %}
            <li class="page-item">
                <a class="page-link" href="?page=1&search={{ search }}&status={{ status_filter }}&priority={{ priority_filter }}&category={{ category_filter }}">
                    <i class="bi bi-chevron-double-left"></i>
                </a>
            </li>
            <li class="page-item">
                <a class="page-link" href="?page={{ complaints.previous_page_number }}&search={{ search }}&status={{ status_filter }}&priority={{ priority_filter }}&category={{ category_filter }}">
                    <i class="bi bi-chevron-left"></i>
                </a>
            </li>
            {% endif %}

            {% for num in complaints.paginator.page_range %}
                {% if complaints.number == num %}
                <li class="page-item active"><span class="page-link">{{ num }}</span></li>
                {% elif num > complaints.number|add:'-3' and num < complaints.number|add:'3' %}
                <li class="page-item">
                    <a class="page-link" href="?page={{ num }}&search={{ search }}&status={{ status_filter }}&priority={{ priority_filter }}&category={{ category_filter }}">
                        {{ num }}
                    </a>
                </li>
                {% endif %}
            {% endfor %}

            {% if complaints.has_next %}
            <li class="page-item">
                <a class="page-link" href="?page={{ complaints.next_page_number }}&search={{ search }}&status={{ status_filter }}&priority={{ priority_filter }}&category={{ category_filter }}">
                    <i class="bi bi-chevron-right"></i>
                </a>
            </li>
            <li class="page-item">
                <a class="page-link" href="?page={{ complaints.paginator.num_pages }}&search={{ search }}&status={{ status_filter }}&priority={{ priority_filter }}&category={{ category_filter }}">
                    <i class="bi bi-chevron-double-right"></i>
                </a>
            </li>
            {% endif %}
        </ul>
    </nav>
    {% endif %}
</div>
{% endblock %}