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

{% block title %}{% trans "Patient Comments" %} - 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);
    }
    .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;
    }
</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 "Step 1 — Classified Patient Comments" %}</h1>
            <p class="text-blue-100 text-sm">{% trans "Classified comments with categories and sentiment" %}</p>
        </div>
        <div class="flex items-center gap-3">
            <a href="{% url 'feedback:export_comments_step1' %}?{{ request.GET.urlencode }}"
               class="inline-flex items-center px-4 py-2.5 bg-white/10 border-2 border-white/30 text-white font-medium rounded-xl hover:bg-white hover:text-navy transition text-sm">
                <i data-lucide="download" class="w-4 h-4 me-2"></i>{% trans "Export Classification" %}
            </a>
            <a href="{% url 'feedback:export_comments_step2' %}?{{ request.GET.urlencode }}"
               class="inline-flex items-center px-4 py-2.5 bg-white text-navy font-medium rounded-xl hover:bg-blue-50 transition text-sm">
                <i data-lucide="download" class="w-4 h-4 me-2"></i>{% trans "Export by Dept" %}
            </a>
        </div>
    </div>
</div>

<div class="section-card mb-6">
    <div class="section-header">
        <div class="section-icon bg-slate-500/10">
            <i data-lucide="sliders-horizontal" class="w-5 h-5 text-slate-600"></i>
        </div>
        <h5 class="text-lg font-semibold text-gray-800">{% trans "Filters" %}</h5>
    </div>
    <div class="p-6">
        <form method="get" class="flex flex-wrap gap-4 items-end">
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "Classification" %}</label>
                <select name="classification" class="px-3 py-1.5 bg-white border border-slate-200 rounded-lg text-xs">
                    <option value="">{% trans "All" %}</option>
                    {% for val, label in classifications %}
                    <option value="{{ val }}" {% if selected_classification == val %}selected{% endif %}>{{ label }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "Sub-Category" %}</label>
                <select name="sub_category" class="px-3 py-1.5 bg-white border border-slate-200 rounded-lg text-xs">
                    <option value="">{% trans "All" %}</option>
                    {% for val, label in sub_categories %}
                    <option value="{{ val }}" {% if selected_sub_category == val %}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 border-slate-200 rounded-lg text-xs">
                    <option value="">{% trans "All" %}</option>
                    {% for val, label in sentiments %}
                    <option value="{{ val }}" {% if selected_sentiment == val %}selected{% endif %}>{{ label }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "Source" %}</label>
                <select name="source_category" class="px-3 py-1.5 bg-white border border-slate-200 rounded-lg text-xs">
                    <option value="">{% trans "All" %}</option>
                    {% for val, label in source_categories %}
                    <option value="{{ val }}" {% if selected_source_category == val %}selected{% endif %}>{{ label }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "Year" %}</label>
                <select name="year" class="px-3 py-1.5 bg-white border border-slate-200 rounded-lg text-xs">
                    <option value="">{% trans "All" %}</option>
                    {% for y in years_range %}
                    <option value="{{ y }}" {% if selected_year == y %}selected{% endif %}>{{ y }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "Month" %}</label>
                <select name="month" class="px-3 py-1.5 bg-white border border-slate-200 rounded-lg text-xs">
                    <option value="">{% trans "All" %}</option>
                    {% for m in months_range %}
                    <option value="{{ m }}" {% if selected_month == m %}selected{% endif %}>{{ m }}</option>
                    {% endfor %}
                </select>
            </div>
            <button type="submit" class="px-4 py-1.5 bg-navy text-white rounded-lg text-xs font-bold">
                <i data-lucide="filter" class="w-3 h-3 inline-block me-1"></i>{% trans "Filter" %}
            </button>
        </form>
    </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="message-square" class="w-5 h-5 text-cyan-600"></i>
            </div>
            <h5 class="text-lg font-semibold text-gray-800">{% trans "Comments" %}</h5>
        </div>
        <span class="text-xs font-bold text-slate uppercase">{% trans "Total:" %} <span class="text-navy">{{ page_obj.paginator.count }}</span></span>
    </div>
    <div class="p-0">
        <div class="overflow-x-auto">
            <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">#</th>
                        <th class="px-6 py-4">{% trans "Source" %}</th>
                        <th class="px-6 py-4">{% trans "Comment" %}</th>
                        <th class="px-6 py-4">{% trans "Classification" %}</th>
                        <th class="px-6 py-4">{% trans "Sub-Category" %}</th>
                        <th class="px-6 py-4">{% trans "Sentiment" %}</th>
                        <th class="px-6 py-4">{% trans "Negative" %}</th>
                        <th class="px-6 py-4">{% trans "Positive" %}</th>
                    </tr>
                </thead>
                <tbody class="text-sm divide-y divide-slate-100">
                    {% for c in page_obj %}
                    <tr class="hover:bg-slate-50/50 transition-colors">
                        <td class="px-6 py-4 font-semibold text-navy">{{ c.serial_number }}</td>
                        <td class="px-6 py-4 text-slate-600">{{ c.get_source_category_display }}</td>
                        <td class="px-6 py-4 max-w-xs truncate text-slate-700" title="{{ c.comment_text }}">{{ c.comment_text }}</td>
                        <td class="px-6 py-4">
                            <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase bg-blue-100 text-blue-700">{{ c.get_classification_display }}</span>
                        </td>
                        <td class="px-6 py-4 text-slate-600">{{ c.get_sub_category_display }}</td>
                        <td class="px-6 py-4">
                            {% if c.sentiment == 'negative' %}
                            <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase bg-red-100 text-red-700">{{ c.get_sentiment_display }}</span>
                            {% elif c.sentiment == 'positive' %}
                            <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase bg-green-100 text-green-700">{{ c.get_sentiment_display }}</span>
                            {% elif c.sentiment == 'neutral' %}
                            <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase bg-yellow-100 text-yellow-700">{{ c.get_sentiment_display }}</span>
                            {% else %}
                            <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase bg-slate-100 text-slate-600">{{ c.get_sentiment_display }}</span>
                            {% endif %}
                        </td>
                        <td class="px-6 py-4 max-w-[150px] truncate text-slate-500">{{ c.negative_keywords }}</td>
                        <td class="px-6 py-4 max-w-[150px] truncate text-slate-500">{{ c.positive_keywords }}</td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="8" 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 comments found." %}</p>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        {% 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 }}&{{ request.GET.urlencode }}"
                   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 }}&{{ request.GET.urlencode }}"
                       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 }}&{{ request.GET.urlencode }}"
                       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 }}&{{ request.GET.urlencode }}"
                   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 %}
