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

{% block title %}{{ _("Inquiries Console")}} - PX360{% endblock %}

{% block extra_css %}
<style>
    .filter-panel {
        background: #f8f9fa;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        padding: 20px;
        margin-bottom: 20px;
    }
    .filter-panel.collapsed {
        padding: 10px 20px;
    }
    .filter-panel.collapsed .filter-body {
        display: none;
    }
    .table-toolbar {
        background: #fff;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        padding: 15px;
        margin-bottom: 15px;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .status-badge {
        padding: 4px 12px;
        border-radius: 12px;
        font-size: 0.85rem;
        font-weight: 500;
    }
    .status-open { background: #e3f2fd; color: #1976d2; }
    .status-in_progress { background: #fff3e0; color: #f57c00; }
    .status-resolved { background: #e8f5e9; color: #388e3c; }
    .status-closed { background: #f5f5f5; color: #616161; }
    
    .priority-badge {
        padding: 4px 12px;
        border-radius: 12px;
        font-size: 0.85rem;
        font-weight: 500;
    }
    .priority-low { background: #e8f5e9; color: #388e3c; }
    .priority-medium { background: #fff3e0; color: #f57c00; }
    .priority-high { background: #ffebee; color: #d32f2f; }
    .priority-urgent { background: #880e4f; color: #fff; }
    
    .overdue-badge {
        background: #d32f2f;
        color: white;
        padding: 2px 8px;
        border-radius: 10px;
        font-size: 0.75rem;
        font-weight: 600;
        margin-left: 8px;
    }
    
    .inquiry-row:hover {
        background: #f8f9fa;
        cursor: pointer;
    }
    
    .stat-card {
        border-left: 4px solid;
        transition: transform 0.2s;
    }
    .stat-card:hover {
        transform: translateY(-2px);
    }
</style>
{% 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>
                {{ _("Inquiries Console")}}
            </h2>
            <p class="text-muted mb-0">{{ _("Manage patient inquiries and requests")}}</p>
        </div>
        <div>
            <a href="{% url 'complaints:inquiry_create' %}" class="btn btn-primary">
                <i class="bi bi-plus-circle me-1"></i> {{ _("New Inquiry")}}
            </a>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card stat-card border-primary">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">{% trans "Total Inquiries" %}</h6>
                            <h3 class="mb-0">{{ stats.total }}</h3>
                        </div>
                        <div class="text-primary">
                            <i class="bi bi-list-ul" style="font-size: 2rem;"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card stat-card border-info">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">{% trans "Open" %}</h6>
                            <h3 class="mb-0">{{ stats.open }}</h3>
                        </div>
                        <div class="text-info">
                            <i class="bi bi-folder2-open" style="font-size: 2rem;"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card stat-card border-warning">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">{% trans "In Progress" %}</h6>
                            <h3 class="mb-0">{{ stats.in_progress }}</h3>
                        </div>
                        <div class="text-warning">
                            <i class="bi bi-hourglass-split" style="font-size: 2rem;"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card stat-card border-success">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">{% trans "Resolved" %}</h6>
                            <h3 class="mb-0 text-success">{{ stats.resolved }}</h3>
                        </div>
                        <div class="text-success">
                            <i class="bi bi-check-circle-fill" style="font-size: 2rem;"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Filter Panel -->
    <div class="filter-panel" id="filterPanel">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <h5 class="mb-0">
                <i class="bi bi-funnel me-2"></i>{{ _("Filters") }}
            </h5>
            <button class="btn btn-sm btn-outline-secondary" onclick="toggleFilters()">
                <i class="bi bi-chevron-up" id="filterToggleIcon"></i>
            </button>
        </div>
        
        <div class="filter-body">
            <form method="get" action="{% url 'complaints:inquiry_list' %}" id="filterForm">
                <div 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 'Subject, contact name...' %}" 
                               value="{{ filters.search }}">
                    </div>
                    
                    <!-- Status -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Status" %}</label>
                        <select class="form-select" name="status">
                            <option value="">{{ _("All Statuses")}}</option>
                            {% for value, label in status_choices %}
                            <option value="{{ value }}" {% if filters.status == value %}selected{% endif %}>
                                {{ label }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <!-- Priority -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Priority" %}</label>
                        <select class="form-select" name="priority">
                            <option value="">{{ _("All Priorities")}}</option>
                            <option value="low" {% if filters.priority == 'low' %}selected{% endif %}>{{ _("Low") }}</option>
                            <option value="medium" {% if filters.priority == 'medium' %}selected{% endif %}>{{ _("Medium") }}</option>
                            <option value="high" {% if filters.priority == 'high' %}selected{% endif %}>{{ _("High") }}</option>
                            <option value="urgent" {% if filters.priority == 'urgent' %}selected{% endif %}>{{ _("Urgent") }}</option>
                        </select>
                    </div>
                    
                    <!-- Category -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Category" %}</label>
                        <select class="form-select" name="category">
                            <option value="">{{ _("All Categories")}}</option>
                            <option value="appointment" {% if filters.category == 'appointment' %}selected{% endif %}>{{ _("Appointment")}}</option>
                            <option value="billing" {% if filters.category == 'billing' %}selected{% endif %}>{{ _("Billing") }}</option>
                            <option value="medical_records" {% if filters.category == 'medical_records' %}selected{% endif %}>{{ _("Medical Records")}}</option>
                            <option value="pharmacy" {% if filters.category == 'pharmacy' %}selected{% endif %}>{{ _("Pharmacy")}}</option>
                            <option value="insurance" {% if filters.category == 'insurance' %}selected{% endif %}>{{ _("Insurance")}}</option>
                            <option value="feedback" {% if filters.category == 'feedback' %}selected{% endif %}>{{ _("Feedback")}}</option>
                            <option value="general" {% if filters.category == 'general' %}selected{% endif %}>{{ _("General")}}</option>
                            <option value="other" {% if filters.category == 'other' %}selected{% endif %}>{{ _("Other") }}</option>
                        </select>
                    </div>
                    
                    <!-- Hospital -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Hospital" %}</label>
                        <select class="form-select" name="hospital">
                            <option value="">{{ _("All Hospitals")}}</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>
                    
                    <!-- Assigned To -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Assigned To" %}</label>
                        <select class="form-select" name="assigned_to">
                            <option value="">{{ _("All Users")}}</option>
                            {% for user_obj in assignable_users %}
                            <option value="{{ user_obj.id }}" {% if filters.assigned_to == user_obj.id|stringformat:"s" %}selected{% endif %}>
                                {{ user_obj.get_full_name }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <!-- Date Range -->
                    <div class="col-md-3">
                        <label class="form-label">{% trans "Date From" %}</label>
                        <input type="date" class="form-control" name="date_from" value="{{ filters.date_from }}">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">{% trans "Date To" %}</label>
                        <input type="date" class="form-control" name="date_to" value="{{ filters.date_to }}">
                    </div>
                </div>
                
                <div class="mt-3 d-flex gap-2">
                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-search me-1"></i> {{ _("Apply Filters")}}
                    </button>
                    <a href="{% url 'complaints:inquiry_list' %}" class="btn btn-outline-secondary">
                        <i class="bi bi-x-circle me-1"></i> {{ _("Clear") }}
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Table Toolbar -->
    <div class="table-toolbar">
        <div>
            <span class="text-muted">
                Showing {{ page_obj.start_index }} to {{ page_obj.end_index }} of {{ page_obj.paginator.count }} inquiries
            </span>
        </div>
        <div class="d-flex gap-2">
            <button class="btn btn-sm btn-outline-primary" onclick="exportData('csv')">
                <i class="bi bi-file-earmark-spreadsheet me-1"></i> {{ _("Export CSV")}}
            </button>
            <button class="btn btn-sm btn-outline-primary" onclick="exportData('excel')">
                <i class="bi bi-file-earmark-excel me-1"></i> {{ _("Export Excel")}}
            </button>
        </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 style="width: 50px;">
                                <input type="checkbox" class="form-check-input" id="selectAll">
                            </th>
                            <th>{% trans "ID" %}</th>
                            <th>{% trans "Subject" %}</th>
                            <th>{% trans "Contact" %}</th>
                            <th>{% trans "Category" %}</th>
                            <th>{% trans "Status" %}</th>
                            <th>{% trans "Priority" %}</th>
                            <th>{% trans "Hospital" %}</th>
                            <th>{% trans "Assigned To" %}</th>
                            <th>{% trans "Due Date" %}</th>
                            <th>{% trans "Created" %}</th>
                            <th>{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for inquiry in inquiries %}
                        <tr class="inquiry-row" onclick="window.location='{% url 'complaints:inquiry_detail' inquiry.id %}'">
                            <td onclick="event.stopPropagation();">
                                <input type="checkbox" class="form-check-input inquiry-checkbox" 
                                       value="{{ inquiry.id }}">
                            </td>
                            <td>
                                <small class="text-muted">#{{ inquiry.id|slice:":8" }}</small>
                            </td>
                            <td>
                                <div class="d-flex align-items-center">
                                    {{ inquiry.subject|truncatewords:8 }}
                                    {% if inquiry.is_overdue %}
                                    <span class="overdue-badge">{{ _("OVERDUE") }}</span>
                                    {% endif %}
                                </div>
                            </td>
                            <td>
                                {% if inquiry.patient %}
                                    <strong>{{ inquiry.patient.get_full_name }}</strong><br>
                                    <small class="text-muted">{{ _("MRN") }}: {{ inquiry.patient.mrn }}</small>
                                {% else %}
                                    <strong>{{ inquiry.contact_name|default:inquiry.contact_email }}</strong><br>
                                    <small class="text-muted">{{ inquiry.contact_email }}</small>
                                {% endif %}
                            </td>
                            <td>
                                <span class="badge bg-secondary">{{ inquiry.get_category_display }}</span>
                            </td>
                            <td>
                                <span class="status-badge status-{{ inquiry.status }}">
                                    {{ inquiry.get_status_display }}
                                </span>
                            </td>
                            <td>
                                {% if inquiry.priority %}
                                <span class="priority-badge priority-{{ inquiry.priority }}">
                                    {{ inquiry.get_priority_display }}
                                </span>
                                {% else %}
                                <span class="text-muted"><em>-</em></span>
                                {% endif %}
                            </td>
                            <td>
                                <small>{{ inquiry.hospital.name_en|truncatewords:3 }}</small>
                            </td>
                            <td>
                                {% if inquiry.assigned_to %}
                                <small>{{ inquiry.assigned_to.get_full_name }}</small>
                                {% else %}
                                <span class="text-muted"><em>{{ _("Unassigned") }}</em></span>
                                {% endif %}
                            </td>
                            <td>
                                {% if inquiry.due_date %}
                                <small class="{% if inquiry.is_overdue %}text-danger fw-bold{% endif %}">
                                    {{ inquiry.due_date|date:"M d, Y H:i" }}
                                </small>
                                {% else %}
                                <span class="text-muted"><em>-</em></span>
                                {% endif %}
                            </td>
                            <td>
                                <small class="text-muted">{{ inquiry.created_at|date:"M d, Y" }}</small>
                            </td>
                            <td onclick="event.stopPropagation();">
                                <div class="btn-group btn-group-sm">
                                    <a href="{% url 'complaints:inquiry_detail' inquiry.id %}" 
                                       class="btn btn-outline-primary" title="{% trans 'View' %}">
                                        <i class="bi bi-eye"></i>
                                    </a>
                                </div>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="12" class="text-center py-5">
                                <i class="bi bi-inbox" style="font-size: 3rem; color: #ccc;"></i>
                                <p class="text-muted mt-3">{{ _("No inquiries found")}}</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <nav aria-label="Inquiries 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=1{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                    <i class="bi bi-chevron-double-left"></i>
                </a>
            </li>
            <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>
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                    <i class="bi bi-chevron-double-right"></i>
                </a>
            </li>
            {% endif %}
        </ul>
    </nav>
    {% endif %}
</div>
{% endblock %}

{% block extra_js %}
<script>
function toggleFilters() {
    const panel = document.getElementById('filterPanel');
    const icon = document.getElementById('filterToggleIcon');
    panel.classList.toggle('collapsed');
    icon.classList.toggle('bi-chevron-up');
    icon.classList.toggle('bi-chevron-down');
}

function exportData(format) {
    const params = new URLSearchParams(window.location.search);
    params.set('export', format);
    window.location.href = '{% url "complaints:inquiry_list" %}?' + params.toString();
}

// Select all checkbox
document.getElementById('selectAll')?.addEventListener('change', function() {
    const checkboxes = document.querySelectorAll('.inquiry-checkbox');
    checkboxes.forEach(cb => cb.checked = this.checked);
});
</script>
{% endblock %}
