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

{% block title %}Comments Analysis Report - PX360{% endblock %}

{% block content %}
<div class="max-w-[1400px] mx-auto">
    <div class="mb-6">
        <h1 class="text-2xl font-bold text-gray-800">{% trans "Survey Comments Analysis" %}</h1>
        <p class="text-sm text-gray-500 mt-1">{% trans "Comment classification, sentiment analysis, and action plan tracking" %}</p>
    </div>

    {% if no_data %}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-12 text-center">
        <div class="w-16 h-16 bg-gray-100 rounded-full flex items-center justify-center mx-auto mb-4">
            <i data-lucide="message-square" class="w-8 h-8 text-gray-400"></i>
        </div>
        <h3 class="text-lg font-semibold text-gray-700 mb-2">{% trans "No Comment Data" %}</h3>
        <p class="text-gray-500">{% trans "No survey comments found for the selected hospital and period." %}</p>
    </div>
    {% else %}

    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 mb-6">
        <form method="get" class="flex flex-wrap gap-4 items-end">
            <div class="flex-1 min-w-[200px]">
                <label class="block text-xs font-semibold text-gray-600 mb-1">{% trans "Hospital" %}</label>
                <select name="hospital" class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm" data-tomselect>
                    {% for h in hospitals %}
                    <option value="{{ h.id }}" {% if selected_hospital == h.id|stringformat:'s' %}selected{% endif %}>{{ h.name }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="w-28">
                <label class="block text-xs font-semibold text-gray-600 mb-1">{% trans "Year" %}</label>
                <select name="year" class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm">
                    <option value="{{ selected_year }}" selected>{{ selected_year }}</option>
                </select>
            </div>
            <div class="w-36">
                <label class="block text-xs font-semibold text-gray-600 mb-1">{% trans "Quarter" %}</label>
                <select name="quarter" class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm">
                    <option value="1" {% if selected_quarter == 1 %}selected{% endif %}>Q1</option>
                    <option value="2" {% if selected_quarter == 2 %}selected{% endif %}>Q2</option>
                    <option value="3" {% if selected_quarter == 3 %}selected{% endif %}>Q3</option>
                    <option value="4" {% if selected_quarter == 4 %}selected{% endif %}>Q4</option>
                </select>
            </div>
            <button type="submit" class="px-4 py-2 bg-navy text-white text-sm font-medium rounded-lg hover:bg-navy/90 transition">
                {% trans "Apply" %}
            </button>
        </form>
    </div>

    <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
            <p class="text-xs text-gray-500 font-medium">{% trans "Total Comments" %}</p>
            <p class="text-2xl font-bold text-gray-800">{{ summary.total }}</p>
        </div>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
            <p class="text-xs text-gray-500 font-medium">{% trans "Negative" %}</p>
            <p class="text-2xl font-bold text-red-600">{{ summary.negative }}</p>
        </div>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
            <p class="text-xs text-gray-500 font-medium">{% trans "Positive" %}</p>
            <p class="text-2xl font-bold text-emerald-600">{{ summary.positive }}</p>
        </div>
    </div>

    {% if by_category %}
    <div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-6">
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
            <h3 class="text-sm font-semibold text-gray-700 mb-4">{% trans "By Category" %}</h3>
            <div id="category-chart"></div>
        </div>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
            <h3 class="text-sm font-semibold text-gray-700 mb-4">{% trans "By Sentiment" %}</h3>
            <div id="sentiment-chart"></div>
        </div>
    </div>
    {% endif %}

    {% if by_subcategory %}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 mb-6">
        <div class="p-5 border-b border-gray-200">
            <h3 class="text-lg font-semibold text-gray-800">{% trans "Sub-Category Breakdown" %}</h3>
        </div>
        <div class="overflow-x-auto">
            <table class="w-full text-sm">
                <thead>
                    <tr class="bg-[#2F75B5] text-white">
                        <th class="px-4 py-3 text-left font-semibold">{% trans "Category" %}</th>
                        <th class="px-4 py-3 text-left font-semibold">{% trans "Sub-Category" %}</th>
                        <th class="px-4 py-3 text-center font-semibold">{% trans "Count" %}</th>
                    </tr>
                </thead>
                <tbody>
                    {% for row in by_subcategory %}
                    <tr class="border-b border-gray-100 {% cycle '' 'bg-gray-50' %}">
                        <td class="px-4 py-3 text-gray-700">{{ row.category }}</td>
                        <td class="px-4 py-3 text-gray-700">{{ row.subcategory }}</td>
                        <td class="px-4 py-3 text-center font-semibold">{{ row.count }}</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    {% endif %}

    {% if action_plans %}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200">
        <div class="p-5 border-b border-gray-200">
            <h3 class="text-lg font-semibold text-gray-800">{% trans "Action Plans" %}</h3>
        </div>
        <div class="overflow-x-auto">
            <table class="w-full text-sm">
                <thead>
                    <tr class="bg-[#2F75B5] text-white">
                        <th class="px-4 py-3 text-left font-semibold">{% trans "Category" %}</th>
                        <th class="px-4 py-3 text-left font-semibold">{% trans "Comment" %}</th>
                        <th class="px-4 py-3 text-left font-semibold">{% trans "Action Plan" %}</th>
                        <th class="px-4 py-3 text-left font-semibold">{% trans "Responsible" %}</th>
                        <th class="px-4 py-3 text-center font-semibold">{% trans "Status" %}</th>
                    </tr>
                </thead>
                <tbody>
                    {% for row in action_plans %}
                    <tr class="border-b border-gray-100 {% cycle '' 'bg-gray-50' %}">
                        <td class="px-4 py-3 text-gray-700">{{ row.category }}</td>
                        <td class="px-4 py-3 text-gray-600 max-w-xs truncate">{{ row.comment }}</td>
                        <td class="px-4 py-3 text-gray-700">{{ row.action_plan }}</td>
                        <td class="px-4 py-3 text-gray-600">{{ row.responsible }}</td>
                        <td class="px-4 py-3 text-center">
                            <span class="px-2 py-1 rounded text-xs font-medium
                                {% if row.status == 'Completed' %}bg-emerald-100 text-emerald-700
                                {% elif row.status == 'On Process' %}bg-amber-100 text-amber-700
                                {% else %}bg-gray-100 text-gray-700{% endif %}">
                                {{ row.status }}
                            </span>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    {% endif %}

    {% endif %}
</div>
{% endblock %}

{% block extra_js %}
{% if not no_data and chart_data_json %}
<script>
(function() {
    var cd = JSON.parse('{{ chart_data_json|escapejs }}');

    if (cd.by_category.length > 0) {
        new ApexCharts(document.getElementById('category-chart'), {
            chart: { type: 'bar', height: 280, toolbar: { show: false } },
            plotOptions: { bar: { borderRadius: 3, horizontal: true } },
            dataLabels: { enabled: false },
            series: [{ name: 'Count', data: cd.by_category, color: '#6366F1' }],
        }).render();
    }

    if (cd.by_sentiment.length > 0) {
        new ApexCharts(document.getElementById('sentiment-chart'), {
            chart: { type: 'donut', height: 280 },
            series: cd.by_sentiment.map(function(d) { return d.y; }),
            labels: cd.by_sentiment.map(function(d) { return d.x; }),
            colors: ['#EF4444', '#10B981', '#F59E0B', '#8B5CF6', '#3B82F6'],
            legend: { position: 'bottom' },
        }).render();
    }
})();
</script>
{% endif %}
{% endblock %}
