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

{% block title %}{{ _("Complaints 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; }
    .status-cancelled { background: #ffebee; color: #d32f2f; }

    .severity-badge {
        padding: 4px 12px;
        border-radius: 12px;
        font-size: 0.85rem;
        font-weight: 500;
    }
    .severity-low { background: #e8f5e9; color: #388e3c; }
    .severity-medium { background: #fff3e0; color: #f57c00; }
    .severity-high { background: #ffebee; color: #d32f2f; }
    .severity-critical { 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;
    }

    .complaint-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-exclamation-triangle-fill text-warning me-2"></i>
                {{ _("Complaints Console")}}
            </h2>
            <p class="text-muted mb-0">{{ _("Manage and track patient complaints with SLA monitoring")}}</p>
        </div>
        <div class="d-flex gap-2">
            {% if user.is_px_admin or user.is_hospital_admin %}
            <a href="{% url 'complaints:complaint_create' %}" class="btn btn-primary">
                <i class="bi bi-plus-circle me-1"></i> {{ _("New Complaint")}}
            </a>
            {% endif %}
            <a href="{% url 'complaints:public_complaint_submit' %}" class="btn btn-success" target="_blank" rel="noopener noreferrer">
                <i class="bi bi-globe me-1"></i> {{ _("Public Complaint Form")}}
            </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 Complaints" %}</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-danger">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">{% trans "Overdue" %}</h6>
                            <h3 class="mb-0 text-danger">{{ stats.overdue }}</h3>
                        </div>
                        <div class="text-danger">
                            <i class="bi bi-exclamation-triangle-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:complaint_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 'Title, MRN, Patient 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>

                    <!-- Severity -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Severity" %}</label>
                        <select class="form-select" name="severity">
                            <option value="">{{ _("All Severities")}}</option>
                            <option value="low" {% if filters.severity == 'low' %}selected{% endif %}>{{ _("Low") }}</option>
                            <option value="medium" {% if filters.severity == 'medium' %}selected{% endif %}>{{ _("Medium") }}</option>
                            <option value="high" {% if filters.severity == 'high' %}selected{% endif %}>{{ _("High") }}</option>
                            <option value="critical" {% if filters.severity == 'critical' %}selected{% endif %}>{{ _("Critical") }}</option>
                        </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="clinical_care" {% if filters.category == 'clinical_care' %}selected{% endif %}>{{ _("Clinical Care")}}</option>
                            <option value="staff_behavior" {% if filters.category == 'staff_behavior' %}selected{% endif %}>{{ _("Staff Behavior")}}</option>
                            <option value="facility" {% if filters.category == 'facility' %}selected{% endif %}>{{ _("Facility & Environment")}}</option>
                            <option value="wait_time" {% if filters.category == 'wait_time' %}selected{% endif %}>{{ _("Wait Time")}}</option>
                            <option value="billing" {% if filters.category == 'billing' %}selected{% endif %}>{{ _("Billing") }}</option>
                            <option value="communication" {% if filters.category == 'communication' %}selected{% endif %}>{{ _("Communication") }}</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>

                    <!-- Department -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Department" %}</label>
                        <select class="form-select" name="department">
                            <option value="">{{ _("All Departments")}}</option>
                            {% for dept in departments %}
                            <option value="{{ dept.id }}" {% if filters.department == dept.id|stringformat:"s" %}selected{% endif %}>
                                {{ dept.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>

                    <!-- Overdue -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "SLA Status" %}</label>
                        <select class="form-select" name="is_overdue">
                            <option value="">{{ _("All") }}</option>
                            <option value="true" {% if filters.is_overdue == 'true' %}selected{% endif %}>Overdue Only</option>
                        </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:complaint_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 }} complaints
            </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>
    <!-- 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 style="width: 50px;">
                                <input type="checkbox" class="form-check-input" id="selectAll">
                            </th>
                            <th>{% trans "ID" %}</th>
                            <th>{% trans "Patient" %}</th>
                            <th>{% trans "Title" %}</th>
                            <th>{% trans "Category" %}</th>
                            <th>{% trans "Status" %}</th>
                            <th>{% trans "Severity" %}</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 complaint in complaints %}
                        <tr class="complaint-row" onclick="window.location='{% url 'complaints:complaint_detail' complaint.id %}'">
                            <td onclick="event.stopPropagation();">
                                <input type="checkbox" class="form-check-input complaint-checkbox"
                                       value="{{ complaint.id }}">
                            </td>
                            <td>
                                <small class="text-muted">#{{ complaint.id|slice:":8" }}</small>
                            </td>
                            <td>
                                <strong>{{ complaint.patient.get_full_name }}</strong><br>
                                <small class="text-muted">{{ _("MRN") }}: {{ complaint.patient.mrn }}</small>
                            </td>
                            <td>
                                <div class="d-flex align-items-center">
                                    {{ complaint.title|truncatewords:8 }}
                                    {% if complaint.is_overdue %}
                                    <span class="overdue-badge">{{ _("OVERDUE") }}</span>
                                    {% endif %}
                                </div>
                            </td>
                            <td>
                                <span class="badge bg-secondary">{{ complaint.get_category_display }}</span>
                            </td>
                            <td>
                                <span class="status-badge status-{{ complaint.status }}">
                                    {{ complaint.get_status_display }}
                                </span>
                            </td>
                            <td>
                                <span class="severity-badge severity-{{ complaint.severity }}">
                                    {{ complaint.get_severity_display }}
                                </span>
                            </td>
                            <td>
                                <small>{{ complaint.hospital.name_en|truncatewords:3 }}</small>
                            </td>
                            <td>
                                {% if complaint.assigned_to %}
                                <small>{{ complaint.assigned_to.get_full_name }}</small>
                                {% else %}
                                <span class="text-muted"><em>{{ _("Unassigned") }}</em></span>
                                {% endif %}
                            </td>
                            <td>
                                <small class="{% if complaint.is_overdue %}text-danger fw-bold{% endif %}">
                                    {{ complaint.due_at|date:"M d, Y H:i" }}
                                </small>
                            </td>
                            <td>
                                <small class="text-muted">{{ complaint.created_at|date:"M d, Y" }}</small>
                            </td>
                            <td onclick="event.stopPropagation();">
                                <div class="btn-group btn-group-sm">
                                    <a href="{% url 'complaints:complaint_detail' complaint.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 complaints found")}}</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <nav aria-label="Complaints 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:complaint_list" %}?' + params.toString();
}

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