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

{% block title %}Feedback 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-submitted { background: #e3f2fd; color: #1976d2; }
    .status-reviewed { background: #fff3e0; color: #f57c00; }
    .status-acknowledged { background: #e8f5e9; color: #388e3c; }
    .status-closed { background: #f5f5f5; color: #616161; }
    
    .type-badge {
        padding: 4px 12px;
        border-radius: 12px;
        font-size: 0.85rem;
        font-weight: 500;
    }
    .type-compliment { background: #e8f5e9; color: #2e7d32; }
    .type-suggestion { background: #e3f2fd; color: #1565c0; }
    .type-general { background: #f5f5f5; color: #616161; }
    .type-inquiry { background: #fff3e0; color: #ef6c00; }
    
    .sentiment-badge {
        padding: 4px 12px;
        border-radius: 12px;
        font-size: 0.85rem;
        font-weight: 500;
    }
    .sentiment-positive { background: #e8f5e9; color: #2e7d32; }
    .sentiment-neutral { background: #f5f5f5; color: #616161; }
    .sentiment-negative { background: #ffebee; color: #c62828; }
    
    .rating-stars {
        color: #ffc107;
    }
    
    .feedback-row:hover {
        background: #f8f9fa;
        cursor: pointer;
    }
    
    .stat-card {
        border-left: 4px solid;
        transition: transform 0.2s;
    }
    .stat-card:hover {
        transform: translateY(-2px);
    }
    
    .featured-badge {
        background: #ffd700;
        color: #000;
        padding: 2px 8px;
        border-radius: 10px;
        font-size: 0.75rem;
        font-weight: 600;
        margin-left: 8px;
    }
</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-chat-heart-fill text-primary me-2"></i>
                Feedback Console
            </h2>
            <p class="text-muted mb-0">Manage patient feedback, compliments, and suggestions</p>
        </div>
        <div>
            <a href="{% url 'feedback:feedback_create' %}" class="btn btn-primary">
                <i class="bi bi-plus-circle me-1"></i> New Feedback
            </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 Feedback" %}</h6>
                            <h3 class="mb-0">{{ stats.total }}</h3>
                        </div>
                        <div class="text-primary">
                            <i class="bi bi-chat-dots" 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 "Compliments" %}</h6>
                            <h3 class="mb-0">{{ stats.compliments }}</h3>
                        </div>
                        <div class="text-success">
                            <i class="bi bi-hand-thumbs-up-fill" 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 "Avg Rating" %}</h6>
                            <h3 class="mb-0">{{ stats.avg_rating|floatformat:1 }} <small class="text-muted">/5</small></h3>
                        </div>
                        <div class="text-warning">
                            <i class="bi bi-star-fill" 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 "Pending Review" %}</h6>
                            <h3 class="mb-0">{{ stats.submitted }}</h3>
                        </div>
                        <div class="text-warning">
                            <i class="bi bi-hourglass-split" 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 'feedback:feedback_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, message, patient...' %}" 
                               value="{{ filters.search }}">
                    </div>
                    
                    <!-- Feedback Type -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Type" %}</label>
                        <select class="form-select" name="feedback_type">
                            <option value="">All Types</option>
                            {% for value, label in type_choices %}
                            <option value="{{ value }}" {% if filters.feedback_type == value %}selected{% endif %}>
                                {{ label }}
                            </option>
                            {% endfor %}
                        </select>
                    </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>
                    
                    <!-- Category -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Category" %}</label>
                        <select class="form-select" name="category">
                            <option value="">All Categories</option>
                            {% for value, label in category_choices %}
                            <option value="{{ value }}" {% if filters.category == value %}selected{% endif %}>
                                {{ label }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <!-- Sentiment -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Sentiment" %}</label>
                        <select class="form-select" name="sentiment">
                            <option value="">All Sentiments</option>
                            <option value="positive" {% if filters.sentiment == 'positive' %}selected{% endif %}>Positive</option>
                            <option value="neutral" {% if filters.sentiment == 'neutral' %}selected{% endif %}>Neutral</option>
                            <option value="negative" {% if filters.sentiment == 'negative' %}selected{% endif %}>Negative</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 }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <!-- Rating Range -->
                    <div class="col-md-3">
                        <label class="form-label">{% trans "Min Rating" %}</label>
                        <input type="number" class="form-control" name="rating_min" 
                               min="1" max="5" value="{{ filters.rating_min }}" placeholder="{% trans '1-5' %}">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">{% trans "Max Rating" %}</label>
                        <input type="number" class="form-control" name="rating_max" 
                               min="1" max="5" value="{{ filters.rating_max }}" placeholder="{% trans '1-5' %}">
                    </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 'feedback:feedback_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 }} feedback items
            </span>
        </div>
        <div class="d-flex gap-2">
            <button class="btn btn-sm btn-outline-primary">
                <i class="bi bi-file-earmark-spreadsheet me-1"></i> Export CSV
            </button>
        </div>
    </div>

    <!-- Feedback 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 "Type" %}</th>
                            <th>{% trans "Patient/Contact" %}</th>
                            <th>{% trans "Title" %}</th>
                            <th>{% trans "Category" %}</th>
                            <th>{% trans "Rating" %}</th>
                            <th>{% trans "Sentiment" %}</th>
                            <th>{% trans "Status" %}</th>
                            <th>{% trans "Hospital" %}</th>
                            <th>{% trans "Created" %}</th>
                            <th>{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for feedback in feedbacks %}
                        <tr class="feedback-row" onclick="window.location='{% url 'feedback:feedback_detail' feedback.id %}'">
                            <td onclick="event.stopPropagation();">
                                <input type="checkbox" class="form-check-input feedback-checkbox" 
                                       value="{{ feedback.id }}">
                            </td>
                            <td>
                                <small class="text-muted">#{{ feedback.id|slice:":8" }}</small>
                            </td>
                            <td>
                                <span class="type-badge type-{{ feedback.feedback_type }}">
                                    {{ feedback.get_feedback_type_display }}
                                </span>
                            </td>
                            <td>
                                <strong>{{ feedback.get_contact_name }}</strong><br>
                                {% if feedback.patient %}
                                <small class="text-muted">MRN: {{ feedback.patient.mrn }}</small>
                                {% else %}
                                <small class="text-muted">Anonymous</small>
                                {% endif %}
                            </td>
                            <td>
                                <div class="d-flex align-items-center">
                                    {{ feedback.title|truncatewords:8 }}
                                    {% if feedback.is_featured %}
                                    <span class="featured-badge">★ FEATURED</span>
                                    {% endif %}
                                </div>
                            </td>
                            <td>
                                <span class="badge bg-secondary">{{ feedback.get_category_display }}</span>
                            </td>
                            <td>
                                {% if feedback.rating %}
                                <span class="rating-stars">
                                    {% for i in "12345" %}
                                        {% if forloop.counter <= feedback.rating %}
                                            <i class="bi bi-star-fill"></i>
                                        {% else %}
                                            <i class="bi bi-star"></i>
                                        {% endif %}
                                    {% endfor %}
                                </span>
                                {% else %}
                                <span class="text-muted">-</span>
                                {% endif %}
                            </td>
                            <td>
                                <span class="sentiment-badge sentiment-{{ feedback.sentiment }}">
                                    {{ feedback.get_sentiment_display }}
                                </span>
                            </td>
                            <td>
                                <span class="status-badge status-{{ feedback.status }}">
                                    {{ feedback.get_status_display }}
                                </span>
                            </td>
                            <td>
                                <small>{{ feedback.hospital.name|truncatewords:3 }}</small>
                            </td>
                            <td>
                                <small class="text-muted">{{ feedback.created_at|date:"M d, Y" }}</small>
                            </td>
                            <td onclick="event.stopPropagation();">
                                <div class="btn-group btn-group-sm">
                                    <a href="{% url 'feedback:feedback_detail' feedback.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 feedback found</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <nav aria-label="Feedback 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');
}

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