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

{% block title %}{% trans "Suggestions" %} - PX360{% endblock %}

{% block extra_css %}
<style>
    .page-header-gradient {
        background: linear-gradient(135deg, #005696 0%, #0069a8 50%, #007bbd 100%);
        color: white;
        padding: 1.5rem 2rem;
        border-radius: 1rem;
        margin-bottom: 1.5rem;
        box-shadow: 0 10px 15px -3px rgba(0, 86, 150, 0.2);
    }
    .stat-card {
        background: white;
        border-radius: 1rem;
        border: 2px solid #e2e8f0;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
        transition: all 0.3s ease;
    }
    .stat-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
        border-color: #005696;
    }
    .section-card {
        background: white;
        border-radius: 1rem;
        border: 2px solid #e2e8f0;
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        overflow: hidden;
        transition: all 0.3s ease;
    }
    .section-card:hover {
        border-color: #005696;
        box-shadow: 0 10px 25px -5px rgba(0, 86, 150, 0.15);
    }
    .section-header {
        padding: 1rem 1.5rem;
        border-bottom: 2px solid #e2e8f0;
        background: linear-gradient(to right, #f8fafc, #f1f5f9);
        display: flex;
        align-items: center;
        gap: 0.75rem;
    }
    .section-icon {
        width: 40px;
        height: 40px;
        border-radius: 0.75rem;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .filter-btn.active { background-color: #005696; color: white; }
    .filter-btn:not(.active) { background-color: transparent; border: 1px solid #e2e8f0; color: #64748b; }
    .filter-btn:not(.active):hover { background-color: #f8fafc; }
</style>
{% endblock %}

{% block content %}
<div class="page-header-gradient">
    <div class="flex justify-between items-center">
        <div>
            <h1 class="text-2xl font-bold mb-1">{% trans "Suggestions" %}</h1>
            <p class="text-blue-100 text-sm">{% trans "Manage and review submitted suggestions" %}</p>
        </div>
        <div class="flex items-center gap-3">
            <a href="{% url 'feedback:feedback_create' %}" class="inline-flex items-center px-4 py-2.5 bg-white text-navy font-medium rounded-xl hover:bg-blue-50 transition">
                <i data-lucide="plus" class="w-4 h-4 me-2"></i>{% trans "New Suggestion" %}
            </a>
        </div>
    </div>
</div>

<div class="grid grid-cols-4 gap-6 mb-6">
    <div class="stat-card bg-white rounded-2xl border-2 border-slate-200 p-6 flex items-center gap-4">
        <div class="flex-shrink-0">
            <div class="w-14 h-14 bg-navy/10 rounded-xl flex items-center justify-center">
                <i data-lucide="message-square" class="w-7 h-7 text-navy"></i>
            </div>
        </div>
        <div>
            <h6 class="text-slate-500 text-sm font-medium mb-1">{% trans "Total Suggestions" %}</h6>
            <h2 class="text-3xl font-bold text-gray-800">{{ stats.total }}</h2>
        </div>
    </div>
    <div class="stat-card bg-white rounded-2xl border-2 border-slate-200 p-6 flex items-center gap-4">
        <div class="flex-shrink-0">
            <div class="w-14 h-14 bg-green-500/10 rounded-xl flex items-center justify-center">
                <i data-lucide="thumbs-up" class="w-7 h-7 text-green-600"></i>
            </div>
        </div>
        <div>
            <h6 class="text-slate-500 text-sm font-medium mb-1">{% trans "Compliments" %}</h6>
            <h2 class="text-3xl font-bold text-gray-800">{{ stats.compliments }}</h2>
        </div>
    </div>
    <div class="stat-card bg-white rounded-2xl border-2 border-slate-200 p-6 flex items-center gap-4">
        <div class="flex-shrink-0">
            <div class="w-14 h-14 bg-yellow-500/10 rounded-xl flex items-center justify-center">
                <i data-lucide="star" class="w-7 h-7 text-yellow-600"></i>
            </div>
        </div>
        <div>
            <h6 class="text-slate-500 text-sm font-medium mb-1">{% trans "Avg Rating" %}</h6>
            <h2 class="text-3xl font-bold text-gray-800">{{ stats.avg_rating|floatformat:1 }} <span class="text-sm text-slate">/5</span></h2>
        </div>
    </div>
    <div class="stat-card bg-white rounded-2xl border-2 border-slate-200 p-6 flex items-center gap-4">
        <div class="flex-shrink-0">
            <div class="w-14 h-14 bg-orange-500/10 rounded-xl flex items-center justify-center">
                <i data-lucide="hourglass" class="w-7 h-7 text-orange-600"></i>
            </div>
        </div>
        <div>
            <h6 class="text-slate-500 text-sm font-medium mb-1">{% trans "Pending Review" %}</h6>
            <h2 class="text-3xl font-bold text-gray-800">{{ stats.submitted }}</h2>
        </div>
    </div>
</div>

<div class="section-card">
    <div class="section-header flex items-center justify-between">
        <div class="flex items-center gap-3">
            <div class="section-icon bg-cyan-500/10">
                <i data-lucide="lightbulb" class="w-5 h-5 text-cyan-600"></i>
            </div>
            <h5 class="text-lg font-semibold text-gray-800">{% trans "Suggestions" %}</h5>
        </div>
        <div class="flex items-center gap-3">
            <a href="?" class="filter-btn px-4 py-1.5 rounded-full text-xs font-semibold transition {% if not filters.feedback_type %}active{% endif %}">
                {% trans "All" %}
            </a>
            <a href="?feedback_type=suggestion" class="filter-btn px-4 py-1.5 rounded-full text-xs font-semibold transition {% if filters.feedback_type == 'suggestion' %}active{% endif %}">
                {% trans "Suggestions" %}
            </a>
            <a href="?feedback_type=compliment" class="filter-btn px-4 py-1.5 rounded-full text-xs font-semibold transition {% if filters.feedback_type == 'compliment' %}active{% endif %}">
                {% trans "Compliments" %}
            </a>
            <div class="h-4 w-[1px] bg-slate-200 mx-2"></div>
            <button onclick="toggleFilters()" class="flex items-center gap-2 text-xs font-bold text-blue uppercase tracking-tight hover:underline">
                <i data-lucide="sliders-horizontal" class="w-3 h-3"></i> {% trans "Advanced" %}
            </button>
        </div>
    </div>
    <p class="px-6 py-2 text-xs font-bold text-slate uppercase bg-slate-50 border-b-2 border-slate-200">
        {% trans "Showing:" %} <span class="text-navy">{{ page_obj.start_index|default:0 }}-{{ page_obj.end_index|default:0 }} {% trans "of" %} {{ page_obj.paginator.count|default:0 }}</span>
    </p>

    <div id="advancedFilters" class="hidden bg-slate-50 px-6 py-4 border-b">
        <form method="get" class="flex flex-wrap gap-4 items-end">
            {% if filters.feedback_type %}
            <input type="hidden" name="feedback_type" value="{{ filters.feedback_type }}">
            {% endif %}
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "Search" %}</label>
                <input type="text" name="search" value="{{ filters.search }}" placeholder="{% trans 'Title, message...' %}" class="px-3 py-1.5 bg-white border rounded-lg text-xs">
            </div>
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "Status" %}</label>
                <select name="status" class="px-3 py-1.5 bg-white border rounded-lg text-xs">
                    <option value="">{% trans "All" %}</option>
                    {% for value, label in status_choices %}
                    <option value="{{ value }}" {% if filters.status == value %}selected{% endif %}>{{ label }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "Category" %}</label>
                <select name="category" class="px-3 py-1.5 bg-white border rounded-lg text-xs">
                    <option value="">{% trans "All" %}</option>
                    {% for value, label in category_choices %}
                    <option value="{{ value }}" {% if filters.category == value %}selected{% endif %}>{{ label }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "Sentiment" %}</label>
                <select name="sentiment" class="px-3 py-1.5 bg-white border rounded-lg text-xs">
                    <option value="">{% trans "All" %}</option>
                    <option value="positive" {% if filters.sentiment == 'positive' %}selected{% endif %}>{% trans "Positive" %}</option>
                    <option value="neutral" {% if filters.sentiment == 'neutral' %}selected{% endif %}>{% trans "Neutral" %}</option>
                    <option value="negative" {% if filters.sentiment == 'negative' %}selected{% endif %}>{% trans "Negative" %}</option>
                </select>
            </div>
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "From" %}</label>
                <input type="date" name="date_from" value="{{ filters.date_from }}" class="px-3 py-1.5 bg-white border rounded-lg text-xs">
            </div>
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "To" %}</label>
                <input type="date" name="date_to" value="{{ filters.date_to }}" class="px-3 py-1.5 bg-white border rounded-lg text-xs">
            </div>
            <button type="submit" class="px-4 py-1.5 bg-navy text-white rounded-lg text-xs font-bold">{% trans "Apply" %}</button>
            <a href="?" class="px-4 py-1.5 border rounded-lg text-xs font-semibold text-slate hover:bg-white">{% trans "Clear" %}</a>
        </form>
    </div>

    <div class="p-0">
        <table class="w-full text-left border-collapse">
            <thead class="bg-slate-50 border-b uppercase text-[10px] font-bold text-slate tracking-wider">
                <tr>
                    <th class="px-6 py-4">{% trans "ID" %}</th>
                    <th class="px-6 py-4">{% trans "Type" %}</th>
                    <th class="px-6 py-4">{% trans "Contact" %}</th>
                    <th class="px-6 py-4">{% trans "Title" %}</th>
                    <th class="px-6 py-4">{% trans "Category" %}</th>
                    <th class="px-6 py-4 text-center">{% trans "Rating" %}</th>
                    <th class="px-6 py-4">{% trans "Sentiment" %}</th>
                    <th class="px-6 py-4">{% trans "Status" %}</th>
                    <th class="px-6 py-4">{% trans "Created" %}</th>
                    <th class="px-6 py-4 text-right">{% trans "Actions" %}</th>
                </tr>
            </thead>
            <tbody class="text-sm divide-y divide-slate-100">
                {% for feedback in feedbacks %}
                <tr class="hover:bg-light/30 transition-colors cursor-pointer group" onclick="window.location.href='{% url 'feedback:feedback_detail' feedback.id %}'">
                    <td class="px-6 py-4 font-bold text-navy">#{{ feedback.id|slice:":8" }}</td>
                    <td class="px-6 py-4">
                        <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase
                            {% if feedback.feedback_type == 'compliment' %}bg-green-100 text-green-700
                            {% elif feedback.feedback_type == 'suggestion' %}bg-cyan-100 text-cyan-700
                            {% elif feedback.feedback_type == 'inquiry' %}bg-amber-100 text-amber-700
                            {% else %}bg-slate-100 text-slate-600{% endif %}">
                            {{ feedback.get_feedback_type_display }}
                        </span>
                    </td>
                    <td class="px-6 py-4">
                        <div class="font-semibold text-slate-800">{{ feedback.get_contact_name }}</div>
                        {% if feedback.patient %}
                        <div class="text-[10px] text-slate font-medium">{% trans "MRN:" %} {{ feedback.patient.mrn }}</div>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4 max-w-xs">
                        <div class="flex items-center gap-2">
                            {{ feedback.title|truncatewords:8 }}
                            {% if feedback.is_featured %}
                            <span class="bg-yellow-100 text-yellow-700 px-2 py-0.5 rounded text-[10px] font-bold">{% trans "FEATURED" %}</span>
                            {% endif %}
                        </div>
                    </td>
                    <td class="px-6 py-4">
                        <span class="bg-slate-100 px-2 py-0.5 rounded text-[10px] font-bold text-slate-600">{{ feedback.get_category_display }}</span>
                    </td>
                    <td class="px-6 py-4 text-center">
                        {% if feedback.rating %}
                        <span class="text-yellow-500">
                            {% for i in "12345" %}
                                {% if forloop.counter <= feedback.rating %}<i data-lucide="star" class="w-3 h-3 inline-block" style="fill:currentColor"></i>
                                {% else %}<i data-lucide="star" class="w-3 h-3 inline-block"></i>
                                {% endif %}
                            {% endfor %}
                        </span>
                        {% else %}
                        <span class="text-slate-300">-</span>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4">
                        <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase
                            {% if feedback.sentiment == 'positive' %}bg-green-100 text-green-700
                            {% elif feedback.sentiment == 'negative' %}bg-red-100 text-red-700
                            {% else %}bg-slate-100 text-slate-600{% endif %}">
                            {{ feedback.get_sentiment_display }}
                        </span>
                    </td>
                    <td class="px-6 py-4">
                        <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase
                            {% if feedback.status == 'submitted' %}bg-blue-100 text-blue-700
                            {% elif feedback.status == 'reviewed' %}bg-orange-100 text-orange-700
                            {% elif feedback.status == 'acknowledged' %}bg-green-100 text-green-700
                            {% elif feedback.status == 'closed' %}bg-slate-100 text-slate-600
                            {% else %}bg-slate-100 text-slate-600{% endif %}">
                            {{ feedback.get_status_display }}
                        </span>
                    </td>
                    <td class="px-6 py-4 text-slate-500">{{ feedback.created_at|date:"M d, Y" }}</td>
                    <td class="px-6 py-4 text-right">
                        <div class="flex justify-end gap-2 opacity-0 group-hover:opacity-100 transition-opacity">
                            <a href="{% url 'feedback:feedback_detail' feedback.id %}" class="p-1.5 text-navy hover:bg-white rounded border border-transparent hover:border-slate-200" title="{% trans 'View' %}">
                                <i data-lucide="eye" class="w-4 h-4"></i>
                            </a>
                        </div>
                    </td>
                </tr>
                {% empty %}
                <tr>
                    <td colspan="10" class="px-6 py-12 text-center">
                        <i data-lucide="inbox" class="w-12 h-12 mx-auto text-slate-300 mb-3"></i>
                        <p class="text-slate">{% trans "No suggestions found" %}</p>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>

        {% if page_obj.has_other_pages %}
        <div class="bg-slate-50 px-8 py-4 flex items-center justify-between border-t">
            <div class="flex items-center gap-4">
                <span class="text-xs text-slate font-medium">
                    {% trans "Showing" %} <span class="font-bold text-navy">{{ page_obj.start_index }}-{{ page_obj.end_index }}</span> {% trans "of" %} <span class="font-bold text-navy">{{ page_obj.paginator.count }}</span> {% trans "entries" %}
                </span>
            </div>
            <div class="flex gap-2">
                {% if page_obj.has_previous %}
                <a href="?page={{ page_obj.previous_page_number }}{% for key, value in filters.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}"
                   class="w-8 h-8 flex items-center justify-center rounded-lg border bg-white hover:bg-slate-50 transition">
                    <i data-lucide="chevron-left" class="w-4 h-4 text-slate"></i>
                </a>
                {% else %}
                <span class="w-8 h-8 flex items-center justify-center rounded-lg border bg-slate-100 text-slate-300 cursor-not-allowed">
                    <i data-lucide="chevron-left" class="w-4 h-4"></i>
                </span>
                {% endif %}

                {% for num in page_obj.paginator.page_range %}
                    {% if page_obj.number == num %}
                    <span class="w-8 h-8 flex items-center justify-center rounded-lg bg-navy text-white text-xs font-bold">{{ num }}</span>
                    {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                    <a href="?page={{ num }}{% for key, value in filters.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}"
                       class="w-8 h-8 flex items-center justify-center rounded-lg border bg-white hover:bg-slate-50 text-xs font-bold text-slate transition">{{ num }}</a>
                    {% elif num == 1 or num == page_obj.paginator.num_pages %}
                    <a href="?page={{ num }}{% for key, value in filters.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}"
                       class="w-8 h-8 flex items-center justify-center rounded-lg border bg-white hover:bg-slate-50 text-xs font-bold text-slate transition">{{ num }}</a>
                    {% elif num == page_obj.number|add:'-3' or num == page_obj.number|add:'3' %}
                    <span class="w-8 h-8 flex items-center justify-center text-xs text-slate">...</span>
                    {% endif %}
                {% endfor %}

                {% if page_obj.has_next %}
                <a href="?page={{ page_obj.next_page_number }}{% for key, value in filters.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}"
                   class="w-8 h-8 flex items-center justify-center rounded-lg border bg-white hover:bg-slate-50 transition">
                    <i data-lucide="chevron-right" class="w-4 h-4 text-slate"></i>
                </a>
                {% else %}
                <span class="w-8 h-8 flex items-center justify-center rounded-lg border bg-slate-100 text-slate-300 cursor-not-allowed">
                    <i data-lucide="chevron-right" class="w-4 h-4"></i>
                </span>
                {% endif %}
            </div>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
function toggleFilters() {
    const panel = document.getElementById('advancedFilters');
    panel.classList.toggle('hidden');
}
</script>
{% endblock %}
