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

{% block title %}{% trans "My Inquiries" %} - {{ 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-question-circle-fill text-info me-2"></i>
                {% trans "My Inquiries" %}
                <span class="badge bg-info">{{ inquiries_count }}</span>
            </h2>
            <p class="text-muted mb-0">
                {% trans "View all inquiries from your source" %}
            </p>
        </div>
        {% if source_user.can_create_inquiries %}
        <a href="{% url 'complaints:inquiry_create' %}" class="btn btn-primary">
            <i class="bi bi-plus-circle me-1"></i> {% trans "Create Inquiry" %}
        </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-5">
                        <label class="form-label">{% trans "Search" %}</label>
                        <input type="text" class="form-control" name="search"
                               placeholder="{% trans 'Subject, contact name...' %}"
                               value="{{ search|default:'' }}">
                    </div>

                <!-- Status -->
                <div class="col-md-3">
                    <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>

                <!-- 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_inquiry_list' %}" 
                           class="btn btn-outline-secondary">
                            <i class="bi bi-x-circle"></i>
                        </a>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- Inquiries 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 "Subject" %}</th>
                            <th>{% trans "Contact" %}</th>
                            <th>{% trans "Category" %}</th>
                            <th>{% trans "Status" %}</th>
                            <th>{% trans "Assigned To" %}</th>
                            <th>{% trans "Created" %}</th>
                            <th>{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for inquiry in inquiries %}
                        <tr>
                            <td><code>{{ inquiry.id|slice:":8" }}</code></td>
                            <td>{{ inquiry.subject|truncatewords:8 }}</td>
                            <td>
                                {% if inquiry.patient %}
                                    <strong>{{ inquiry.patient.get_full_name }}</strong><br>
                                    <small class="text-muted">{% trans "MRN" %}: {{ inquiry.patient.mrn }}</small>
                                {% else %}
                                    {{ inquiry.contact_name|default:"-" }}<br>
                                    <small class="text-muted">{{ inquiry.contact_email|default:"-" }}</small>
                                {% endif %}
                            </td>
                            <td><span class="badge bg-secondary">{{ inquiry.get_category_display }}</span></td>
                            <td>
                                {% if inquiry.status == 'open' %}
                                    <span class="badge bg-danger">{% trans "Open" %}</span>
                                {% elif inquiry.status == 'in_progress' %}
                                    <span class="badge bg-warning text-dark">{% trans "In Progress" %}</span>
                                {% elif inquiry.status == 'resolved' %}
                                    <span class="badge bg-success">{% trans "Resolved" %}</span>
                                {% else %}
                                    <span class="badge bg-secondary">{% trans "Closed" %}</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if inquiry.assigned_to %}
                                    {{ inquiry.assigned_to.get_full_name }}
                                {% else %}
                                    <span class="text-muted"><em>{% trans "Unassigned" %}</em></span>
                                {% endif %}
                            </td>
                            <td><small class="text-muted">{{ inquiry.created_at|date:"Y-m-d" }}</small></td>
                            <td>
                                <a href="{% url 'complaints:inquiry_detail' inquiry.pk %}" 
                                   class="btn btn-sm btn-info" 
                                   title="{% trans 'View' %}">
                                    <i class="bi bi-eye"></i>
                                </a>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="8" 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 inquiries found for your source." %}
                                </p>
                                {% if source_user.can_create_inquiries %}
                                <a href="{% url 'complaints:inquiry_create' %}" class="btn btn-primary">
                                    <i class="bi bi-plus-circle me-1"></i> {% trans "Create Inquiry" %}
                                </a>
                                {% endif %}
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

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

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

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